Watermark an Image

Written something you are proud of, post it here.
Post Reply
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Watermark an Image

Post 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
Last edited by jacek on Thu May 05, 2011 8:35 pm, edited 2 times in total.
Reason: syntax tags
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
wignall6
Posts: 38
Joined: Thu May 05, 2011 8:05 pm

Re: Watermark an Image

Post 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?
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: Watermark an Image

Post 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.
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Watermark an Image

Post 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 :)
Image
wignall6
Posts: 38
Joined: Thu May 05, 2011 8:05 pm

Re: Watermark an Image

Post by wignall6 »

This will be a great addion to my website, thanks very much!
Post Reply