Resize Image
Resize an image to specific dimensions with control over aspect ratio and resizing method.
/api/image/resize
Request Parameters
Parameter | Type | Required | Description |
---|---|---|---|
file | File | Yes | The image file to resize. Supported formats: JPEG, PNG, WebP, BMP, TIFF, GIF. |
width | Integer | Yes* | The target width in pixels. Range: 1-10000. *Either width or height must be provided. |
height | Integer | Yes* | The target height in pixels. Range: 1-10000. *Either width or height must be provided. |
keep_aspect_ratio | Boolean | No | Whether to maintain the original aspect ratio. If true and both width and height are provided, the image will be resized to fit within the specified dimensions without stretching. Default: true . |
Response
Returns the resized image in the specified or original format.
Example Request
// example.sh
curl -X POST "https://oyyi.xyz/api/image/resize" -H "accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F "file=@image.jpg" \
-F "width=800" \
-F "height=600" \
-F "keep_aspect_ratio=true" \
--output resized.jpg
Example with Python
// resize.py
import requests
url = "https://oyyi.xyz/api/image/resize"
files = {
'file': open('image.jpg', 'rb')
}
data = {
'width': 800,
'height': 600,
'keep_aspect_ratio': True
}
response = requests.post(url, files=files, data=data)
# Save the resized image
with open('resized.jpg', 'wb') as f:
f.write(response.content)
Example with JavaScript
// resize.js
// Using fetch API
const formData = new FormData();
formData.append('file', fileInput.files[0]);
formData.append('width', '800');
formData.append('height', '600');
formData.append('keep_aspect_ratio', 'true');
fetch('https://oyyi.xyz/api/image/resize', {
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 = 'resized.jpg';
a.click();
// Clean up by revoking the URL
URL.revokeObjectURL(url);
})
.catch(error => console.error('Error:', error));
Resizing Methods
Lanczos
High-quality resampling algorithm that produces sharp results. Best for most use cases, especially for downscaling. Slower but higher quality.
Bicubic
Good balance between quality and speed. Produces smooth results with some sharpness. Good for general-purpose resizing.
Bilinear
Faster than Lanczos and Bicubic, but produces softer results. Good for thumbnails or when speed is more important than quality.
Nearest
Fastest method but lowest quality. Uses nearest-neighbor interpolation. Good for pixel art or when you want to preserve hard edges.
How It Works
The resize endpoint changes the dimensions of an image using various resampling algorithms:
- Image Loading: The uploaded image is loaded into memory.
- Dimension Calculation: The target dimensions are calculated based on the provided parameters:
- If both width and height are provided and keep_aspect_ratio is true, the image is resized to fit within these dimensions while maintaining its aspect ratio.
- If both width and height are provided and keep_aspect_ratio is false, the image is stretched to exactly match these dimensions.
- If only width or height is provided, the other dimension is calculated to maintain the aspect ratio.
- If scale_factor is provided, the dimensions are calculated by multiplying the original dimensions by this factor.
- Upscale Check: If upscale is false and the target dimensions would enlarge the image, the original dimensions are preserved.
- Resampling: The image is resized using the specified method (Lanczos, Bicubic, Bilinear, or Nearest).
- Format Conversion: If an output_format is specified, the image is converted to that format.
- Quality Setting: For lossy formats (JPEG, WebP), the specified quality level is applied.
The result is an image with the requested dimensions, optimized according to the specified parameters.
Aspect Ratio Behavior
keep_aspect_ratio = true
The image will be resized to fit within the specified dimensions while maintaining its original aspect ratio. This means one dimension might be smaller than requested to avoid stretching the image.
keep_aspect_ratio = false
The image will be stretched or compressed to exactly match the specified width and height, which may distort the image if the target aspect ratio differs from the original.
Error Responses
Status Code | Description |
---|---|
400 | Bad request. Missing required parameters, invalid width/height values, invalid method, invalid scale_factor, invalid upscale value, invalid output_format, invalid quality value, 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
- Either width, height, or scale_factor must be provided. If multiple are provided, they are prioritized in this order: scale_factor, then width and height together, then width or height individually.
- When only one dimension (width or height) is provided, the other dimension is calculated automatically to maintain the aspect ratio.
- The Lanczos method provides the highest quality results but is also the slowest. For batch processing or real-time applications, consider using Bilinear or Bicubic methods.
- Setting upscale to false is useful to prevent image quality degradation when resizing small images to larger dimensions.
- The maximum file size allowed is 50MB.
- For animated GIFs, only the first frame will be resized unless the output format is also GIF.
- This operation preserves the alpha channel (transparency) if present in the original image and the output format supports it (PNG, WebP, GIF).
Use Cases
- Creating thumbnails for image galleries
- Optimizing images for web display
- Preparing images for social media platforms
- Standardizing image dimensions for a consistent layout
- Reducing file size by decreasing image dimensions
- Creating responsive image sets for different device sizes
- Preparing images for print at specific dimensions