Webnetics UK Ltd. - Forums
How do I add a watermark to an image? - Printable Version

+- Webnetics UK Ltd. - Forums (https://www.webneticsuk.com/forum)
+-- Forum: News & Announcements (https://www.webneticsuk.com/forum/forumdisplay.php?fid=2)
+--- Forum: VWDesigns Blog (https://www.webneticsuk.com/forum/forumdisplay.php?fid=12)
+---- Forum: Programming (https://www.webneticsuk.com/forum/forumdisplay.php?fid=17)
+---- Thread: How do I add a watermark to an image? (/showthread.php?tid=73)



How do I add a watermark to an image? - webnetics - 25-03-08

Here a simple routine to protect your photos with a watermark. If you want to place some identifying image or text within the original image to show that you own the copyright to it.

With the GD library and PHP, watermarking’s it’s easy as 123.

The imagestring function can be used to place text within an image, while the imagecopymerge function can be used to place another image within your original image. Using either of these functions is extremely easy.

Displaying a Text Watermark

Adding text to an image is the simplest form of watermarking.
PHP Code:
<?php
// Load the original image
$image imagecreatefromjpeg('your image direcory/image.jpg');

// Get a color and allocate to the image pallet
$color imagecolorallocate($image686868);

// Add the text to the image
imagestring($image5900"Virtual Web Designs’"$color);

// Send the HTTP content header
header('Content-Type: image/jpg');

// Display the final image
imagejpeg($image);
?>
The imagecolorallocate function allows you to create a new colour to use for drawing on the image by specifying the red, green, and blue components. This function returns a number, which identifies that color in the image.

Once you have the color in hand, you can use the imagestring function to place the text over the image. The first of the function’s arguments is the image, and the second is a font number—the numbers 1–5 refer to built-in fonts. You can use imageloadfont to make other fonts available. The third and fourth arguments represent the horizontal and vertical coordinates at which the text should be drawn on the image. The fifth argument contains the text you wish to be placed in the image, and the last argument specifies the color of the text.

Displaying a Graphical Watermark

A logo or some other identifiable graphic with a transparent background is easily placed over another image.

PHP Code:
<?php
// Load the original image
$image imagecreatefromjpeg('your image directory/thumb_image.jpg');

// Get image width
$iWidth imagesx($image);

// Get the watermark image
$watermark imagecreatefrompng('your image directory/vwd_logo.png');

// Get the height and width
$wmWidth imagesx($watermark);
$wmHeight imagesy($watermark);

// Find the far right position
$xPos $iWidth $wmWidth;

// Copy the watermark to the top right of original image
imagecopymerge($image$watermark$xPos000$wmWidth$wmHeight50); 

// Send the HTTP content header
header('Content-Type: image/jpg');

// Display the final image
imagepng($image);
?>

The process is a simple matter. Load the original image and the watermark image; then, once the original image’s height and width have been obtained, use imagecopymerge to place the watermark on the original image. The first two arguments to the imagecopymerge function are the original or destination image object, and the source image object—the watermark, in our case. The next four arguments represent the x and y coordinates of the destination image and source image respectively, starting from the top-left corner of the images. The following two arguments represent the width and height of the source image. The last argument represents the level of transparency desired for true color images—an argument of 100 specifies no transparency, while an argument of 0 causes the original image to remain unmarked by the watermark.