« 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"

Matt August 1, 2008 at 12:57 pm | Reply

0

I know this is a really old thread but does anyone know how i can add a horizontal coloured bar/background behind the overlayed text because some of my images are dark and some light so i need to set a coloured background for he text? thanks
--------------
Veselin: you should create a new image for your watermark and then using imagecopymerge() to copy that image over your own images.

http://www.php.net/manual/en/function.imagecopymerge.php
Saul July 29, 2008 at 8:32 pm | Reply

0

I'm trying to download the arial.ttf file that you reference on your site but it is blocked with a password... What do I have to do to download the font?
---------
Veselin: sorry, you can download it now
Sam George June 9, 2008 at 2:30 pm | Reply

0

Fantastic, Congratulations! Thank you so much!
Mr. Watermark May 26, 2008 at 4:15 am | Reply

0

I just implemented the version of this that uses transparency. It works like a charm! Thank you so much!
Paul April 12, 2008 at 9:45 pm | Reply

+1

Great script and easy to follow. However, I've got a problem.

I've got a php page which creates a watermarked image. The destination filename is blank so it displays on screen. When I use Firefox, the watermarked image is a jpg (about 400KB) but when I use IE it is a bmp (about 2.5MB).

The original images are all jpg. Any ideas what I've done wrong?
-------------------
Veselin: can you post your code or send it to our email address if it is too long
jit February 7, 2008 at 7:46 am | Reply

0

Ash posted on 2008-02-06 14:49:40

thank you Ash for semi-transparent ;)
Ash February 6, 2008 at 2:49 pm | Reply

0

Hey, sorry for the double post, but i worked it out.
replace $black = imagecolorallocate($image_p, 0, 0, 0);
with $black = imagecolorallocatealpha($image_p, 255, 255, 255, 50);

hope it helps somebody else
Ash February 6, 2008 at 2:15 pm | Reply

0

Hi, great tutorial! I have been looking at the script and I was wondering if it would be possible to make the watermark text semi-transparent?

Thank you for any help.
Amit October 29, 2007 at 3:20 pm | Reply

0

This is a cool dude.....
pawan July 26, 2007 at 12:41 pm | Reply

0

The above mentioned function is only to apply watermark text on image..........
But there is no function to apply watermark image on image...
Please provide me that script


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 ~