This image processing class can scale, add watermarks, and crop images. It supports processing of multiple image types and optimization During Scaling. Www.lai18.comcontent410393.html none? Php ** file: image. class. php class Name: Image image processing class, which allows you to scale and add Image watermarks to various types of images.
This image processing class can scale, add watermarks, and crop images. It supports processing of multiple image types and optimization During Scaling. Http://www.lai18.com/content/369393.html none? Php/** file: image. class. php class Name: Image image processing class, which allows you to scale and add Image watermarks to various types of images.
This image processing class can scale, add watermarks, and crop images. It supports processing of multiple image types and optimization During Scaling.
Http://www.lai18.com/content/369393.html <无>
Path = rtrim ($ path ,"/"). "/";} /*** scale the specified image * @ param string $ name is the name of the image to be processed * @ param int $ width: scaled width * @ param int $ height * @ param string $ qz is the prefix of the new image * @ return mixed is the scaled image name, false is returned for failure; */function thumb ($ name, $ width, $ height, $ qz = "th _") {/* obtain the image width, height, and type information */$ imgInfo = $ this-> getInfo ($ name ); /* obtain the resource of the background image */$ srcImg = $ this-> getImg ($ name, $ imgInfo ); /* get the new image size */$ size = $ this-> getN EwSize ($ name, $ width, $ height, $ imgInfo);/* Get new image resources */$ newImg = $ this-> kidOfImage ($ srcImg, $ size, $ imgInfo);/* saves the thumbnail and returns the name of the new thumbnail through the private method of this class, prefix with "th _" */return $ this-> createNewImage ($ newImg, $ qz. $ name, $ imgInfo);}/*** Add a watermark to the image * @ param string $ groundName background image, that is, the image to be watermarked. Currently, only GIF and JPG images are supported, PNG format * @ param string $ waterName: watermark image. Currently, only images in GIF, JPG, and PNG formats are supported. * @ param int $ waterPos: watermark position. There are 10 statuses, 0 is a random position; * 1 is the top left, and 2 is Center top, 3 is center top right, * 4 is center, 5 is center, 6 is center right, * 7 is center bottom, 8 is center bottom, 9. The bottom is right. * @ param string $ qz adds the prefix to the name of the watermark image file before the original file name * @ return mixed indicates the name of the image after the watermark is generated, false */function waterMark ($ groundName, $ waterName, $ waterPos = 0, $ qz = "wa _") is returned for failure {/* The current path is used to obtain the waterMark image, specify the path */$ curpath = rtrim ($ this-> path ,"/"). "/"; $ dir = dirname ($ waterName); if ($ dir = ". ") {$ wpath = $ curpath;} else {$ wpath = $ dir. "/"; $ waterName = basename ($ waterNam E);}/* the watermark image and background image must both exist */if (file_exists ($ curpath. $ groundName) & file_exists ($ wpath. $ waterName) {$ groundInfo = $ this-> getInfo ($ groundName); // get the background information $ waterInfo = $ this-> getInfo ($ waterName, $ dir ); // obtain the watermark image information/* if the background is smaller than the watermark image, it will be completely covered by the watermark */if (! $ Pos = $ this-> position ($ groundInfo, $ waterInfo, $ waterPos) {echo 'watermark should not be smaller than the background image! '; Return false ;}$ groundImg = $ this-> getImg ($ groundName, $ groundInfo); // obtain background image resources $ waterImg = $ this-> getImg ($ waterName, $ waterInfo, $ dir ); // obtain watermark image resources/* call the private method to copy the watermark image to the background image at the specified position */$ groundImg = $ this-> copyImage ($ groundImg, $ waterImg, $ pos, $ waterInfo);/* saves the attached image and returns the name of the new image through the private method of this class, the default prefix is "wa _" */return $ this-> createNewImage ($ groundImg, $ qz. $ groundName, $ groundInfo);} else {echo 'image or watermark image does not exist! '; Return false ;}} /*** crop the image in the specified area in a large background image * @ param string $ name: the background image to be cut * @ param int $ x cut the position on the left of the image * @ param int $ y cut the position at the top of the image * @ param int $ width the width of the cropped image * @ param int $ height the height of the cropped image * @ param string $ qz of the new image name prefix * @ return The Name Of The cropped image after mixed, false is returned for failure. */function cut ($ name, $ x, $ y, $ width, $ height, $ qz = "cu _") {$ imgInfo = $ this-> getInfo ($ name ); // obtain image information/* the cropping position cannot exceed the range of the background image */if ($ x + $ width)> $ ImgInfo ['width']) | ($ y + $ height)> $ imgInfo ['height']) {echo "the cropped position is out of the range of the background image! "; Return false;} $ back = $ this-> getImg ($ name, $ imgInfo ); // obtain image resources/* create a resource that can save the cropped image */$ cutimg = imagecreatetruecolor ($ width, $ height);/* use imagecopyresampled () the function crops the image */imagecopyresampled ($ cutimg, $ back, 0, 0, $ x, $ y, $ width, $ height, $ width, $ height ); imagedestroy ($ back);/* Save the cut image and return the name of the new image through the private method of this class, the default prefix is "cu _" */return $ this-> createNewImage ($ cutimg, $ qz. $ name, $ imgInfo);}/* Private method used internally, To determine the position of the watermark image */private function position ($ groundInfo, $ waterInfo, $ waterPos) {/* the length or width of the image to be watermark is smaller than that of the watermark, the watermark cannot be generated */if ($ groundInfo ["width"] <$ waterInfo ["width"]) | ($ groundInfo ["height"] <$ waterInfo ["height"]) {return false;} switch ($ waterPos) {case 1: // 1 is the top position, with $ posX = 0; $ posY = 0; break; case 2: // 2 center the top $ posX = ($ groundInfo ["width"]-$ waterInfo ["width"])/2; $ posY = 0; break; case 3: // 3: top-Right $ PosX = $ groundInfo ["width"]-$ waterInfo ["width"]; $ posY = 0; break; case 4: // 4 is left in the middle. $ posX = 0; $ posY = ($ groundInfo ["height"]-$ waterInfo ["height"])/2; break; case 5: // 5 is centered in the center $ posX = ($ groundInfo ["width"]-$ waterInfo ["width"])/2; $ posY = ($ groundInfo ["height"]-$ waterInfo ["height"])/2; break; case 6: // 6 is the center-right $ posX = $ groundInfo ["width"]-$ waterInfo ["width"]; $ posY = ($ groundInfo ["h Eight "]-$ waterInfo [" height "])/2; break; case 7: // 7 is left at the bottom $ posX = 0; $ posY = $ groundInfo ["height"]-$ waterInfo ["height"]; break; case 8: // 8 is centered at the bottom $ posX = ($ groundInfo ["width"]-$ waterInfo ["width"])/2; $ posY = $ groundInfo ["height"]-$ waterInfo ["height"]; break; case 9: // 9 is the bottom right $ posX = $ groundInfo ["width"]-$ waterInfo ["width"]; $ posY = $ groundInfo ["height"]-$ waterInfo ["height"]; break; c Ase 0: default: // random $ posX = rand (0, ($ groundInfo ["width"]-$ waterInfo ["width"]); $ posY = rand (0, ($ groundInfo ["height"]-$ waterInfo ["height"]); break;} return array ("posX" => $ posX, "posY" => $ posY);}/* private method used internally to obtain image attributes (width, height, and type) */private function getInfo ($ name, $ path = ". ") {$ spath = $ path = ". "? Rtrim ($ this-> path ,"/"). "/": $ path. '/'; $ data = getimagesize ($ spath. $ name); $ imgInfo ["width"] = $ data [0]; $ imgInfo ["height"] = $ data [1]; $ imgInfo ["type"] = $ data [2]; return $ imgInfo;}/* Private method used internally to create images in different formats (jpg, gif, png) Resources */private function getImg ($ name, $ imgInfo, $ path = '. ') {$ spath = $ path = ". "? Rtrim ($ this-> path ,"/"). "/": $ path. '/'; $ srcPic = $ spath. $ name; switch ($ imgInfo ["type"]) {case 1: // gif $ img = imagecreatefromgif ($ srcPic); break; case 2: // jpg $ img = imagecreatefromjpeg ($ srcPic); break; case 3: // png $ img = imagecreatefrompng ($ srcPic); break; default: return false; break ;} return $ img;}/* Private method used internally. return the width and height of the scaled image, if the source image is smaller than the scaled image size, it remains unchanged */private function getNewSize ($ name, $ width, $ height, $ imgInfo) {$ size ["width"] = $ imgInfo ["width"]; // The width of the original image $ size ["height"] = $ imgInfo ["height"]; // if ($ width <$ imgInfo ["width"]) {$ size ["width"] = $ width; // reset the width if the scaled width is smaller than the original image size.} if ($ height <$ imgInfo ["height"]) {$ size ["height"] = $ height; // if the scaled height is smaller than the original image, You can reset the height}/* proportional Scaling Algorithm */if ($ imgInfo ["width"] * $ size ["width"]> $ imgInfo ["height"] * $ size ["height"]) {$ size ["height"] = round ($ imgInfo ["height"] * $ size ["width"]/$ imgInfo ["width"]);} else {$ size ["width"] = round ($ imgInfo ["width"] * $ size ["height"]/$ imgInfo ["height"]);} return $ size;}/* private method used internally to save the image and retain the original image format */private function createNewImage ($ newImg, $ newName, $ imgInfo) {$ this-> path = rtrim ($ this-> path ,"/"). "/"; switch ($ imgInfo ["type"]) {case 1: // gif $ result = imageGIF ($ newImg, $ this-> path. $ newName); break; case 2: // jpg $ result = imageJPEG ($ newImg, $ this-> path. $ newName); break; case 3: // png $ result = imagePng ($ newImg, $ this-> path. $ newName); break;} imagedestroy ($ newImg); return $ newName;}/* private method used internally to copy an image when adding a watermark */private function copyImage ($ groundImg, $ waterImg, $ pos, $ waterInfo) {imagecopy ($ groundImg, $ waterImg, $ pos ["posX"], $ pos ["posY"], 0, 0, $ waterInfo ["width"], $ waterInfo ["height"]); imagedestroy ($ waterImg); return $ groundImg;}/* Private method used internally, process images with transparency as is */private function kidOfImage ($ srcImg, $ size, $ imgInfo) {$ newImg = imagecreatetruecolor ($ size ["width"], $ size ["height"]); $ otsc = imagecolortransparent ($ srcImg); if ($ otsc >=0 & $ otsc <imagecolorstotal ($ srcImg )) {$ transparentcolor = imagecolorsforindex ($ srcImg, $ otsc); $ newtransparentcolor = imagecolorallocate ($ newImg, $ transparentcolor ['red'], $ transparentcolor ['green'], $ transparentcolor ['blue']); imagefill ($ newImg, 0, 0, $ newtransparentcolor); imagecolortransparent ($ newImg, $ newtransparentcolor);} imagecopyresized ($ newImg, $ srcImg, 0, 0, 0, 0, $ size ["width"], $ size ["height"], $ imgInfo ["width"], $ imgInfo ["height"]); imagedestroy ($ srcImg); return $ newImg ;}}