Create Watermark in PHP

Introduction

If you are a professional or a photographer you will probably want to protect your photos from being stolen and used for free. So using PHP you can create a different type of watermark on all of your images. So this article describes how to place an image or text as a watermark on another image. In other words, the script will watermark an image using another image. A simple approach is to put your company logo or copyright notice on the image, thus protecting it from theft.

Water mark image

watermarkcsharp.gif
Main image

Photo.gif 

Function use to set watermark

The following functions help for using a watermark in PHP:

  • imagecreatefromgif()
  • magecreatefromjpeg()
  • getimagesize()
  • imagecopymerge()
  • header()
  • imagejpeg()
  • imagedestroy()

Example

<?php 

$main_img            = "Photo.jpg"; // main big photo / picture

$watermark_img       = "watermarkcsharp.gif"; // use GIF or PNG, JPEG has no tranparency support//

$padding             = 80; // distance to border in pixels for watermark image

$opacity             = 100; // image opacity for transparent watermark

$watermark    = imagecreatefromgif($watermark_img); // create watermark

$image               = imagecreatefromjpeg($main_img); // create main graphic

if(!$image || !$watermark) die("Error: main image or watermark could not be loaded!");

$watermark_size      = getimagesize($watermark_img);

$watermark_width     = $watermark_size[0]; 

$watermark_height    = $watermark_size[1]; 

$image_size   = getimagesize($main_img); 

$dest_x              = $image_size[0] - $watermark_width - $padding; 

$dest_y              = $image_size[1] - $watermark_height - $padding;

// copy watermark on main image

imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $opacity); 

// print image to screen

header("content-type: image/jpeg");  

imagejpeg($image); 

imagedestroy($image); 

imagedestroy($watermark); 

?>

Output

Water Mark image with 40% opacity

watermark-in-php.gif

Water Mark image with 10% opacity

watermark10-in-php.gif

Water Mark image with 100% opacity

watermark100-in-php.gif