Home > Backend Development > PHP Tutorial > PHP implements the function of adding watermark to pictures_PHP tutorial

PHP implements the function of adding watermark to pictures_PHP tutorial

WBOY
Release: 2016-07-13 10:37:30
Original
929 people have browsed it

Copy code The code is as follows:

/**
* 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) {
                                                                                                                                         -2; //The watermark image does not exist
}
$srcImgObj = image_create_from_ext($srcImg);
if (!$srcImgObj) {
return -3; //The creation of the original file image object failed
}
$waterImgObj = image_create_from_ext($waterImg);
if (!$waterImgObj) {
return -4; //Failed to create watermark file image object
}
switch ( $positon) {
//1 The top is on the left
case 1: $x=$y=0; break;
//2 The top is on the right
case 2: $x = $srcinfo[0 ]-$waterinfo[0]; $y = 0; break;
//3 centered
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; //Save failed
}
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;
}




http://www.bkjia.com/PHPjc/735250.html

www.bkjia.com

http: //www.bkjia.com/PHPjc/735250.htmlTechArticleCopy the code as follows: ?php /** * Add watermark to pictures (applicable to png/jpg/gif format) * * @author flynetcn * * @param $srcImg original image* @param $waterImg watermark image* @param $sa...
Related labels:
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