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

boo March 14, 2014 at 10:42 am | Reply

0

nice Article ... but its possible to put a background... like this
<span style="background: gray;">Copyright phpJabbers.com</span>

Mr. PhP April 26, 2013 at 1:36 pm | Reply

+12

nice demo. Thanks
Naren December 6, 2012 at 6:32 am | Reply

+8

Wow. Thanks a lot.
sanky December 5, 2012 at 2:59 pm | Reply

+8

Nice code but i have problem to change the text position from image. how to change the position for watertext in Right Bottom.
In watermark function
imagettftext($image_p, $font_size, 0, 10, 20, $black, $font, $WaterMarkText);
can u know me about the value of x and y cordinates to change the bottom right position?
Job Atabor November 4, 2012 at 5:27 pm | Reply

+4

Wow!!! This is interesting.
Rio Nubneka August 30, 2012 at 11:45 am | Reply

+7

Great tutorial. Like it
zaka June 11, 2012 at 11:58 am | Reply

+5

nice demo
inbarasan June 5, 2012 at 11:36 am | Reply

+3

Thank you
aneeeq June 1, 2012 at 8:35 am | Reply

+10

Watermarking an image in PHP is very easy. If you follow the code below, you can do it in 2 minutes.

<?php

function watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile) {
//$SourceFile is source of the image file to be watermarked
//$WaterMarkText is the text of the watermark
//$DestinationFile is the destination location where the watermarked images will be placed

//Delete if destinaton file already exists
@unlink($DestinationFile);

//This is the vertical center of the image
$top = getimagesize($SourceFile);
$top = $top[1]/2;
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);

//Path to the font file on the server. Do not miss to upload the font file
$font = ‘arial.ttf’;

//Font sie
$font_size = 16;

//Give a white shadow
$white = imagecolorallocate($image_p, 255, 255, 255);
imagettftext($image_p, $font_size, 0, 10, $top, $white, $font, $WaterMarkText);

//Print in black color
$black = imagecolorallocate($image_p, 0, 0, 0);
imagettftext($image_p, $font_size, 0, 8, $top-1, $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);

};

?>


<?php

$SourceFile = ‘image.jpg’;//Source image
$DestinationFile = ‘watermarked/image.jpg’;//Destination path
$WaterMarkText = ‘www.phpHelp.co’;//Watermark text

//Call the function to watermark the image
watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile);

//Display watermarked image if desired
if(file_exists($DestinationFile)){
echo “<img src=”watermarked/image.jpg”>”;
echo “<p>The image has been watermarked at ‘”.$DestinationFile.”‘</p>”;
}
?>


Note:
Place all the files in a folder on web server.
Do not forget to upload the font file – arial.ttf.
Once the image is watermarked, it will be placed in the folder named ‘watermarked’.
You might need to give ‘write permission’ or ’777 permission’ to the folder named ‘watermarked’ as the new watermarked image will be written in this folder.
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.


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 ~