PHP Image Watermark-Add one image as a watermark to another picture

Source: Internet
Author: User
Tags save file

The code is as follows:

/** * image watermark (for png/jpg/gif format) * * @param $srcImg Original image * @param $waterImg watermark image * @param $savepath Save Road Diameter * @param $savename Save name * @param $positon watermark Location: 1: Top left, 2: Top right, 3: center, 4: Bottom station left, 5: Bottom Right * @param $alpha transparency: 0 : Fully transparent, 100: completely opaque * * @return Success-Watermark New image address * failed---1: Original file does not exist,-2: Watermark image does not exist,-3: Original file image object failed to build,-4: Watermark file Image object failed to build -5: New image save failed after watermark*/    functionWater_mark ($srcImg,$waterImg,$savepath=NULL,$savename=NULL,$positon=1,$alpha=50){        //determine if a file exists        $srcImgInfo= @getimagesize($srcImg); if(!$srcImgInfo){            return-1; }        $waterImgInfo= @getimagesize($waterImg); if(!$waterImgInfo){            return-1; }        //Creating an Image object        $SRCIMGOBJ= Image_create_from_ext ($srcImg,$srcImgInfo[2]); if(!$SRCIMGOBJ){            return-3;//The original file image object failed to build        }        $WATERIMGOBJ= Image_create_from_ext ($waterImg,$waterImgInfo[2]); if(!$WATERIMGOBJ){            return-4;//The original file image object failed to build        }        //Preparation Information: Save path, save file name        $temp=PathInfo($srcImg); $name=$temp[' basename ']; $path=$temp[' DirName ']; $ext=$temp[' extension ']; $savename=$savename?$savename:$name; $savepath=$savepath?$savepath:$path; $savefile=$savepath.‘ /‘.$savename; //determine where the watermark is generated        Switch($positon){            //1 Top left             Case1:$x=$y=0;  Break; //2 top Right             Case2:$x=$srcImgInfo[0]-$waterImgInfo[0];$y= 0;  Break; //3 Centering             Case3:$x= ($srcImgInfo[0]-$waterImgInfo[0]) /2;$y= ($srcImgInfo[1]-$waterImgInfo[1]) /2;  Break; //4 Bottom Left             Case4:$x= 0;$y=$srcImgInfo[1]-$waterImgInfo[1];  Break; //5 Bottom Right             Case5:$x=$srcImgInfo[0]-$waterImgInfo[0];$y=$srcImgInfo[1]-$waterImgInfo[1];  Break; default:$x=$y=0; }        //Add a watermark pictureImagecopymerge ($SRCIMGOBJ,$WATERIMGOBJ,$x,$y, 0, 0,$srcImgInfo[0]/2,$srcImgInfo[1]/2,$alpha); //Output Picture        Switch($srcImgInfo[2]) {             Case1:Imagegif ($SRCIMGOBJ,$savefile);  Break;  Case2:imagejpeg ($SRCIMGOBJ,$savefile);  Break;  Case3:Imagepng ($SRCIMGOBJ,$savefile);  Break; default:return-5;//Save failed        }        //Destroying image ResourcesImagedestroy ($SRCIMGOBJ); Imagedestroy ($WATERIMGOBJ); return $savefile; }    /** Create Image Object * @param $imgFile picture path * @param $imgExt picture extension * @return $im Image Object **/    functionImage_create_from_ext ($imgFile,$imgExt){        $im=NULL; Switch($imgExt) {             Case1:$im=imagecreatefromgif ($imgFile);  Break;  Case2:$im=imagecreatefromjpeg ($imgFile);  Break;  Case3:$im=imagecreatefrompng ($imgFile);  Break; }        return $im; }

Call Method:

// Call Test    $res null, ' res.jpg ');     Var_dump ($res);

PHP Image Watermark-Add one image as a watermark to another picture

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.