PHP GD Library Watermark Class 7 years later reconstructed support PHP7

Source: Internet
Author: User
 ErrorMsg;} /*** parameter settings * @author sanshi0815* @param string$font font file with a directory text watermark using * @param string$watermark watermark text or image address * @param string$ Srcfile Original Image Address * @param string$dstfile generate new picture address * @return NULL no return value */public function set ($font, $watermark, $srcFile, $ Dstfile) {$this->font = $font; $this->watermark = $watermark; $this->srcfile = $srcFile; $this->dstfile = $ Dstfile;if (Empty ($this->watermark)) {//no watermark $this->watettype = 0;} ElseIf (Is_file ($this->watermark)) {//Picture watermark $this->watettype = 1;} else{//text watermark $this->watettype = 2;}} /*** picture resource gets successfully returned array, failed to return false* @author sanshi0815* @param string$filename picture address * @return arrayr{handle},t{suffix},w{width},h{height}*/ Private Function GetResource ($fileName) {if (!is_file ($fileName)) {$this->errormsg = "{$fileName} does not exist line:". __line_ _;} $temp = Explode ('. ', $fileName); $fileType = Strtolower (end ($temp));//Determine if the suffix meets the requirements if (!in_array ($fileType, $this, Imgtype)) {//file type does not support $this->errormsg = "{$fileName} The picture suffix type does not support line:". __line__;return false;} if ($fileType = = "JPG" | | $fileType== "JPEG") {$im =imagecreatefromjpeg ($fileName);} else{$im =imagecreatefrompng ($fileName);} if (! $im) {//Picture initialization failed $this->errormsg = "{$fileName} Picture initialization resource failed line:". __line__;return false;} Source Image width $width=imagesx ($im);//source Image High $height=imagesy ($im); if (Empty ($width) | | empty ($height)) {//Picture height width get failed $this-> ErrorMsg = "{$fileName} picture height or width gets failed line:". __line__;return false;} Return Array ("r" = $im, "t" = = $fileType, "w" = = $width, "h" = = $height);}  /*** Original picture global variable setting returns the array successfully, failure returns false * @author sanshi0815* @return arrayw{width},h{height}*/private function Initsrcimgwh () {$temp = $this->getresource ($this->srcfile), if (Empty ($temp)) {$this->errormsg = "The image resource does not exist in line:". __line__;return false;} $this->filetype = $temp [' t ']; $this->im= $temp [' R '];return Array ("w" + = $temp [' W '], "h" + = $temp [' h ']);} /*** fixed width, height for picture scaling * @author sanshi0815* @param int$width Generate picture Width * @param int$height generate picture Height * @return bool succeeds to True, failure is false* /public function Resetimgwh ($width, $height) {$temp = $this->initsrcimgwh (); if (empty ($temp)) {$this->errormsg = "Image resource does not exist line:". __line__;return false;} $srcW = $temp [' W ']; $srcH = $temp [' h ']; $detW = Intval ($width); $detH = Intval ($height);//Generate new Image resource $om = $this->getnewimg ($srcW, $srcH, $detW, $detH); $temp = Empty ($om)? False: $this->createimg ($om, $detW, $detH); return $temp;} /*** the image is scaled according to the maximum height * @author sanshi0815* @param int$maxheight generate picture Height * @return bool succeeded to true, failed to false*/public function res Etimgmaxh ($maxHeight) {$maxHeight = Intval ($maxHeight), $temp = $this->initsrcimgwh (), if (Empty ($temp)) {$this errormsg = "Image resource does not exist line:". __line__;return false;} $srcW = $temp [' W ']; $srcH = $temp [' h '];//compute zoom ratio $scale = round ($maxHeight/$srcH, 4); $detW = Round ($srcW * $scale); $detH = Round ($srcH * $scale);//Generate a new image resource $om = $this->getnewimg ($srcW, $srcH, $detW, $detH); $temp = Empty ($om)? False: $this->createimg ($om, $detW, $detH); return $temp;} /*** the image is scaled according to the maximum width * @author sanshi0815* @param int$maxwidth Generate picture Width * @return bool succeeds to True, failure is false*/public function Rese Timgmaxw ($maxWidth) {$temp = $thIs->initsrcimgwh (); if (empty ($temp)) {$this->errormsg = "Image resource does not exist line:". __line__;return false;} $srcW = $temp [' W ']; $srcH = $temp [' h '];//compute zoom ratio $scale = round ($maxWidth/$srcW, 4); $detW = Round ($srcW * $scale); $detH = Round ($srcH * $scale);//Generate a new image resource $om = $this->getnewimg ($srcW, $srcH, $detW, $detH);//$om = $this->im; $temp = Empty ($om)? False: $this->createimg ($om, $detW, $detH); return $temp;} /*** get scaled picture resource handle * @author sanshi0815* @param INT$SRCW Original picture width * @param int$srch original Image Height * @param int$detw Original image width * @param int$de Th original Image Height * @return bool succeeded to true, failed for false*/private function getnewimg ($srcW, $srcH, $detW, $detH) {$om = Imagecreatetruecolor ($detW, $detH);//True color is required for GB libraries if (empty ($om)) {$this->errormsg = "Imagecreatetruecolor function failed line: ". __line__;return false;} Imagecopyresized ($om, $im, 0,0,0,0, $detW, $detH, $srcW, $srcH); $temp = imagecopyresampled ($om, $this->im,0,0,0,0, $detW, $detH, $srcW, $srcH); if (empty ($temp)) {$this->errormsg = "imagecopyresampled function failed Line:". __line__;return false;} return $om;} /*** Get picture Plus text watermark resource * @author sanshi0815* @param resource$im Original resource * @param INT$DETW original image Width * @param int$deth original Image Height * @return Reso Urce succeeded for the watermark after the picture resource, failed for false*/private function Getwatermarktext ($im, $detW, $detH) {if (! $is _file ($this->font)) {$ This->errormsg = "{$this->font} font does not exist line:". __line__;return false;} Rotation angle $angle = 20; $width = $detW/10; $size = $detW/8; $height = $detH;//echo $height; $black = imagecolorallocate ($im, 0, 0, 0); $grey = Imagec Olorallocate ($im, 180, 180, 180);//watermark Generation $formax = 3;for ($i = $formax; $i >=1; $i-) {$height = $height-$detH/($formax +2 );//echo $height. "
"; $temp = Imagettftext ($im, $size, $angle, $width, $height, $grey, $this->font, $this->watermark); if (Empty ($temp) {$this->errormsg = "Imagettftext function failed Line:". __line__;return false;}} return $im;} /*** get picture plus picture watermark resource * @author sanshi0815* @param resource$im Original resource * @param INT$DETW original image Width * @param int$deth original Image Height * @return r Esource succeeded for the watermark after the picture resource, failed for false*/private function getwatermarkpic ($im, $detW, $detH) {$temp = $this->getresource ($ This->watermark); if (empty ($temp)) return false; $wm = $temp [' R ']; $src _x = 0; $src _y = 0; $src _w = $temp [' W ']; $src _h = $te mp[' H ']; $dst _x = $detW; $dst _y = $detH; $height = $dst _y > $src _h? ($DST _y-$src _h)/2:0 $width = $dst _x > $src _w? ($DST _x-$src _w)/2:0; $temp = Imagealphablending ($im, true); if (empty ($temp)) {$this->errormsg = "imagealphablending Function failed Line: ". __line__;return false;} $temp = Imagecopymerge ($im, $WM, $width, $height, 0,0, $src _w, $src _h,70), if (Empty ($temp)) {$this->errormsg = " Imagecopymerge function failed Line: ". __line__;return false;} Return $im;} /*** New Image Generation * @author sanshi0815* @param resource$im Original resource * @param int$detw original picture width * @param int$deth original Image Height * @return bool success is t Rue, failed for false*/private function createimg ($im, $detW, $detH) {//Handling watermark if ($this->watettype==2) {$om = $this Getwatermarktext ($im, $detW, $detH);} ElseIf ($this->watettype==1) {$om = $this->getwatermarkpic ($im, $detW, $detH);} else{$om = $im;} if (empty ($om)) {$this->errormsg = "The picture resource does not exist line:". __line__;return false;} $fileType = $this->filetype;if ($fileType = = "JPG" | | $fileType = = "jpeg") {$temp =imagejpeg ($om, $this->dstfile);} else{$temp =imagepng ($om, $this->dstfile);} return $temp;}} $file =new makeminiature (), $file->set ("./simhei.ttf", "Zhang Lei dedicated", "1_1453362028.png", "s1_1453362028.png"); $file- >RESETIMGMAXW (800);? >

A long time ago, the watermark class write something, see has been written watermark class, too many times, the use of some things are not too handy, and the scene has changed, such as thumbnails, more should be based on the maximum width or height to generate rather than a fixed proportion, because such a picture is the best viewing effect, And it doesn't deform. So the class was re-improved, and for PHP7 support, the old class was actually supported.

The previous class http://blog.csdn.net/sanshi0815/article/details/1604905

The above describes the PHP GD library Watermark Class 7 years after the reconstruction of the support PHP7, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    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.