Please bookmark this page to avoid losing your image tool!

Image Redaction 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, redactionAreas = "", redactionColor = "black") {
    // 1. Create a canvas element
    const canvas = document.createElement('canvas');

    // 2. Set the canvas dimensions to match the originalImg
    // Assumes originalImg is a loaded JavaScript Image object (e.g., from new Image() or <img> tag custom_image_data_attributes.onload has fired)
    canvas.width = originalImg.naturalWidth;
    canvas.height = originalImg.naturalHeight;

    // Handle cases where the image might not be loaded or has no dimensions
    if (canvas.width === 0 || canvas.height === 0) {
        console.warn("Original image has zero dimensions. Make sure the image is loaded and valid before processing.");
        // Return an empty canvas or null. Null might be better to indicate failure.
        // Or, return a 0x0 canvas, but it won't be useful.
        const errorCanvas = document.createElement('canvas');
        errorCanvas.width = 0;
        errorCanvas.height = 0;
        // Optionally, you could draw some error text on a small fixed-size canvas if that's desired.
        // For now, returning the 0x0 canvas to fulfill "must return a single canvas"
        return errorCanvas;
    }

    // 3. Get the 2D rendering context
    const ctx = canvas.getContext('2d');
    if (!ctx) {
        console.error("Could not get 2D rendering context for the image redaction tool.");
        // If context cannot be obtained, we cannot proceed.
        // Return the canvas as is (it will be blank and have dimensions).
        return canvas;
    }

    // 4. Draw the originalImg onto the canvas
    try {
        ctx.drawImage(originalImg, 0, 0, canvas.width, canvas.height);
    } catch (e) {
        console.error("Error drawing the original image onto the canvas. This might be due to CORS restrictions if the image is from a different origin.", e);
        // If drawing the original image fails, we can still draw redaction marks on a blank canvas,
        // or return the canvas as is (blank), or return null.
        // For this implementation, we'll proceed to draw redaction marks on what might be a blank canvas.
        // Alternatively, to signal a more critical failure:
        // return canvas; // Returns a blank canvas of correct size
    }

    // 5. Parse the redactionAreas string and apply redactions
    const trimmedRedactionAreas = redactionAreas.trim();
    if (trimmedRedactionAreas !== "") {
        const areas = trimmedRedactionAreas.split(';');
        areas.forEach(areaStrInput => {
            const areaStr = areaStrInput.trim();
            if (areaStr === "") { // Skip empty segments caused by "foo;;bar"
                return;
            }

            const parts = areaStr.split(',').map(s => {
                const num = Number(s.trim());
                return isNaN(num) ? NaN : num; // Ensure non-numeric strings become NaN
            });

            if (parts.length === 4 && parts.every(p => !isNaN(p))) {
                const [x, y, w, h] = parts;

                // Validate width and height (must be positive)
                if (w <= 0 || h <= 0) {
                    console.warn(`Invalid redaction area dimensions: "${areaStr}". Width and height must be positive. Skipping this area.`);
                    return; // Skip this invalid area
                }

                // Set the fill style to redactionColor
                // The browser handles invalid color strings gracefully (usually defaults to black or ignores the change)
                ctx.fillStyle = redactionColor;

                // Draw a filled rectangle
                ctx.fillRect(x, y, w, h);
            } else {
                console.warn(`Invalid redaction area format or non-numeric values: "${areaStr}". Expected "x,y,w,h" with numeric values. Skipping this area.`);
            }
        });
    }

    // 7. Return the canvas with the redacted 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 Redaction Tool allows users to securely redact specific areas of an image. By specifying the coordinates and dimensions of the areas to be redacted, users can obscure sensitive content in their photos, ensuring privacy and confidentiality. This tool is useful in various situations, such as preparing images for publication, handling confidential documents, or complying with data protection regulations. The redacted areas can be filled with a solid color, typically black, to effectively hide the content beneath.

Leave a Reply

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