Please bookmark this page to avoid losing your image tool!

Image Expand With Border Adding 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, borderWidth = 10, borderColor = 'black') {
    let numericBorderWidth = Number(borderWidth);

    // If borderWidth is not a valid number (e.g., "abc" or NaN passed directly),
    // issue a warning and default to 0. This makes the function more robust.
    if (isNaN(numericBorderWidth)) {
        console.warn(`Invalid borderWidth value provided: "${borderWidth}". Defaulting to 0 for the border width.`);
        numericBorderWidth = 0;
    }

    // Ensure the border width is non-negative. "Adding space" implies positive or zero width.
    const effectiveBorderWidth = Math.max(0, numericBorderWidth);

    const canvas = document.createElement('canvas');
    const ctx = canvas.getContext('2d');

    // It's assumed that originalImg is an HTMLImageElement (or similar like HTMLCanvasElement/ImageBitmap).
    // If originalImg is not yet loaded, originalImg.width and originalImg.height might be 0.
    // The function will proceed, creating a canvas that is primarily the border color if the image has no dimensions.
    const originalWidth = originalImg.width;
    const originalHeight = originalImg.height;

    // Calculate new dimensions for the canvas.
    const newWidth = originalWidth + 2 * effectiveBorderWidth;
    const newHeight = originalHeight + 2 * effectiveBorderWidth;

    // Set canvas dimensions.
    // If newWidth or newHeight are 0 (e.g., 0x0 image and 0 border), canvas will be 0x0. This is valid.
    // Browsers have limits on canvas size; exceeding them is handled by the browser (e.g., clamping or error).
    canvas.width = newWidth;
    canvas.height = newHeight;

    // Fill the entire canvas with the borderColor.
    // This acts as the background or the "border" color.
    ctx.fillStyle = borderColor;
    ctx.fillRect(0, 0, newWidth, newHeight);

    // Draw the original image onto the new canvas, offset by the border width.
    // Only attempt to draw if the original image has positive dimensions.
    if (originalWidth > 0 && originalHeight > 0) {
        ctx.drawImage(originalImg, effectiveBorderWidth, effectiveBorderWidth, originalWidth, originalHeight);
    }
    // If originalWidth or originalHeight is 0, the canvas will just be the border color,
    // which is the correct behavior for an image with no content.

    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 Expand With Border Adding Tool allows users to enhance their images by adding customizable borders. Users can specify the width and color of the border to frame their images, making them stand out in presentations, social media posts, or printed materials. This tool is useful for creating a polished look for photos, emphasizing subject matter, or simply adding visual style to images before sharing or displaying them.

Leave a Reply

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