Image watermark in PHP

Watermark is the most using method to copyright images. You can protect your websites images using image watermark. PHP helps to create watermark automatically when you upload an image in your website and when you execute this image in your website. You don't need to do that manually PHP will do that for you. Let's see how PHP will do that for you. We are going to create a PHP script for that and we will use PHP imagecopymerge() function for do that. create a file name as watermark.php and write down this code into the script.

watermark.php

<?php

header('content-type: image/jpeg');
$src = $_GET['src'];
$path = pathinfo($src);
$watermark = imagecreatefrompng('watermark.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatetruecolor($watermark_width, $watermark_height);
if ($path['extension']=='png')
$image = imagecreatefrompng($src);
else if ($path['extension']=='jpg'||$path['extension']=='jpeg')
$image = imagecreatefromjpeg($src);
$size = getimagesize($_GET['src']);
$dest_x = $size[0] - $watermark_width-10;
$dest_y = $size[1] - $watermark_height-10;
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 50);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);

?>
We need a watermark image for this script, this image will show in every Image as a watermark. We declared it on this script name as watermark.png. In this script we will get image using $_GET['src'] get method then merge this image using imagecopymerge() function. When this script works then all images will merge into a single image and execute as a new image with image watermark.

When we execute an image file into watermark format then we need to call image address like this
http://example.com/watermark.php?src=image_filename.jpg 
If you do tasks before correctly then this script will execute image with watermark. this script will show image watermark via watermark.php script.

You can protect your website images and confirm your authority using this watermark method. It's most popular method for websites. Hope you can understand this tutorial correctly. If you face any problem you can comment here.


Did you like this article? it will be appreciated if you share a coffee or burger with the author

Sent $5 to the author
Sent $10 to the author

Need Assistance?

I'm Sajjad Hossain, working on web application development since 2012. Do you need assistance on your project? or are you stuck with problems? I am available to help you.
If you want to contact with me ping me at -

WhatsApp
Skype


We use cookies on our website. To find out more about how and why they are used or opt-out, please read our Cookie Policy. By choosing "I Accept", you consent to our use of cookies. Cookie Policy
Top