Image Watermarking Using PHP

There are someways to protect your copyrighted images. One way is use watermark image so when someone copy your image, it is hard to remove the watermark and they cannot use it for their own benefit without asking your permission. In other words, digital image watermark is one way of anti piracy technique to protect your image copyright.

There are 2 types of digital image watermark, visible and invisible watermark. Visible watermark is embed your digital sign or logo to your original images. And the invisible watermark is embed your digital sign but hide it under your original images, this is so called Steganography.

For now i want to talk about how to make visible image watermarking using PHP. As you know PHP is server scripting that allow to process the images from the server side. And PHP have it’s own image library called php gd or imagemagik.

See the example below to make image watermarking using php:

<?php
//// By aksshay sharma - to create watermark images (runtime) using php
// Telling browser that the content is of image type
header('content-type: image/jpeg');
// Setting our watermark image (company logo)
$watermark_image = imagecreatefrompng('watermark_logo.png');
// Calculating Dimension of our watermark
$watermark_width = imagesx($watermark_image);
$watermark_height = imagesy($watermark_image);
$your_image = imagecreatetruecolor($watermark_width, $watermark_height);
//Set the image on which watermark is to be made
$your_image = imagecreatefromjpeg("my_image.jpg");
$size = getimagesize("my_image.jpg");
$final_x = $size[0] - $watermark_width - 5;
$final_y = $size[1] - $watermark_height - 5;
imagecopymerge($your_image, $watermark_image, $final_x, $final_y, 0, 0, $watermark_width, $watermark_height, 100);
// Generating image having watermark
imagejpeg($your_image);
// Destroying temporary images
imagedestroy($your_image);
imagedestroy($watermark_image);
?>

Example for watermark image with text using PHP:

<?php
//Using imagecopymerge() to create a translucent watermark
// Load the image on which watermark is to be applied
$original_image = imagecreatefromjpeg('pic.jpeg');

// First we create our watermark image manually from GD
$watermark = imagecreatetruecolor(100, 70);
$original_image = imagecreatefromjpeg('pic.jpeg');
//Set the hex color code for your watermark and dimension
imagestring($watermark, 5, 20, 20, 'Aksshay', 0xFFFFF);
imagestring($watermark, 3, 20, 40, 'Sharma', 0xFFFFF);

// Set the margins for the watermark and get the height/width of the watermark image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($watermark);
$sy = imagesy($watermark);

// Merge the watermark onto our photo with an opacity (transparency) of 50%
imagecopymerge($original_image, $watermark, imagesx($original_image) - $sx - $marge_right, imagesy($original_image) - $sy - $marge_bottom, 0, 0, imagesx($watermark), imagesy($watermark), 50);

// Save the image to file and free memory
imagepng($original_image, 'watermark_image.png');
imagedestroy($original_image);
?>

The first code will watermark an image with another image, so it’s like to stamp the other image with your digital sign. And the second example, it will stamp your image with your text. Basically the text will converted to image then stamp to the original image.

To use the code don’t forget to enable php gd library in your php.ini:

extension=php_gd2.dll

Note: Your watermark image is of 8-bit PNG. To make your image 8-bit PNG you can use imagemagik.

Download the script.

The code and this article is provide by my friend Aksshay Sharma, with a necessary editing by me.

Comments

  1. Thanks for the tutorial. It will be very helpful for me in short time.

Give me your feedback

This site uses Akismet to reduce spam. Learn how your comment data is processed.