Watermark an image Dynamically in PHP

Simple script to watermark an image when it is called on Browser !!

<?php
// this script creates a watermarked image from an image file - can be a .jpg .gif or .png file
// where watermark.gif is a mostly transparent gif image with the watermark - goes in the same directory as this script
// where this script is named watermark.php
// call this script with an image tag
//<img src=”watermark.php?path=imagepath”> where path is a relative path such as subdirectory/image.jpg
$imagesource = $_GET['path'];
// $watermarkPath = $_GET['watermark']; // if u want to use it dynamically then pass the watermarkfile location when calling
$watermarkPath =”watermark.png”;
$filetype = substr($imagesource,strlen($imagesource)-4,4);
$filetype = strtolower($filetype);
$watermarkType = substr($watermarkPath,strlen($watermarkPath)-4,4);
$watermarkType = strtolower($watermarkType);

if($filetype == “.gif”)
$image = @imagecreatefromgif($imagesource);
else
if($filetype == “.jpg” || $filetype == “jpeg”)
$image = @imagecreatefromjpeg($imagesource);
else
if($filetype == “.png”)
$image = @imagecreatefrompng($imagesource);
else
die();

if(!$image)
die();

if($watermarkType == “.gif”)
$watermark = @imagecreatefromgif($watermarkPath);
else
if($watermarkType == “.png”)
$watermark = @imagecreatefrompng($watermarkPath);
else
die();

if(!$watermark)
die();

$imagewidth = imagesx($image);
$imageheight = imagesy($image);
$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);
$startwidth = (($imagewidth - $watermarkwidth)/2);
$startheight = (($imageheight - $watermarkheight)/2);
imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>

save the file as watermark.php

method to call the image is

<img src=’watermark.php?path=myimage.jpg’>

in the example watemark.png is the locationof the watermarking logo and myimage.jpg is the relative path


Image:

code:
< img src="http://developer.bigkerala.com/scripts/watermark.php?path=http://www.newlaunches.com/entry_images/1107/06/google.jpg" alt="" />

Result

In Thursday, January 15, 2009


Leave a Reply

Popular Posts

Total Pageviews