jump to navigation

How To Watermark Images With PHP December 1, 2011

Posted by Tournas Dimitrios in PHP.

If you want to watermark  photos that are displayed into your webpages , dynamically , without an external graphics editing program (Photoshop , GIMP ) then  PHP comes into the game . PHP is not limited to creating just HTML output . It can also be used to create and manipulate image files in a variety of different image formats , including GIF, PNG, JPEG , WBMP, and XPM . Even more convenient , PHP can output image streams directly to a browser . You will need to have installed the proper PHP-extension  on your server to work with the different image libraries . Some libraries  may also require other libraries  to work , depending on which image formats you want to work with . This article uses functions that depends on the GD library . The format of images you are able to manipulate depend on the version of  the installed GD extension , and any other libraries GD might need to access those image formats . Versions of GD older than gd-1.6 support GIF format images , and do not support PNG , where versions greater than gd-1.6 and less than gd-2.0.28 support PNG , not GIF . GIF support was re-enabled in gd-2.0.28 .  Usually , host providers have the GD-extension  installed on their servers , though , run a phpinfo() statement to verify that the following scripts will run without any problem .

This article will present two practical examples  how to dynamically put a text message on your images . The first code-example will embed an external transparent image (logo-text ) into the image , while the second script will produce the logo-text with the help of functions that are provided to us by the GD-library . The watermark into the following image was created with PHP ‘s GD library .

First example :


$imagesource = $_SERVER['DOCUMENT_ROOT']."/".$_GET['path'];
if (!file_exists($imagesource)) die();
$filetype = strtolower(substr($imagesource,strlen($imagesource)-4,4));
if($filetype == ".gif") $image = @imagecreatefromgif($imagesource);
if($filetype == ".jpg") $image = @imagecreatefromjpeg($imagesource);
if($filetype == ".png") $image = @imagecreatefrompng($imagesource);
if (empty($image)) die();
$watermark = @imagecreatefromgif('watermark.gif');
$imagewidth = imagesx($image);
$imageheight = imagesy($image);
$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);
$startwidth = (($imagewidth - $watermarkwidth)/2);
$startheight = ($imageheight - $watermarkheight);
imagecopy($image, $watermark,  $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);
header("Content-type: image/jpeg");

Store the code into a file (watermark.php ) . The watermark.gif image is the transparentlogo-image that was created with an external application (Photoshop , GIMP ) and should be uploaded into the same directory as watermark.php . Now create a webpage that will embed the image with an html-img-tag .
<img src=”watermark.php?path=images/img.gif” height=”250″ width=”400″>

The src attribute of the image will request the image via a GET-method – URL-encoded string . PHP will parse the path  of the URL-string and will return a image .

Second example : 


$SourceFile = $_GET['path'];
$DestinationFile = '';
$WaterMarkText = 'Copyright TournasDimitrios';
// Define a destination path for storing the watermarket image
$DestinationFile ;
list($width, $height) = getimagesize($SourceFile);
$image_p = imagecreatetruecolor($width, $height);
$filetype = strtolower(substr($SourceFile,strlen($SourceFile)-4,4));
if($filetype == ".gif") $image = @imagecreatefromgif($SourceFile);
if($filetype == ".jpg") $image = @imagecreatefromjpeg($SourceFile);
if($filetype == ".png") $image = @imagecreatefrompng($SourceFile);
   imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
// Customize the position ,size and degree of the watermark text
   $fontColor = imagecolorallocate($image_p, 255, 0, 0);
   $font = 'arial.ttf';
// Customize the position ,size and degree of the watermark text
   $font_size = 12;
   $textposh = $width - 20 ;
   $textposv = $height -40 ;
   $textDegree = 90 ;
   imagettftext($image_p, $font_size, $textDegree, $textposh, $textposv, $fontColor, $font, $WaterMarkText);
   if ($DestinationFile <>'') {
      imagejpeg ($image_p, $DestinationFile, 100);
   } else {
      header('Content-Type: image/jpeg');
      imagejpeg($image_p, null, 100);


This example will generate the watermark text by itself . You need to download the arial.ttf file and upload it on your server, into the same directory as the watermark.php file . Now create a webpage that will embed the image with an html-img-tag .
<img src=”watermark.php?path=img.gif” height=”250″ width=”400″>
If you define a value (path)  to “$DestinationFile  ”  variable ,  the generated image will be stored into the specified path instead of returning a image to the webpage .

Download the source files .


No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s