Sharing a classic and Practical PHP image processing class

Source: Internet
Author: User
This article mainly introduces a classic and Practical PHP image processing class. The PHP image Operation class provided in this article can meet most of the functional requirements of the website, such as film scaling, watermarking, and cropping, for more information about how to scale, add, and crop images, you can use this image processing class to optimize image scaling.

<? Php/** file: The image. class. php class is called Image image processing class, which allows you to scale, add, and crop various types of images. */Class Image {/* path for storing images */private $ path;/*** a path for transferring images when an Image object is used, the default value is the current directory * @ param string $ path. you can specify the path for image processing */function _ construct ($ path = ". /") {$ this-> 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-> getNewSize ($ name, $ width, $ height, $ imgInfo ); /* Get New Image resources */$ newImg = $ this-> kidOfImage ($ srcImg, $ size, $ imgInfo);/* use the private method of this class, save the thumbnail and return the name of the new thumbnail, prefixed with "th _" */return $ this-> createNewImage ($ newImg, $ qz. $ name, $ imgInfo);}/*** add a watermark to the image * @ param string $ g RoundName: specifies the background image to be Watermark. Currently, only images in GIF, JPG, and PNG formats are supported. * @ param string $ waterName: specifies the image to be Watermark. Currently, only images in GIF format are supported, JPG, PNG format * @ param int $ waterPos watermark position. There are 10 states. 0 is a random position. * 1 is the top left, 2 is the top center, and 3 is the top right; * 4: center-left; 5: center-center; 6: center-right; * 7: bottom-left; 8: center-bottom; 9: bottom-right; * @ param string $ qz the name of the watermark image file must be prefixed with 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, the path */$ cur is specified. Path = rtrim ($ this-> path ,"/"). "/"; $ dir = dirname ($ waterName); if ($ dir = ". ") {$ wpath = $ curpath;} else {$ wpath = $ dir. "/"; $ waterName = basename ($ waterName);}/* 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 ;}}

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.