PHP adds watermarks to png/jpg/gif pictures

Source: Internet
Author: User

 php for the image add watermark, suitable for png/jpg/gif format pictures, you can specify the location of the watermark and the transparency of the watermark, very practical, complete code as follows: <?php/**  * image watermark (for png/jpg/gif format)  *   * @author flynetcn  *  * @param $srcImg original picture  * @param $waterImg watermark picture  * @param $savepat H Save path  * @param $savename save name  * @param $positon 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  *   * @return Success--Watermark New image address  *         failed-- -1: The original file does not exist,-2: Watermark picture does not exist,-3: The original file image object build failed-4: Watermark file Image Object build failed,-5: New picture after watermark Save failed  */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;   &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; &nbsp ;   Switch ($info [2]) {    Case 1: $im =imagecreatefromgif ($imgfile), break,     Case 2: $im =IMAGEC Reatefromjpeg ($imgfile); Break     Case 3: $im =imagecreatefrompng ($Imgfile); Break    }     return $im; }?>

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.