Recommended:

  • phpclasses.org
  • jsclasses.org
  • jsmag.com
  • siteapps.com
  • View our reviews on Hot Scripts
  • JS Tutorial
  • scripts.com
  • securesignup.com




Recent Comments

Powered by Disqus




Back to articles

Add watermark on image using PHP and GD2

Best way to protect your images is to put a watermark with your logo on them, so they've become useless for others. Here is an example of how it can be done. This script takes image path and watermark path, creates blank transparent image resource and copies image and watermark to it. It also shows options of watermark placement in different image corners.

<?php
$path = "path/to/image.jpg";
    
//getting extension type (jpg, png, etc)
$type = explode(".", $path);
$ext = strtolower($type[sizeof($type)-1]);
$ext = (!in_array($ext, array("jpeg","png","gif"))) ? "jpeg" : $ext;

//get image size
$size = getimagesize($path);
$width = $size[0];
$height = $size[1];
    
//get source image
$func = "imagecreatefrom".$ext;
$source = $func($path);

//create output resource
$output = imagecreatetruecolor( $width, $height);

//to preserve PNG transparency
//saving all full alpha channel information
imagesavealpha($output, true);
//setting completely transparent color
$transparent = imagecolorallocatealpha($output, 0, 0, 0, 127);
//filling created image with transparent color
imagefill($output, 0, 0, $transparent);

//copy source to destination
imagecopyresampled( $output, $source,  0, 0, 0, 0, 
                    $width, $height, $width, $height);

//get watermark
$opath = "path/to/watermark.png";
//get watermark size
$size2 = getimagesize($opath);
$owidth = $size2[0];
$oheight = $size2[1];

//get watermark extension
$type = explode(".", $opath);
$ext = strtolower($type[sizeof($type)-1]);
$ext = (!in_array($ext, array("jpeg","png","gif"))) ? "jpeg" : $ext;

//create watermark source
$func = "imagecreatefrom".$ext;
$osource = $func($opath);

$padding = 0; //padding from image border

//let's make watermark 1/5 of image size
$wanted_width = round($new_width/5);
$wanted_height = round($new_height/5);

//resize by height
if(($owidth/$wanted_width) < ($oheight/$wanted_height))
{
    $wanted_width = ($owidth*$wanted_height)/$oheight;
}
else//resize by width
{
    $wanted_height = ($oheight*$wanted_width)/$owidth;
}

//placement - choose one
//bottom left
$dst_x = $padding;
$dst_y = $new_height-$padding-$wanted_height;

//bottom right
$dst_x = $new_width - $padding - $wanted_width;
$dst_y = $new_height-$padding-$wanted_height;

//top left
$dst_x = $padding;
$dst_y = $padding;

//top right
$dst_x = $new_width - $padding - $wanted_width;
$dst_y = $padding;

//copy watermark
imagecopyresampled( $output, $osource,  $dst_x, $dst_y, 0, 0, 
                    $wanted_width, $wanted_height, $owidth, $oheight);

//output image
header('Content-Type: image/'.$ext);
$func = "image".$ext;
$func($output); 

//free resources
ImageDestroy($output);
ImageDestroy($source);
ImageDestroy($osource);

?>

You may also be interested in:

Powered by BlogAlike.com

blog comments powered by Disqus