-
-
- /**
- * 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 -- the new image address after adding watermark
- * Failure -- -1: The original file does not exist, -2: The watermarked image does not exist, -3: The original file image object failed to be created
- * -4: The watermark file image object failed to be created -5: The new image after watermarking failed to be saved
- * @Editor: bbs.it-home.org
- */
- 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; //Failed to create the original file image object
- }
- $waterImgObj = image_create_from_ext($waterImg);
- if (!$waterImgObj) {
- return -4; //Creation of watermark file image object failed
- }
- switch ($positon) {
- //1 top to the left
- case 1: $x=$y=0; break;
- //2 top to the right
- case 2: $x = $srcinfo[0]-$waterinfo[0]; $y = 0; break;
- //3 is centered
- case 3: $x = ($srcinfo[0]-$waterinfo[0] )/2; $y = ($srcinfo[1]-$waterinfo[1])/2; break;
- //4 bottom is on the left
- case 4: $x = 0; $y = $srcinfo[1]-$ waterinfo[1]; break;
- //5 bottom is on the 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;
- }
Copy code
|