Create watermark on the Fly

I come to office earlier now. At a momment i just read my schedule of what will i do for today…? Let me see. ooh God, I have job to create a script which can protect all photo in my project using Logo, and i guess that i have to create watermark script.

However, it’s very interisting to create it. OK let’s create it…!!! (And don’t forget to help me if i’m wrong.. ). And this is the code which i used in my script:


<?php
/*create watermark
*By Gunturris - gunturris#a.t#gmail.com
*End of Feb 2007
*$imagesorce is original image
*$imagelogo is logo to blending (I used PNG)
*$alpha is alpha color output for watermark */


function watermak_on_the_fly($imagesource, $imagelogo, $alpha=15 ){
header("content-type: image/png");
//Check image resourse
if(! file_exists($
imagesource)){
$im = @imagecreate(50, 150)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 255, 255, 255);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "Img not found", $text_color);
imagepng($im);
return imagedestroy($im);
}


//Check watermark logo
if(! file_exists($imagelogo)){
$im = @imagecreate(50, 150)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 255, 255, 255);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "Logo not found", $text_color);
imagepng($im);
return imagedestroy($im);
}


settype($alpha,"integer");
if($alpha > 100) $alpha=100;
if($alpha < 0 )$alpha = 0;


list($ori_width,$ori_height,$ori_type,$ori_attr)=getimagesize($
imagesource);
switch($type){
case 1 : $img = @imagecreatefromgif($
imagesource);break; //GIF
case 2 : $img = @imagecreatefromjpeg($
imagesource); break; //JPG
case 3 : $img = @imagecreatefrompng($
imagesource); break; //PNG
default : die("IMAGE NOT SUPPORTED!"); break;
}


$watermark = imagecreatefrompng( $imagelogo );
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);


@imagecopymerge($img, $watermark, 0, ($ori_height-$watermark_height-5), 0, 0, $watermark_width, $watermark_height, $alpha);


return $img;


}
?>

And to use just ‘do’ like this:

<?php
$test = watermak_on_the_fly("imagejpeg.jpeg", "logo.png",25);
@imagejpeg($test );
@imagedestroy($test);
?>

OK, I hope it’s works good, if not please tell me or please debug by your self. Cos in my computer is no problem :p

3 Comments »

  1. Saifuddin said

    nice script

    line 39 should be $ori_type NOT $type :)

  2. gunturris said

    Ya you right,

    It shut $ory_type on switch($type)
    Thanks for your correct :D

  3. elgene said

    Hi, i tried to use your script but the image is not displaying… i just copy & paste your code… but image is not displaying.

    GD is enabled on the machine, running on php 5.2.4

    thanks

RSS feed for comments on this post · TrackBack URI

Leave a Comment