Phpgd Library watermark class is reconstructed after 7 years support php7

Source: Internet
Author: User
: This article describes how to reconstruct and support php7 for the watermark class of the phpgd Library seven years later. if you are interested in the PHP Tutorial, refer to it.
 ErrorMsg ;} /*** parameter settings ** @ author sanshi0815 * @ param string $ Use the directory text watermark in the font file * @ param string $ watermark text or image address * @ param string $ srcFile original image address * @ param string $ dstFile generate new image 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) {// watermarks $ this-> watetType = 0 ;} Elseif (is_file ($ this-> watermark) {// image watermark $ this-> watetType = 1 ;} else {// text watermark $ this-> watetType = 2 ;}}/*** the image resource is obtained successfully and an array is returned, false * @ author sanshi0815 * @ param string $ fileName Image 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 whether the suffix meets the requirements if (! In_array ($ fileType, $ this-> imgType) {// the file type does not support $ this-> errorMsg = "{$ fileName}. The image suffix type does not support line :". _ LINE __; return false;} if ($ fileType = "jpg" | $ fileType = "jpeg") {$ im = imageCreateFromjpeg ($ fileName );} else {$ im = imagecreatefrompng ($ fileName);} if (! $ Im) {// Image initialization failed $ this-> errorMsg = "{$ fileName} image initialization resource failed line :". _ LINE __; return false ;}// source image width $ width = imagesx ($ im); // source image height $ height = imagesy ($ im ); if (empty ($ width) | empty ($ height )) {// Image height/width acquisition failed $ this-> errorMsg = "{$ fileName} image height/width acquisition failed line :". _ LINE __; return false;} return array ("r" => $ im, "t" => $ fileType, "w" => $ width, "h" => $ height);}/*** An array is returned if the global variable of the original image is set successfully. if the global variable of the original image is set, the returned value is 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. line :". _ LINE __; return false ;}$ this-> fileType = $ temp ['t']; $ this-> im = $ temp ['R']; return array ("w" => $ temp ['w'], "h" => $ temp ['H']);}/*** fixed width, zooming images in height * @ author sanshi0815 * @ param int $ width generate image width * @ param int $ height generate image height * @ return bool success = true, failure: false */public function resetImgWH ($ wi Dth, $ height) {$ temp = $ this-> initSrcImgWH (); if (empty ($ temp) {$ this-> errorMsg = "The image resource does not exist line :". _ LINE __; return false ;}$ srcW = $ temp ['w']; $ srcH = $ temp ['H']; $ detW = intval ($ width ); $ detH = intval ($ height); // generate a new image resource $ om = $ this-> getNewImg ($ srcW, $ srcH, $ detW, $ detH ); $ temp = empty ($ om )? False: $ this-> createImg ($ om, $ detW, $ detH); return $ temp ;} /*** perform image proportional scaling based on the maximum height * @ author sanshi0815 * @ param int $ maxHeight to generate image height * @ return bool success = true, false */public function resetImgMaxH ($ maxHeight) {$ maxHeight = intval ($ maxHeight); $ temp = $ this-> initSrcImgWH (); if (empty ($ temp) {$ this-> errorMsg = "The image resource does not exist line :". _ LINE __; return false ;}$ srcW = $ temp ['w']; $ srcH = $ temp ['H']; // calculate the scale ratio $ scale = round ($ maxHe Ight/$ 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 ;} /*** scale the image proportionally according to the maximum width * @ author sanshi0815 * @ param int $ maxWidth to generate the image width * @ return bool success to true, failure: false */public function resetImgMaxW ($ maxWidth) {$ temp = $ this-> initSrcImgWH (); if (empty ($ temp )) {$ this-> errorMsg = "The image resource does not exist. line :". _ LINE __; return false ;}$ srcW = $ temp ['w']; $ srcH = $ temp ['H']; // calculate the scale ratio $ scale = round ($ maxWidth/$ srcW, 4); $ detW = round ($ srcW * $ Scale); $ detH = round ($ srcH * $ scale); // Generate New Image Resources $ om = $ this-> getNewImg ($ srcW, $ srcH, $ detW, $ detH); // $ om = $ this-> im; $ temp = empty ($ om )? False: $ this-> createImg ($ om, $ detW, $ detH); return $ temp ;} /*** get the scaled image resource handle * @ author sanshi0815 * @ param int $ srcW original image width * @ param int $ srcH original image height * @ param int $ detW original image height image width * @ param int $ detH original image height * @ return bool success is true, false */private function getNewImg ($ srcW, $ srcH, $ detW, $ detH) {$ om = imagecreatetruecolor ($ detW, $ detH ); // if (empty ($ om) {$ this-> errorMsg = "imagecreatetruecolor function failed line :". _ LINE __; retu Rn false;} // ImageCopyResized ($ om, $ im, 0, 0, 0, $ detW, $ detH, $ srcW, $ srcH); $ temp = imagecopyresampled ($ om, $ this-> im, 0, 0, 0, $ detW, $ detH, $ srcW, $ srcH); if (empty ($ temp )) {$ this-> errorMsg = "imagecopyresampled function failed line :". _ LINE __; return false;} return $ om ;} /*** obtain image and text watermark Resources * @ author sanshi0815 * @ param resource $ im original resource * @ param int $ detW original image width * @ param int $ detH original image height * @ return resource: the image resource after the watermark is successfully added, failure: false */priva Te function getWatermarkText ($ im, $ detW, $ detH) {if (! $ Is_file ($ this-> font) {$ this-> errorMsg = "{$ this-> font} the 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); $ gray = imagecolorallocate ($ im, 180,180,180 ); // watermark generation times $ formax = 3; for ($ I = $ formax; $ I >=1; $ I --) {$ height = $ height-$ detH/($ formax + 2); // echo $ height."
"; $ Temp = imagettftext ($ im, $ size, $ angle, $ width, $ height, $ gray, $ this-> font, $ this-> watermark ); if (empty ($ temp) {$ this-> errorMsg = "imagettftext function failed line :". _ LINE __; return false ;}} return $ im ;} /*** obtain the resource after adding an image watermark to the image * @ author sanshi0815 * @ param resource $ im original resource * @ param int $ detW original image width * @ param int $ detH original image height * @ return resource: the image resource after the watermark is successfully added, failure: false */private function getWatermarkPic ($ im, $ detW, $ detH) {$ temp = $ t His-> getResource ($ this-> watermark); if (empty ($ temp) return false; $ wm = $ temp ['R']; $ src_x = 0; $ src_y = 0; $ src_w = $ temp ['w']; $ src_h = $ temp ['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, $ 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 image width * @ pa Ram int $ detH original image height * @ return bool success is true, failure is false */private function createImg ($ im, $ detW, $ detH) {// process the 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 image 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 "," 20171453362028.png "," s1_1453362028.png "); $ file-> resetImgMaxW (800);?>

A long time ago, I wrote something about the watermark class. I saw that I used to write the watermark class for a long time. it was not very easy to use and the scenario was changed, such as thumbnails, it is more important to generate the image based on the maximum width or height instead of a fixed proportion, because the image is the best viewing effect and will not be deformed. So I improved this class again. The support for PHP 7 is actually supported by the old class.

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

The above introduces php gd Library watermarking class 7 years after reconstruction support php7, including content, hope to be helpful to friends interested in PHP tutorials.

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.