Home > Backend Development > PHP Tutorial > Add watermark to images in php_PHP tutorial

Add watermark to images in php_PHP tutorial

WBOY
Release: 2016-07-13 17:42:41
Original
944 people have browsed it

/**
* Add watermark to image (applicable to png/jpg/gif format)
*
* @author flynetcn
*
* @param $srcImg Original image
* @param $waterImg Watermark image
* @param $savepath Save path
* @param $savename Save name
* @param $positon Watermark position
* 1: Top left, 2: Top right, 3: Center , 4: Bottom left, 5: Bottom right
* @param $alpha Transparency -- 0: completely transparent, 100: completely opaque
*
* @return Success -- new after adding watermark Image address
* Failed - -1: The original file does not exist, -2: The watermark image does not exist, -3: The creation of the original file image object failed
* -4: The creation of the watermark file image object failed -5: The new image after watermarking failed to save
*/
function img_water_mark($srcImg, $waterImg, $savepath=null, $savename=null, $positon=5, $alpha=30)
{
$temp = pathinfo($srcImg);
$name = $temp[basename];
$path = $temp[dirname];
$exte = $temp[extension ];
$savename = $savename ? $savename : $name;
$savepath = $savepath ? $savepath : $path;
$savefile = $savepath ./. $savename;
$ srcinfo = @getimagesize($srcImg);
if (!$srcinfo) {
return -1; //The original file does not exist
}
$waterinfo = @getimagesize($waterImg);
if (!$waterinfo) {
return -2; //The watermark image does not exist
}
$srcImgObj = image_create_from_ext($srcImg);
if (!$srcImgObj) {
return -3; //Original file image object creation failed
}
$waterImgObj = image_create_from_ext($waterImg);
if (!$waterImgObj) {
return -4; //Watermark file Image object creation failed
}
switch ($positon) {
//1 top left
case 1: $x=$y=0; break;
//2 top right
case 2: $x = $srcinfo[0]-$waterinfo[0]; $y = 0; break;
//3 center
case 3: $x = ($srcinfo[0] -$waterinfo[0])/2; $y = ($srcinfo[1]-$waterinfo[1])/2; break;
//4 bottom left
case 4: $x = 0; $y = $srcinfo[1]-$waterinfo[1]; break;
//5 bottom right
case 5: $x = $srcinfo[0]-$waterinfo[0]; $y = $srcinfo[1]-$waterinfo[1]; break;
default: $x=$y=0;
}
imagecopymerge($srcImgObj, $waterImgObj, $x, $y, 0, 0, $waterinfo[0], $waterinfo[1], $alpha);
switch ($srcinfo[2]) {
case 1: imagegif($srcImgObj, $savefile); break;
case 2: imagejpeg($srcImgObj, $savefile); break;
case 3: imagepng($srcImgObj, $savefile); break;
default: return -5; //Failed to save
}
imagedestroy($srcImgObj);
imagedestroy($waterImgObj);
return $savefile;
}

function image_create_from_ext($imgfile)
{
$info = getimagesize($imgfile);
$im = null;
switch ($info[2]) {
case 1 : $im=imagecreatefromgif($imgfile); break;
case 2: $im=imagecreatefromjpeg($imgfile); break;
case 3: $im=imagecreatefrompng($imgfile); break;
}
return $im;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486057.htmlTechArticle?php /** * Add watermark to pictures (applicable to png/jpg/gif format) * * @author flynetcn * * @param $srcImg original image* @param $waterImg watermark image* @param $savepath save path* @para...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template