go top

Put watermark on images using PHP

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);
?>
  • PHP Scripts

    Check our extensive collection of top-notch PHP Scripts that will enhance your website!

    Commercial PHP scripts
  • Free Scripts

    Add great new functionalities to your website with our Free Scripts collection.

    Free scripts

69 Comments to "Put watermark on images using PHP"

  • Mark

    Mark

    January 16, 2009 at 02:31 am

    I tried to run this script but got nothing but a blank screen on my browser. I have GD enabled on the server (i found that using phpinfo()). arial.ttf is in the same folder as that of script, so is the image. Here's my script

    <?

    $SourceFile = '/www/image1.jpg';
    $WaterMarkText = '12345';

    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);
    echo $SourceFile;
    if ($DestinationFile<>'') {
    imagejpeg ($image_p, $DestinationFile, 100);
    } else {
    header('Content-Type: image/jpeg');
    imagejpeg($image_p, null, 100);
    };
    imagedestroy($image);
    imagedestroy($image_p);
    };
    ?>


    Any idea what is wrong? Please help

  • Jim

    Jim

    December 2, 2008 at 01:03 am

    Hi. I have PHP and GD (bundled (2.0.28 compatible) on my server but I get a blank screen with this script. Any ideas?

  • Noel

    Noel

    November 26, 2008 at 14:12 pm

    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

    silva

    November 15, 2008 at 14:32 pm

    hi,
    i have already check this coding.but it will display like this error:"Warning: imagettftext() [function.imagettftext]: Invalid font filename in C:xampphtdocssamplewatermarkwatersample3.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

    silva

    November 15, 2008 at 12:49 pm

    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:xampphtdocssamplewatermarkwatersample3.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

    Loo

    October 6, 2008 at 15:28 pm

    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

    Diaa Bendary

    September 13, 2008 at 16:23 pm

    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

    Khawaja Abid

    August 13, 2008 at 11:14 am

    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

    thanks

  • Matt

    Matt

    August 1, 2008 at 12:57 pm

    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

    Saul

    July 29, 2008 at 20:32 pm

    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

Add your comment

Captcha