There are some ways to protect your copyrighted images. One way is to use a watermark image so that when someone copies 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, a digital image watermark is one anti-piracy technique to protect your image copyright.
There are 2 types of digital image watermarks: visible and invisible. A visible watermark embeds your digital sign or logo into your original images. An invisible watermark embeds your digital sign but hides it under your original images — this is called Steganography.
For now I want to talk about how to make visible image watermarking using PHP. As you know, PHP is a server scripting language that allows processing images on the server side. PHP has its own image library called php-gd or ImageMagick.
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 stamping the other image with your digital sign. The second example will stamp your image with text. Basically the text is converted to an image then stamped onto the original image.
To use the code, don’t forget to enable the php-gd library in your php.ini:
extension=php_gd2.dll
Note: Your watermark image should be an 8-bit PNG. To make your image 8-bit PNG you can use ImageMagick.
The code and this article are provided by my friend Aksshay Sharma, with some editing by me.
