Add watermark image automatically on website

watermark

 

AbuseĀ in the use of pictures without the permission of the owner is very disturbing, especially when used for commercial purposes, the lack of appreciation for the efforts of the making. It is necessary for the script to automatically add a watermark on your website when you upload the image to the website. In this case i discuss it in the language of the programming language PHP as the most common and most widely used language for web developer.

BASIC LOGIC ANDĀ CODE
What we want to do is add script for watermarking the image before image that you choose save on your server hosting.

//path of destination image file
$image = imagecreatefromjpeg('PATH/TO/DESTINATION/IMAGEFILE');

//path to destination png file
$watermark = imagecreatefrompng('PATH/TO/WATERMARK/PNG');

//basic code to place watermark on jpeg image on php imagecopy()
//bool imagecopy ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h )
//imagesx and imagesy is for get actual size of image / without change size of image
//$posx and $posy is position of watermark x y coordinates.
$posx = 0; //place on top left
$posy = 0; //place on top left
imagecopy($image, $watermark, $posx, $posy, 0, 0, imagesx($watermark), imagesy($watermark));

Above script will save to memory image with watermark, you can manipulate position of watermark with $posx and $posy or you can make some calculation depend on destination image size so the watermark will place in the same spot.

$pos_right = 50;
$pos_bottom = 50;
$posx = imagesx($image) - imagesx($watermark) - $pos_right;
$posy = imagesy($image) - imagesy($watermark) - $pos_bottom;
//it will place on the right bottom of destination jpeg image

 

Complete Example Script

$watermark = imagecreatefrompng('PATH/TO/WATERMARK/PNG'); 
$pos_right = 50; $pos_bottom = 50;
$posx = imagesx($image) - imagesx($watermark) - $pos_right; 
$posy = imagesy($image) - imagesy($watermark) - $pos_bottom; 
imagecopy($image, $watermark, $posx, $posy, 0, 0, imagesx($watermark), imagesy($watermark)); 
imagejpeg($image, 'PATH/TO/DESTINATION/FOLDER/SAVE/IMAGE'); //save image to folder SERVER 
imagedestroy($image); //free memory ?>

Note: JPEG support is only available if PHP was compiled against GD-1.8 or later.

No Comments, Be The First!

Your email address will not be published.