How to Put Water Mark on an Image in PHP

PHP Code for Insert Watermark in an Image

It has become a need to protect your contents especially pictures to be copied and shared with someone else’s name. Watermark is one of the precautionary measures taken to prevent this. Here we are going to give a PHP function that allows you to add Water Mark on an Image in PHP that you upload.
The following function gets some parameters and puts a watermark on the bottom right of the image. Make a watermark image for your brand or blog etc. Give the necessary paths of images and it will put a watermark using PHP GD library. You can use text instead of watermark_image as well.
You can change this function according to your needs. Hope this will help we are trying to share more coding problems with you.

$watermark_image = "http://www.yourdomain.com/images/watermark-image.png";

function watermark_image($path_of_the_image, $image_name, $path_image_after_watermark, $extention_image){
global $watermark_image;
list($owidth,$oheight) = getimagesize($path_of_the_image);
$width = 590;
$height = 500;
$im = imagecreatetruecolor($width, $height);
if(($extention_image == ".jpg") or ($extention_image == ".jpeg")){
$img_src = imagecreatefromjpeg($path_of_the_image);
}elseif(($extention_image == ".gif")){
$img_src = imagecreatefromgif($path_of_the_image);
}elseif(($extention_image == ".png")){
$img_src = imagecreatefrompng($path_of_the_image);
}
imagecopyresampled($im, $img_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight);
$watermark = imagecreatefrompng($watermark_image);
list($w_width, $w_height) = getimagesize($watermark_image);
$pos_x = $width - $w_width;
$pos_y = $height - $w_height;
imagecopy($im, $watermark, $pos_x, $pos_y, 0, 0, $w_width, $w_height);
if(($extention_image == ".jpg")){
imagejpeg($im, $path_image_after_watermark.$image_name, 100);
}elseif(($extention_image == ".gif")){
imagegif($im, $path_image_after_watermark.$image_name, 100);
}elseif(($extention_image == ".png")){
imagepng($im, $path_image_after_watermark.$image_name);
}

imagedestroy($im);
//unlink($path_of_the_image);
return true;
}

Learn PHP here in PHP tutorials and make your own websit

Comments