Please bookmark this page to avoid losing your image tool!

Image Upscaler Tool

(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, scaleFactor = 2, interpolationQuality = "high") {
    // 1. Sanitize scaleFactor: must be >= 1 for an upscaler. Default to 2 if invalid.
    let numericScaleFactor = Number(scaleFactor);

    if (isNaN(numericScaleFactor) || numericScaleFactor <= 0) {
        console.warn(`processImage: Invalid scaleFactor "${scaleFactor}" (e.g., non-numeric, zero, or negative). Using default upscale factor 2.`);
        numericScaleFactor = 2;
    } else if (numericScaleFactor < 1) {
        // This is an "Upscaler" tool, so it should not make the image smaller.
        // If scaleFactor is < 1, set it to 1 (original size).
        console.warn(`processImage: scaleFactor "${scaleFactor}" is < 1. Upscaler tool will not downscale. Setting scaleFactor to 1 (original size).`);
        numericScaleFactor = 1; 
    }
    // At this point, numericScaleFactor is guaranteed to be >= 1.

    // 2. Sanitize interpolationQuality
    // `interpolationQuality` defaults to "high" via function parameter default.
    // `String(...)` handles if `interpolationQuality` is explicitly passed as `null`.
    // `toLowerCase()` makes the check case-insensitive for common inputs like "HIGH" or "Medium".
    const lowerCaseInterpolationQuality = String(interpolationQuality).toLowerCase();
    const validQualities = ["low", "medium", "high"];
    let finalInterpolationQuality = "high"; // Initialize with a safe default, will be used if provided one is invalid.

    if (validQualities.includes(lowerCaseInterpolationQuality)) {
        finalInterpolationQuality = lowerCaseInterpolationQuality;
    } else {
        // Warn if the provided quality string is not valid.
        // This warning is not shown if `interpolationQuality` was left to its default "high",
        // as `lowerCaseInterpolationQuality` would be "high" and thus valid.
        console.warn(`processImage: Invalid interpolationQuality "${interpolationQuality}". Using default "high".`);
        // `finalInterpolationQuality` remains "high" as set initially.
    }
    
    // 3. Prepare canvas
    const canvas = document.createElement('canvas');
    const ctx = canvas.getContext('2d');

    // 4. Check if originalImg is loaded and has valid dimensions
    // `originalImg` is expected to be an HTMLImageElement (like `new Image()` produces).
    // `naturalWidth` and `naturalHeight` give the intrinsic dimensions of the loaded image.
    const sourceWidth = originalImg.naturalWidth;
    const sourceHeight = originalImg.naturalHeight;

    if (!sourceWidth || !sourceHeight || sourceWidth === 0 || sourceHeight === 0) {
        // This can happen if the image isn't loaded yet, or if it's a 0x0 image.
        console.error("processImage: originalImg has zero width or height. Ensure the image is fully loaded and valid before calling processImage.");
        // Return a minimal 1x1 canvas to prevent errors downstream.
        // The caller is responsible for ensuring the image is loaded.
        canvas.width = 1;
        canvas.height = 1;
        return canvas;
    }
    
    // 5. Calculate new dimensions for the upscaled image
    const newWidth = sourceWidth * numericScaleFactor;
    const newHeight = sourceHeight * numericScaleFactor;

    // 6. Set canvas dimensions
    // Canvas dimensions must be integers. Rounding is generally preferred over flooring (which is the default conversion).
    // Also, ensure dimensions are at least 1x1 to avoid issues with 0-size canvases, or if calculated dimensions become < 1.
    canvas.width = Math.max(1, Math.round(newWidth));
    canvas.height = Math.max(1, Math.round(newHeight));

    // 7. Set image rendering properties for scaling
    // `imageSmoothingEnabled` is true by default in most modern browsers.
    // Explicitly setting it can be useful for clarity or targeting older browser behavior.
    ctx.imageSmoothingEnabled = true; 
    // `imageSmoothingQuality` hints at the algorithm for smoothing (e.g., "high" for bicubic-like).
    // Support and behavior can vary slightly between browsers.
    ctx.imageSmoothingQuality = finalInterpolationQuality;

    // 8. Draw the original image onto the canvas, scaled to the new dimensions.
    // This is where the actual "upscaling" (resampling) happens, performed by the browser's rendering engine.
    ctx.drawImage(originalImg, 0, 0, canvas.width, canvas.height);

    // 9. Return the canvas element containing the upscaled image
    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 Upscaler Tool allows users to enhance the size of images while maintaining or improving quality. By specifying a scale factor and desired interpolation quality, users can enlarge their images for various applications, such as preparing high-resolution graphics for print, improving image clarity for presentations, or enhancing images for web use. This tool is particularly useful for designers, photographers, and anyone needing to resize images without losing detail.

Leave a Reply

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