Page 1 of 1

Watermark an Image

Posted: Thu May 05, 2011 8:30 pm
by bowersbros
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);


}

echo 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

Re: Watermark an Image

Posted: Thu May 05, 2011 8:31 pm
by wignall6
This looks really nice, so if i was to have a image hosting website i could add this code and it add a watermark to all the images?

Re: Watermark an Image

Posted: Thu May 05, 2011 8:33 pm
by bowersbros
wignall6 wrote:This looks really nice, so if i was to have a image hosting website i could add this code and it add a watermark to all the images?
Yes, that is a purpose of something like this.

You would call the function on the image that was uploaded, and then save that new image to overwrite the old one.

Re: Watermark an Image

Posted: Thu May 05, 2011 8:37 pm
by jacek
To be a pain: can you use the syntax buttons when posting code, as that’s what XHBB will eventually use for code.

also, this looks good :)

Re: Watermark an Image

Posted: Thu May 05, 2011 9:08 pm
by wignall6
This will be a great addion to my website, thanks very much!