PHP to add a watermark to the image of the specific implementation

Source: Internet
Author: User
Tags watermark images

To add a watermark to the image implementation method is as follows:

1 classMark2 {3      Public function__construct ()4     {5  6     }7  8 /**9 * Add WatermarkTen * @param file $srcImg image to be watermarked One * @param file $waterImg watermark image A * @param integer $position watermark picture placement 1: Top left, 2: Top right, 3: center, 4: Lower left, 5: lower right - * @param integer $alpha watermark picture Transparency 0-100 - * @return Resource Returns the image resource after Watermark the  */ -      Public functionWaterMark ($srcImg,$waterImg,$position=5,$alpha) -     { -         /** + * Obtain the width and height of the original image and Watermark (px) - * @var [type] +          */ A         $srcImgInfo= @getimagesize($srcImg); at         if(!$srcImgInfo)return FALSE; -         $srcW=$srcImgInfo[0]; -         $srcH=$srcImgInfo[1]; -   -         $waterInfo= @getimagesize($waterImg); -         if(!$waterInfo)return FALSE;//watermark picture does not exist or path error in         $waterW=$waterInfo[0];//width -         $waterH=$waterInfo[1];//Height to   +   -         /** the * Generate Image Resources * * @var [type] $          */Panax Notoginseng         $SRCIMGOBJ=$this->imagecreatefromext ($srcImg); -         if(!$SRCIMGOBJ)return FALSE; the   +         $WATERIMGOBJ=$this->imagecreatefromext ($waterImg); A         if(!$WATERIMGOBJ)return FALSE; the         //calculate the location of the watermark +         Switch($position) -         { $         //1 Top left $          Case1: -             $srcX=$srcY= 0; -              Break; the         //2 top Right -          Case2:Wuyi             $srcX=$srcW-$waterW; the             $srcY= 0; -              Break; Wu         //3 Centering -          Case3: About             $srcX= ($srcW-$waterW)/2; $             $srcY= ($srcH-$waterH)/2; -              Break; -         //4 Bottom Left -          Case4: A             $srcX= 0; +             $srcY=$srcH-$waterH; the              Break; -         //5 Bottom Right $          Case5: the             $srcX=$srcW-$waterW; the             $srcY=$srcH-$waterH; the              Break; the         default: -             $srcX=$srcY= 0; in         } the         //merging original and watermark images theImagecopymerge ($SRCIMGOBJ,$WATERIMGOBJ,$srcX,$srcY, 0, 0,$waterW,$waterH,$alpha); AboutImagedestroy ($WATERIMGOBJ);//Close the Watermark image resource the //Returns the merged image resource, which can be exported directly to the browser or saved as a picture file after the external image resource is obtained . the         return $SRCIMGOBJ; the         } +   -     /** the * Create new image resources based on the original format providedBayi * @param file $imgfile the * @return ambigous <null, resource> the     */ -     Private functionImagecreatefromext ($imgfile) -     { the         //get the format of the picture; Format: 1, gif,2, jpg,3, PNG the         $imageType=getimagesize($imgfile) [2]; the         $im=NULL; the         Switch($imageType) { -          Case1: the             $im= Imagecreatefromgif ($imgfile); the              Break; the          Case2:94             $im= Imagecreatefromjpeg ($imgfile); the              Break; the          Case3: the             $im= Imagecreatefrompng ($imgfile);98              Break; About         default: -             $im= Imagecreatefrompng ($imgfile);101         }102         return $im;103     }104}

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.