Senin, 09 Februari 2009

Watermark Images on the Fly in PHP 2

We'll keep the script simple by using a variable, $_GET['src'], which should have the complete path to the file that needs to be watermarked. Alternatively, you can build the path in the script, which is a bit more secure and private. As long as that function receives the full path as its first parameter, everything will be fine.

Next, we are going to make some dimension calculations on the image being watermarked. These dimension calculations will be completed (for no other reason but to learn about new functions} using the getimagesize() function. The getimagesize() function works by returning an array where key "0" = width and key "1" = height.

The procedure of the next three lines of code are to:


grab the dimensions of the image we want watermarked, $image

subtract the corresponding dimension from our watermarking image

add a 5 pixel margin

Finally, we should be left with the exact pixel (further referenced as the "destination location") at which where our watermarking image ($watermark) should be placed on the image to be watermarked ($image):

$size = getimagesize($_GET['src']);
$dest_x = $size[0] - $watermark_width - 5;
$dest_y = $size[1] - $watermark_height - 5;

Merging the Image and the Watermark
Here is the most complex function in the entire script: imagecopymerge(). The official syntax for this function is:

int imagecopymerge ( resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y, int src_w, int src_h, int pct )

This excerpt from the manual explains this function better than I ever could:

Copy a part of src_im onto dst_im starting at the x,y coordinates src_x, src_y with a width of src_w and a height of src_h. The portion defined will be copied onto the x,y coordinates, dst_x and dst_y. The two images will be merged according to pct which can range from 0 to 100.

(Source: PHP Manual)

In short, the following line of code merges the two images using the destination locations we calculated earlier:

imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);

Our final lines of code will output the image merged with the watermark to the browser using imagejpeg(). They will then use imagedestroy() to clear the RAM of all the images we have loaded with the GD library.

imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);

There you have it! You can now watermark any image on your Web server dynamically using one of the deadliest combinations of free software: PHP and the GD image library. The complete watermarking script is below. Enjoy!

?

header('content-type: image/jpeg');

$watermark = imagecreatefrompng('watermark.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefromjpeg($_GET['src']);
$size = getimagesize($_GET['src']);
$dest_x = $size[0] - $watermark_width - 5;
$dest_y = $size[1] - $watermark_height - 5;
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);

?


---------------------------------


Related :

UsingCrystalReports6
VerifyUserEmailAddressPHP-1
VerifyUserEmailAddressPHP-2
viewinformationvb2005
WatermarkImagesFlyPHP-1
WatermarkImagesFlyPHP-2
WhatAretheIssues
What-isMySQL
WhatSQLServerExpress
writefileinvb2005
WritingFileDialogBox
SimpleDatabasevb6
SQLServer2005fromVisualBasic6
SQLServer2005withPHP
SQLServerSecurityGuidelines
TransferringFiles
UsingCrystalReports6