Please bookmark this page to avoid losing your image tool!

Image Aspect Ratio Warper

(Free & Supports Bulk Upload)

Drag & drop your images here or

The result will appear here...
You can edit the below JavaScript code to customize the image tool.
function processImage(originalImg, aspectRatio = "16:9", scalingMethod = "preserve-area") {
    let ratio = 1;

    // Parse the aspect ratio input
    if (typeof aspectRatio === 'number') {
        ratio = aspectRatio;
    } else if (typeof aspectRatio === 'string') {
        if (aspectRatio.includes(':')) {
            const parts = aspectRatio.split(':');
            ratio = parseFloat(parts[0]) / parseFloat(parts[1]);
        } else if (aspectRatio.includes('/')) {
            const parts = aspectRatio.split('/');
            ratio = parseFloat(parts[0]) / parseFloat(parts[1]);
        } else {
            ratio = parseFloat(aspectRatio);
        }
    }

    // Fall back to 1:1 if invalid input is provided
    if (isNaN(ratio) || ratio <= 0) {
        ratio = 1; 
    }

    const originalWidth = originalImg.width;
    const originalHeight = originalImg.height;
    let targetWidth, targetHeight;

    // Determine new dimensions based on the chosen scaling method
    switch (scalingMethod.toLowerCase()) {
        case 'preserve-width':
            targetWidth = originalWidth;
            targetHeight = originalWidth / ratio;
            break;
        case 'preserve-height':
            targetHeight = originalHeight;
            targetWidth = originalHeight * ratio;
            break;
        case 'preserve-area':
        default:
            // Calculate a new width/height that matches the target aspect ratio 
            // while preserving the original pixel count (area)
            const originalArea = originalWidth * originalHeight;
            targetWidth = Math.sqrt(originalArea * ratio);
            targetHeight = targetWidth / ratio;
            break;
    }

    const canvas = document.createElement('canvas');
    canvas.width = Math.round(targetWidth);
    canvas.height = Math.round(targetHeight);

    const ctx = canvas.getContext('2d');
    
    // Maximize interpolation quality when stretching/squashing
    ctx.imageSmoothingEnabled = true;
    ctx.imageSmoothingQuality = 'high';

    // Draw the image, which naturally warps (stretches or squashes) it into the new dimensions
    ctx.drawImage(originalImg, 0, 0, canvas.width, canvas.height);

    return canvas;
}

Free Image Tool Creator

Can't find the image tool you're looking for?
Create one based on your own needs now!

Description

The Image Aspect Ratio Warper allows users to change the dimensions of an image to match a specific aspect ratio. By utilizing different scaling methods—such as preserving width, preserving height, or preserving the original pixel area—the tool stretches or squashes the image to fit desired proportions like 16:9, 4:3, or custom ratios. This tool is useful for designers or content creators who need to quickly adjust images to meet specific platform requirements or layout constraints without cropping the original content.

Leave a Reply

Your email address will not be published. Required fields are marked *