Method Code:
/** * image watermark (for png/jpg/gif format) * * @author FLYNETCN * * @param $srcImg Original picture * @param $waterWord water Print text * @param $savepath save path * @param $savename save name * @param $positon Watermark Position * 1: Top left, angle-30 degrees; 2: Centered, Angle 30 degrees; 3: Center, angle-30 degrees; 4: Bottom left, angle 30 degrees; * @param $size Font Size * @param $alpha Transparency--0: Fully transparent, 100: completely opaque * * @return Success-Watermark New image address * failed---1: Original File does not exist,-2: Watermark picture does not exist,-3: Original file image Object build failed *-4: Watermark file Image object failed to build-5: New image after Watermark failed to save*/ functionWater_mark ($srcImg,$waterWord,$savepath=NULL,$savename=NULL,$positon=3,$size=74,$color=NULL,$alpha=50){ //determine if a file exists $srcImgInfo= @getimagesize($srcImg); if(!$srcImgInfo){ 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 } //Preparation Information: Save path, save file name $temp=PathInfo($srcImg); $name=$temp[' basename ']; $path=$temp[' DirName ']; $ext=$temp[' extension ']; $color=$color?$color: Imagecolorallocate ($SRCIMGOBJ, 255,250,250); $savename=$savename?$savename:$name; $savepath=$savepath?$savepath:$path; $savefile=$savepath.‘ /‘.$savename; //Create a canvas and fill it with color, otherwise the background is black $imgW= 500; $imgH=$srcImgInfo[1]/($srcImgInfo[0]/500); $IMGOBJ= Imagecreatetruecolor ($imgW,$imgH); $trans= Imagecolorallocate ($IMGOBJ, 255, 255, 255); Imagefill ($IMGOBJ, 0, 0,$trans); //Zoom out the picture to the canvasImagecopyresampled ($IMGOBJ,$SRCIMGOBJ, 0, 0, 0, 0,$imgW,$imgH,$srcImgInfo[0],$srcImgInfo[1]); //determine where the watermark is generated Switch($positon){ //1 top left, angle-20 degrees Case1:$x=$y= 20;$angle=-20; Break; //2 Center, Angle-20 degrees Case2:$x=$imgW/7;$y=$imgH/3;$angle=-20; Break; //3 Center, angle 20 degrees Case3:$x=$imgW/7;$y=$imgH/1.5;$angle= 20; Break; //4 bottom left, angle 20 degrees Case4:$x= 20;$y=$imgH;$angle= 20; Break; default:$x=$imgW/7;$y=$imgH/2;$angle= 20; Break; } //Add a watermark pictureImagettftext ($IMGOBJ,$size,$angle,$x,$y,$color, './fonts/stxinwei. TTF ',$waterWord); //Output Picture Switch($srcImgInfo[2]) { Case1:Imagegif ($IMGOBJ,$savefile); Break; Case2:imagejpeg ($IMGOBJ,$savefile); Break; Case3:Imagepng ($IMGOBJ,$savefile); Break; default:return-5;//Save failed } //Destroying image ResourcesImagedestroy ($IMGOBJ); Imagedestroy ($SRCIMGOBJ); 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; }
Test:
// Call method Test $res null, ' res.jpg ', 3, (); Var_dump ($res);
PHP Image Watermark-Add text as a watermark to the picture