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

Put watermark on images using PHP Posted in PHP Tutorials | 67 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.

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);

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);

$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 experience with millions of other webmasters visiting our website. Contact us for more information how to become a contributor.

67 Replies to "Put watermark on images using PHP"

Noel November 26, 2008 at 2:12 pm | Reply


Do I have to have something installed on my server as when I put that code in, nothing happens, it gives me a blank screen
Veselin: yes you need PHP with GD support and the code correctly placed.
silva November 15, 2008 at 2:32 pm | Reply


i have already check this coding.but it will display like this error:"Warning: imagettftext() [function.imagettftext]: Invalid font filename in C:\xampp\htdocs\sample\watermark\water\sample3.php on line 20". but all files are in one folder.arial.ttf also in the same folder. so, pls help me to solve this.thanks
silva November 15, 2008 at 12:49 pm | Reply


This is great script . I'm working in local host now , where i should upload the font file .i already download the arial.ttf and save in script folder. when i run this script, i got warning message like this: "
Warning: imagettftext() [function.imagettftext]: Invalid font filename in C:\xampp\htdocs\sample\watermark\water\sample3.php on line 20 ". but destinatiion place have a image without watermark text. so, pls, help me.how to display the watermark text on the destination place.ok bye
Veselin: the file should be in the same folder where your PHP files are
Loo October 6, 2008 at 3:28 pm | Reply


This is brilliant, exactly what I was looking for. I did mod it slightly as I wanted the watermark through the middle of the image (it's easy to cut the edge off with the watermark on) so I used the following:

list($width, $height) = getimagesize($SourceFile);
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($SourceFile);
$left = ($width / 7) ;
$top = ($height / 3) ;
$bot = ($top * 2) ;
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
$black = imagecolorallocatealpha($image_p, 255, 255, 255, 50);
$font = 'arial.ttf';
$font_size = 10;
imagettftext($image_p, $font_size, 0, $left, $bot, $black, $font, $WaterMarkText);

I had a website address to watermark, hence the margin from the left being small. Thanks to everyone who has contributed here, you saved me a lot of headaches.
Diaa Bendary September 13, 2008 at 4:23 pm | Reply


This is great script . I'm working in local host now , where i should upload the font file .
If you can make this script with watermark photo instead of font
Best regards
Veselin: it should be in the same folder where your PHP script is
Khawaja Abid August 13, 2008 at 11:14 am | Reply


I utilized the above code it was really great and help me alot i really thank full to you

and pawan asked for image on image for that please use this may its help

$dest = imagecreatefromjpeg('2.jpg');
$src = imagecreatefromgif('php2.gif');

imagecopymerge($dest, $src, 0, 0, 0, 0, 800, 600, 75);

header('Content-Type: image/gif');

hope this help you all

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


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.

Saul July 29, 2008 at 8:32 pm | Reply


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


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


I just implemented the version of this that uses transparency. It works like a charm! Thank you so much!

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 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 ~