Networking, Programming and Graphics - Tutorials
ONLINEHOWTO.net Tutorials Category

PHP GD Display Watermark with PNG Alpha Blending

Type: Code Networking, Programming and Graphics - Tutorials
Networking, Programming and Graphics - Tutorials
Level: Intermediate Networking, Programming and Graphics - Tutorials Networking, Programming and Graphics - Tutorials 
Networking, Programming and Graphics - Tutorials
Date: 2010-Feb-03
Networking, Programming and Graphics - Tutorials
Visited: 914 times
Networking, Programming and Graphics - Tutorials
Rating: Networking, Programming and Graphics - Tutorials
Networking, Programming and Graphics - Tutorials
Published: Ivory Morhuld

If you have a web gallery with truly your copyrighted image like photography, paintings, illustrations, drawings, etc. probably you want to protect this images from stealing.

Most easiest, automated and secure way is to display the images with some watermark image over the original image with your name, email, website or some other copyright information like shown on the image below and like in all other images and screenshots in Onlinehowto.net website. For this example we will use famous Mona Lisa picture by Leonardo da Vinci.

In this tutorial I will show you how to display this image with PNG Alpha Blending Watermark over it and all of this to be done in real time (on the fly) by using PHP GD library.
PHP GD Display Watermark with PNG Alpha Blending
Like you can see the above image uses transparent watermark and the background image can be seen through the watermark. Also this transparency affecting the watermark image edges smoothly with true Alpha Blending. For this we will use transparent 24bit PNG image for watermark.

Now I will show you what is the differences between using Alpha Blending transparency and without:
PHP GD Display Watermark with PNG Alpha Blending
And here is the complete code using PHP and GD Library.

Please note that this PHP script get the background image name by GET parameter, attaches the watermark transparent PNG and then send the finished result directly to browser like JPG resource. And for this, you have to use the script (if you save under pic.php name) by this way in your HTML files:

<img src="pic.php?pic=mona_lisa.jpg" alt="Mona Lisa picture"/>

Note: Also you have in mind what parameters are enough secure to receive by $_GET['pic'] and to check, and waht to reject.
<?php
// set watermark image and path to it
$watermark="onlinehowto_logo.png";

// read the original background image set by ?pic= 
$img=imagecreatefromjpeg($_GET['pic']);
$img_width=imagesx($img);
$img_height=imagesy($img);

// read the watermark image
$watermark=imagecreatefrompng($watermark);  
$watermark_width=imagesx($watermark);  
$watermark_height=imagesy($watermark);  
$image=imagecreatetruecolor($watermark_width$watermark_height);  
imagealphablending($imagefalse);

// place the watermark
$dest_x=$img_width-$watermark_width-5;
$dest_y=$img_height-$watermark_height-5;
imagecopy($img$watermark$dest_x$dest_y00$watermark_width$watermark_height);
imagesavealpha($imgtrue);

// output the result to browser
header ("Content-type: image/jpg");
imagejpeg($img""90);
?>
Rate this tutorial:                    
Post Comment

Need a specific tutorial? Do not hesitate and submit a request!
Your e-mail: