an easy way of Adding water marks to all your site images

Basically what we have to do is to have .htaccess file in the location where images reside and forward the request to a PHP script that will combine the requested image with the pre defined water mark using phpGD library and out put the new image. (here we use phpGD library there are other libraries that you can use.)

First of all you need add a .htaccess file into the location where your images are stored. here i have images in /images folder. This will get all the http requests going to the folder, where htaccess resides and direct them into /watermark/image_full.php file.


RewriteEngine     on 
# watermark big pictures with own watermark image 
RewriteRule    ^([0-9]+)_(.*).jpg        
/watermark/image_full.php?%{REQUEST_FILENAME} 
RewriteRule    ^([0-9]+)_(.*).gif        
/watermark/image_full.php?%{REQUEST_FILENAME} 
RewriteRule    ^([0-9]+)_(.*).png        
/watermark/image_full.php?%{REQUEST_FILENAME}  


Then we create a folder called, /watermark. Then put your water mark image in a png format in to that folder. Then we have to build the php file that will out put the image requested, with the water mark added to it.

We will be using phpGD library to merge two images and create a new image. Check the following PHP code. php file is in the same directory as the water mark file (/watermark)



// out put the result as a image
header("Content-type: image/jpeg"); 

//File name of the water mark file
$wmark='watermark_copyright.png'; 

// get the image name into a variable
$mimage=$QUERY_STRING; 

// load water mark image into a  new image object
$watermark = imagecreatefrompng($wmark); 

// get water mark width and height
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);

// load requested image into a image object if it is a gif image
if(eregi('.gif',$mimage)) {
$image = imagecreatefromgif($mimage);
}

// load requested image into a image object if it is a jpg image
elseif(eregi('.jpeg',$mimage)||eregi('.jpg',$mimage)) {
$image = imagecreatefromjpeg($mimage);
}

// load requested image into a image object if it is a png image
elseif(eregi('.png',$mimage)) {
$image = imagecreatefrompng($mimage);
}


// get the width and height of requested image
$size = getimagesize($mimage);

// here we calculate new width and height for our watermark.

$WM_N_W = $size[0]/2;
$WM_N_H = ($WM_N_W*$watermark_height)/$watermark_width;

// create image holder object for new water mark
$nw_watermark = imageCreate($WM_N_W, $WM_N_H);

// resize the water mark
ImageCopyResized($nw_watermark, $watermark, 0, 0, 0, 0, $WM_N_W, $WM_N_H, $watermark_width, $watermark_height);

// set x and y co-ordinates for placement of the watermark
$dest_x = $size[0] - $WM_N_W - 10;
$dest_y = $size[1] - $WM_N_H - 20;


//make the watermark transparent
imagecolortransparent($watermark,imagecolorat($watermark,0,0));


// merge two images
imagecopyresampled($image, $nw_watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $watermark_width, $watermark_height);


// out put image
imagejpeg($image);

// destroy image objects and free the memory
imagedestroy($watermark); 
imagedestroy($image); 

Check the comments for explanations. If you are copying and pasting this, make sure, comments are escaped with // or /* and */

0 comments:

Post a Comment