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

Put watermark on images using PHP Posted in PHP Tutorials | 62 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 experience with millions of other webmasters visiting our website. Contact us for more information how to become a contributor.

62 Replies to "Put watermark on images using PHP"

Mr. N00B May 31, 2012 at 3:02 pm | Reply

+1

I am a total newbie.
What if i m using it on localhost using xampp?
I can't figure out what should be the path of the image-file. Please help.
Eoin H August 3, 2012 at 12:00 pm

+3

You can probably just use the full path like 'c:/xampp/htdocs/yourfolder/image_file.xxx'

In theory that should do the trick.
Poonam May 19, 2012 at 8:33 am | Reply

+2

i Want to Put database values on Flash using php.How can i do that?? anyone knows??
Nirmal Sharma May 13, 2012 at 8:57 am | Reply

+1

i want to add more info of user in image watermark from MySQL like user name , address .. can i do that and how ?..
zaka September 9, 2012 at 7:34 am

0

yes you can do it just fetch what you want to put on image and pass to the function
Thalia Mundo March 20, 2012 at 3:59 pm | Reply

+10

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.
Claudia David March 19, 2012 at 5:28 pm | Reply

+6

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. This is actually great.
PosamozB March 18, 2012 at 8:15 am | Reply

+2

Hi there Anyone... nice to get accepted in here.
Looking forward to learn and discuss new things here.
Riddhi September 28, 2011 at 4:07 pm | Reply

+2

Hi this is nice but am trying to put text coming from tinymce with fonts of bold italic and dynamically put it into image. M getting the result but bold and italic is not happening can anyone help me out
thesis April 5, 2011 at 9:24 am | Reply

+2

Very nifty item. With subject being so essential online, this is a awesome outline for begin new on that people will use for dissertation service.
tested and works April 4, 2011 at 11:47 pm | Reply

+4

worked on localhost. i have xammp installed and GD library is installed too.

i have copy and paste the arial.ttf from windows/fonts to same location with the code.

if you want to make a shadow simply create this 2 lines and add to function watermarkImage()

$grey= imagecolorallocate($image_p, 128, 128, 128);
imagettftext($image_p, $font_size, 0, 11, 21, $grey, $font, $WaterMarkText);

thanks for the simple and clean code.

Raju Jangid(Rajotia) February 5, 2011 at 11:08 am | Reply

+1

thanks for .........
code working .....


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 ~