Please bookmark this page to avoid losing your image tool!

Image Timestamp Adder

(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.
/**
 * Adds a timestamp to an image.
 *
 * @param {Image} originalImg The original image object.
 * @param {string} [format='YYYY-MM-DD HH:mm:ss'] The format for the timestamp. Placeholders: YYYY, MM, DD, HH, mm, ss.
 * @param {string} [position='bottom-right'] The position of the timestamp. Options: 'top-left', 'top-right', 'bottom-left', 'bottom-right'.
 * @param {number} [fontSize=24] The font size of the timestamp text in pixels.
 * @param {string} [fontFamily='Arial'] The font family for the timestamp text.
 * @param {string} [textColor='#FFFFFF'] The color of the timestamp text.
 * @param {string} [backgroundColor='rgba(0, 0, 0, 0.5)'] The background color for the text box.
 * @param {number} [padding=10] The padding around the text in pixels.
 * @returns {HTMLCanvasElement} A new canvas element with the timestamp added to the image.
 */
function processImage(originalImg, format = 'YYYY-MM-DD HH:mm:ss', position = 'bottom-right', fontSize = 24, fontFamily = 'Arial', textColor = '#FFFFFF', backgroundColor = 'rgba(0, 0, 0, 0.5)', padding = 10) {
    // 1. Create a canvas and get its 2D rendering context.
    const canvas = document.createElement('canvas');
    const ctx = canvas.getContext('2d');

    // 2. Set the canvas dimensions to match the original image.
    canvas.width = originalImg.naturalWidth;
    canvas.height = originalImg.naturalHeight;

    // 3. Draw the original image onto the canvas.
    ctx.drawImage(originalImg, 0, 0);

    // 4. Generate the timestamp string.
    const now = new Date();
    const pad = (n) => (n < 10 ? '0' + n : '' + n);
    const replacements = {
        'YYYY': now.getFullYear(),
        'MM': pad(now.getMonth() + 1),
        'DD': pad(now.getDate()),
        'HH': pad(now.getHours()),
        'mm': pad(now.getMinutes()),
        'ss': pad(now.getSeconds()),
    };
    const timestampText = format.replace(/YYYY|MM|DD|HH|mm|ss/g, (match) => replacements[match]);

    // 5. Set font properties and measure the text to determine its size.
    ctx.font = `${fontSize}px ${fontFamily}`;
    const textMetrics = ctx.measureText(timestampText);

    // 6. Calculate the dimensions of the text's background box.
    const boxWidth = textMetrics.width + padding * 2;
    const boxHeight = fontSize + padding * 2; // Approximate height based on font size

    // 7. Calculate the position of the background box based on the 'position' parameter.
    let boxX, boxY;
    const margin = 5; // A small margin from the edge of the canvas
    switch (position) {
        case 'top-left':
            boxX = margin;
            boxY = margin;
            break;
        case 'top-right':
            boxX = canvas.width - boxWidth - margin;
            boxY = margin;
            break;
        case 'bottom-left':
            boxX = margin;
            boxY = canvas.height - boxHeight - margin;
            break;
        case 'bottom-right':
        default:
            boxX = canvas.width - boxWidth - margin;
            boxY = canvas.height - boxHeight - margin;
            break;
    }

    // 8. Draw the semi-transparent background box.
    ctx.fillStyle = backgroundColor;
    ctx.fillRect(boxX, boxY, boxWidth, boxHeight);

    // 9. Draw the timestamp text on top of the background box.
    ctx.fillStyle = textColor;
    ctx.textBaseline = 'top'; // Align text to the top, making y-coordinate calculation simpler.
    const textX = boxX + padding;
    const textY = boxY + padding;
    ctx.fillText(timestampText, textX, textY);

    // 10. Return the canvas with the modified 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 Timestamp Adder tool allows users to add a customizable timestamp to their images. Users can specify the format, position, font size, font family, text color, and background color of the timestamp. This tool is suitable for photographers, content creators, or anyone who wants to mark their images with the date and time they were taken. It can be utilized for creating watermarked images for social media, documenting events, or enhancing personal photo albums.

Leave a Reply

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