Please bookmark this page to avoid losing your image tool!

Image Aspect Ratio And Stretch Spacing Adjuster

(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, imageAspectRatio = "1.85:1", imageFitMode = "Stretch", canvasAspectRatio = "24:9", spaceBgColor = "#000000") {
    // Parser for user-friendly aspect ratio strings
    const parseRatio = (ratio) => {
        let str = String(ratio).trim();
        
        // Handle common typographical patterns like "1 85 1" -> "1.85:1" or "2 35 1" -> "2.35:1"
        str = str.replace(/^(\d+)\s+(\d+)\s+(\d+)$/, "$1.$2:$3");
        // Handle separated values like "24 9" -> "24:9" or "16 9" -> "16:9"
        str = str.replace(/^(\d+(?:\.\d+)?)\s+(\d+(?:\.\d+)?)$/, "$1:$2");
        
        let parts = str.split(':');
        if (parts.length === 2) {
            let num = parseFloat(parts[0]);
            let den = parseFloat(parts[1]);
            return (den && !isNaN(den) && den !== 0) ? num / den : 1;
        }
        
        parts = str.split(/\s+/);
        if (parts.length >= 2) {
            let num = parseFloat(parts[0]);
            let den = parseFloat(parts[1]);
            return (den && !isNaN(den) && den !== 0) ? num / den : 1;
        }
        
        let parsed = parseFloat(str);
        return isNaN(parsed) || parsed <= 0 ? 1 : parsed;
    };

    const innerAR = Math.max(0.01, parseRatio(imageAspectRatio));
    const outerAR = Math.max(0.01, parseRatio(canvasAspectRatio));

    const baseH = originalImg.height;
    const baseW = originalImg.width;

    let innerW, innerH;
    let sx = 0, sy = 0, sw = baseW, sh = baseH;

    // Apply fit mode (Stretch, Crop, Fit) to reach the primary target inner aspect ratio
    const mode = String(imageFitMode).toLowerCase();
    
    if (mode === "crop") {
        const origAR = baseW / baseH;
        if (origAR > innerAR) {
            sh = baseH;
            sw = baseH * innerAR;
            sx = (baseW - sw) / 2;
        } else {
            sw = baseW;
            sh = baseW / innerAR;
            sy = (baseH - sh) / 2;
        }
        innerW = sw;
        innerH = sh;
    } else if (mode === "fit") {
        const origAR = baseW / baseH;
        if (origAR > innerAR) {
            innerW = baseW;
            innerH = baseW / innerAR;
        } else {
            innerH = baseH;
            innerW = baseH * innerAR;
        }
    } else { 
        // Default to "stretch" as expected by standard behavior
        innerH = baseH;
        innerW = innerH * innerAR;
    }

    // Determine outer canvas dimensions enclosing inner width & height while maintaining the spacing aspect ratio
    let outerW = innerW;
    let outerH = innerH;
    
    if (innerW / innerH < outerAR) {
        outerW = outerH * outerAR;
    } else {
        outerH = outerW / outerAR;
    }

    const canvas = document.createElement('canvas');
    canvas.width = Math.max(1, Math.round(outerW));
    canvas.height = Math.max(1, Math.round(outerH));
    const ctx = canvas.getContext('2d');

    // Fill the underlying spacing area defined by the outer aspect ratio
    ctx.fillStyle = spaceBgColor;
    ctx.fillRect(0, 0, canvas.width, canvas.height);

    // Coordinate offsets to perfectly center the manipulated inner image onto the final canvas
    const drawX = (canvas.width - innerW) / 2;
    const drawY = (canvas.height - innerH) / 2;

    // Draw executing the stretching, cropping or fitting instruction
    ctx.drawImage(
        originalImg,
        sx, sy, sw, sh,                               // Source mapping frame 
        Math.round(drawX), Math.round(drawY),         // Destination origin
        Math.max(1, Math.round(innerW)),              // Destination width
        Math.max(1, Math.round(innerH))               // Destination 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

This tool allows you to adjust the aspect ratio of an image and place it within a larger canvas of a specific ratio. You can choose how the original image is handled through three different modes: ‘Stretch’ to fill a target aspect ratio, ‘Crop’ to fill the area by trimming the edges, or ‘Fit’ to ensure the entire image is visible. Additionally, the tool can add customizable background spacing (letterboxing or pillarboxing) to match a secondary canvas aspect ratio, making it useful for preparing images for specific cinematic formats, social media layouts, or digital displays.

Leave a Reply

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