Watermark an Image

Code that the staff think is particularly good will be copied here.
Locked
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Watermark an Image

Post 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 :)
Last edited by jacek on Fri May 06, 2011 11:59 pm, edited 5 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
Locked