Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I add a watermark to an image?
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:
// 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
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:
// 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

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

// Display the final 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.
Webnetics UK Ltd.

Messages In This Thread
How do I add a watermark to an image? - by webnetics - 25-03-08, 08:36 PM

Forum Jump:

Users browsing this thread: 2 Guest(s)