PHP Implementation Image Add watermark function

Source: Internet
Author: User

 php to add a watermark for the picture, apply to the png/jpg/gif format, you need friends can refer to the following

Code as follows: <?php/**  * picture watermark (for png/jpg/gif format)  *   * @author flynetcn  *  * @param $srcImg Original picture  * @param $waterImg watermark picture  * @param $savepath save path  * @param $savename save name  * @param watermark Location    * 1: Top left, 2: Top right, 3: center, 4: Bottom left, 5: Bottom right    * @param $alpha Transparency-0: completely transparent, 100: completely opaque  *  &NB sp;* @return Success--a new image after watermark add  *           failed---1: Original file does not exist,-2: Watermark picture does not exist,-3: Original file image object Set up failed   *          -4: Failed to set watermark file image Object-5: New image saved after Watermark failed  */function Img_water_mark ($SRCIMG, $WATERIMG, $savepath =null, $savename =null, $positon =5, $alpha =30) {    $temp = PathInfo ($SRCIMG);     $name = $t emp[' basename '];     $path = $temp [' dirname '];     $exte = $temp [' extension '];     $savename = $savename? $savename: $name;     $savepath = $savepath? $savepath: $path;     $savefile = $savepath. ' /'. $savename; &nbSp   $srcinfo = @getimagesize ($SRCIMG);     if (! $srcinfo) {        return-1;//original file does not exist    }     $waterinfo = @getimagesize ($WATERIMG);     if (! $waterinfo) {        return-2;//watermark image does not exist    }     $SRCIMGOBJ = Image_create_from_ext ($SRCIMG);     if (! $srcImgObj) {        return-3;//original file image Object Setup failed    }     $water Imgobj = Image_create_from_ext ($WATERIMG);     if (! $waterImgObj) {        RETURN-4//Watermark file Image Object Setup failed    }     SWI TCH ($positon) {   //1 top left     Case 1: $x = $y =0, break;    //2 top right of     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:I Magejpeg ($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; }  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.