Page 1 of 1

Watermark an Image

Posted: Thu May 05, 2011 8:30 pm
by bowersbros
jacek wrote:This is an archive post, any discussion should take place here http://betterphp.co.uk/board/viewtopic.php?f=5&t=6
This code will allow you to tile watermark an image.

I was working on a project called Spark Lemon while producing this, so if you use the code in this example, thats where the image derived from.

Hope you find it useful, also Please do NOT use the smart lemon logo as the tile, as it is obviously copyritten.
<?php

function add_watermark($image) {
    $overlay = 'http://sparklemon.com/php/watermark.png';
    
    $w_offset = 0;
    $h_offset = 0;
    
    $extension = strtolower(substr($image, strrpos($image, ".") +1));
    
    switch ($extension)
    {
        case 'jpg':
        $background = imagecreatefromjpeg($image);
        break;
        case 'jpeg':
        $background = imagecreatefromjpeg($image);
        break;
        case 'png':
        $background = imagecreatefrompng($image);
        break;
        default:
        die("Image type not supported");
    }
    
    $base_width = imagesx($background);
    $base_height = imagesy($background);
    imagealphablending($background, true);
    
    $overlay = imagecreatefrompng($overlay);

    
    imagesettile($background, $overlay);

    // Make the image repeat
    imagefilledrectangle($background, 0, 0, $base_width, $base_height, IMG_COLOR_TILED);
    header('Content-type: image/png');
    imagepng($background);
}

add_watermark('http://c360958.r58.cf2.rackcdn.com/6831 ... 296d18.jpg');

?>
EDIT: the final image (http://c360958.r58.cf2.rackcdn.com/6831 ... 296d18.jpg) is from my game currently in development, I own all graphics within that document, so please do not replicate or reproduce. Thank you

ADMIN EDIT: removed echo and tidied up indenting :)