Generate Thumbnail
Generate optimized thumbnails with high performance using libvips.
POST
/api/image/thumbnail
Request Parameters
Parameter | Type | Required | Description |
---|---|---|---|
file | File | Yes | The image file to create a thumbnail from. Supported formats: JPEG, PNG, WebP, BMP, TIFF. |
width | Integer | Yes | Target width in pixels for the thumbnail. |
height | Integer | Yes | Target height in pixels for the thumbnail. |
crop | Boolean | No | Whether to crop the image to exactly match the target dimensions. If false, the image will be resized to fit within the dimensions while maintaining aspect ratio. Default: false . |
Response
Returns the thumbnail image in the same format as the input file.
Example Request
// example.sh
curl -X POST "https://oyyi.xyz/api/image/thumbnail" -H "accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F "file=@image.jpg" \
-F "width=200" \
-F "height=200" \
-F "crop=false" \
--output thumbnail.jpg
Example with Python
// thumbnail.py
import requests
url = "https://oyyi.xyz/api/image/thumbnail"
files = {
'file': open('image.jpg', 'rb')
}
data = {
'width': 200,
'height': 200,
'crop': False
}
response = requests.post(url, files=files, data=data)
# Save the thumbnail image
with open('thumbnail.jpg', 'wb') as f:
f.write(response.content)
Example with JavaScript
// thumbnail.js
// Using fetch API
const formData = new FormData();
formData.append('file', fileInput.files[0]);
formData.append('width', '200');
formData.append('height', '200');
formData.append('crop', 'false');
fetch('https://oyyi.xyz/api/image/thumbnail', {
method: 'POST',
body: formData
})
.then(response => response.blob())
.then(blob => {
// Create a URL for the blob
const url = URL.createObjectURL(blob);
// Create a link to download the image
const a = document.createElement('a');
a.href = url;
a.download = 'thumbnail.jpg';
a.click();
// Clean up by revoking the URL
URL.revokeObjectURL(url);
})
.catch(error => console.error('Error:', error));
How It Works
The thumbnail endpoint is specifically optimized for generating thumbnails with high performance:
- Efficient Loading: The image is loaded at the lowest resolution needed for the target size, saving memory and processing time.
- Dimension Calculation: The target dimensions are calculated based on the provided parameters:
- If crop is false, the image is resized to fit within the specified dimensions while maintaining its aspect ratio.
- If crop is true, the image is resized and cropped to exactly match the specified dimensions.
- Optimized Processing: Uses libvips for high-performance image processing, which is much faster than traditional methods, especially for large images.
- Format Preservation: The output thumbnail maintains the same format as the input image.
Error Responses
Status Code | Description |
---|---|
400 | Bad request. Missing required parameters, invalid width/height values, or invalid file format. |
413 | Payload too large. The file size exceeds the maximum allowed limit. |
500 | Internal server error. Something went wrong on the server. |
Notes
- The thumbnail generation is optimized for performance and is much faster than regular resize operations.
- When crop is false, the image will be resized to fit within the specified dimensions while maintaining its aspect ratio.
- When crop is true, the image will be resized and cropped from the center to exactly match the specified dimensions.
- The maximum file size allowed is 10MB.
- This operation preserves the alpha channel (transparency) if present in the original image and the output format supports it.
Use Cases
- Creating thumbnails for image galleries and product listings
- Generating preview images for documents or videos
- Creating avatar images for user profiles
- Optimizing images for mobile devices
- Creating consistent image sizes for grid layouts
- Batch processing large image collections for web display
Performance
The thumbnail endpoint is highly optimized and can process a 12MP image in approximately 50ms, making it suitable for real-time applications and batch processing.