« Captcha image verificationMake contact form and send email in PHP »

Put watermark on images using PHP Posted in PHP Tutorials | 61 Comments
If you are a professional or a newbie photographer you will probably want to protect your photos from being stolen and used for free. Using PHP you can create different types of watermarks on all of your images. I am going to show you a way how to dynamically put a text messages on your images. What you need is a JPG image file and a font file used for generate the watermark message. I am going to use arial font which you can also download.

Below you can find a watermarkImage() image function which takes 3 parameters ($SourceFile, $WaterMarkText, $DestinationFile) and creates an watermarked image from the source image specified. The first parameter - $SourceFile - is the full server path to the JPG image that you are going to watermark. The second one - $WaterMarkText - is the text message that you want to use for the watermark. And the last parameter - $DestinationFile - can either be blank or full server path to a new file which will be the source file with watermark text on it. What this function does is to read the source file, then create a new image object (using the imagecopyresampled() function). Then using the Arial.ttf font file and the imagettftext() function it writes the WaterMarkText onto the image. The last IF statement checks if it should save a new watermarked file or should just display it on the screen.

<?php
function watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile) {
list($width, $height) = getimagesize($SourceFile);
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($SourceFile);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
$black = imagecolorallocate($image_p, 0, 0, 0);
$font = 'arial.ttf';
$font_size = 10;
imagettftext($image_p, $font_size, 0, 10, 20, $black, $font, $WaterMarkText);
if ($DestinationFile<>'') {
imagejpeg ($image_p, $DestinationFile, 100);
} else {
header('Content-Type: image/jpeg');
imagejpeg($image_p, null, 100);
};
imagedestroy($image);
imagedestroy($image_p);
};
?>


You need to download the arial.ttf file and upload it on your server. Then create a new PHP file and copy and paste the above function in it. Next 4 lines are used to set the Source file, Watermark text message and Destination file. If you want to just display the watermarked image you need to leave the $DestinationFile variable empty ($DestinationFile=''; ). Also please make sure that for source file and destination file you include the full server path and the image file name. If you want to change the position of the watermark message on your images you can chage that line imagettftext($image_p, $font_size, 0, 10, 20, $black, $font, $WaterMarkText);

<?php
$SourceFile = '/home/user/www/images/image1.jpg';
$DestinationFile = '/home/user/www/images/image1-watermark.jpg';
$WaterMarkText = 'Copyright phpJabbers.com';
watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile);
?>

Do you know PHP / HTML / CSS / JS well?

Write tutorial on a topic you are good in and become a trusted PHP jabber! Share your knowledge with thousands of webmasters and we will reward you for your generosity by giving you bonus points which you can use as a voucher to buy any of our commercial products. Read more about our reward program.

61 Replies to "Put watermark on images using PHP"

Robin April 16, 2010 at 12:53 pm | Reply

0

Can u plz tell me how to reduce the transparency of the text
Monzur April 15, 2010 at 9:55 am | Reply

+1

This is much more good and its saved in a file and it will be center merged
<?
$filename = 'life.jpg';
$watermark = 'copy.gif';
$DestinationFile = 'marked.jpg';
$dest = imagecreatefromjpeg($filename);
$src = imagecreatefromgif($watermark);

list($width, $height, $type, $attr)=getimagesize($filename);

list($markwidth, $markheight, $type1, $attr1)=getimagesize($watermark);

// Copy and merge
$opacity = 30;
imagecopymerge($dest, $src, ($width-$markwidth)>>1, ($height-$markheight)>>1, 0, 0, $markwidth, $markheight, $opacity);

if ($DestinationFile<>'') {
imagejpeg ($dest, $DestinationFile, 100);
}

// Output and free from memory
header('Content-Type: image/jpeg');
imagegif($dest);

imagedestroy($dest);
imagedestroy($src);
?>
Ehsan April 14, 2010 at 9:06 am | Reply

0

Excellent work!! Thanks..How can I change the position of watermark??
Skeptic January 9, 2010 at 5:07 am | Reply

0

Here is a watermark routine:
$filename = $_GET[\'mark\'];
$watermark = \'OTOKOwm.gif\';
$dest = imagecreatefromjpeg($filename);
$src = imagecreatefromgif($watermark);

list($width, $height, $type, $attr)=getimagesize($filename);

// Copy and merge
$markwidth = 225;
$markheight = 32;
$opacity = 32;
imagecopymerge($dest, $src, ($width-$markwidth)>>1, ($height-$markheight)>>1, 0, 0, $markwidth, $markheight, $opacity);

// Output and free from memory
header(\'Content-Type: image/jpeg\');
imagegif($dest);

imagedestroy($dest);
imagedestroy($src);

You can see the result at http://ticketeddy.com/gdlib/pihtml.php
Skeptic January 9, 2010 at 4:35 am | Reply

+2

If I am not mistaken, this \"Put watermark on images using PHP\" is actually putting \"text\" on images, not \"watermark.\" What\'s a watermark? A watermark is a recognizable image or pattern in paper that appears as various shades of lightness/darkness when viewed by transmitted light, caused by thickness variations in the paper. Here is an example: http://www.dwphotoshop.com/photoshop/effects/watermark2.jpg

Usually, watermark is of the same color as the image, except for the raise above the surface look. The routine doesn\'t appear to achieve that.
Jay December 29, 2009 at 10:58 pm | Reply

0

great script and works very good. While it works on most of the images, there are some cases that I see simply a black image (with the water marked text) that overwrites the original images. It happens on some images and works very good on large (e.g: > 600 x 400) images, have you ever encountered this and have a work around?

If you don't mind, please shoot me an email so I can send you the images to reproduce the bug.
Gagan Gupta November 9, 2009 at 9:37 pm | Reply

0

Thanks dude...
Its working...
Marlox August 4, 2009 at 10:39 pm | Reply

+1

excelent....... worked just fine!!!
Milind July 3, 2009 at 8:34 am | Reply

0

Its awsome script run in first shot thanks man....
Ashay June 13, 2009 at 2:44 pm | Reply

0

Hey this is very nice & easy to understand code.
This is the same which I'm looking for gr8 work man


Please be polite and helpful and do not spam or offend others. We promise you will be treated the same way.

Log in your free account or if you still haven't joined our Webmaster Community Reward Program, you can create your free account now.

Posting tip:
if you use code in your comments please put it in these tags [php], [sql], [css], [js]
PHP code example: [php] echo date("Y-m-d"); [/php]

Thank you,
~ PHPJabbers team ~