Sharing php multi-function image processing,
The examples in this article share with you the multi-function php image processing class for your reference. The specific content is as follows:
<? Php/*** Image. class. php Image processing class * @ author Administrator **/class Image {private $ path = ''; /*** constructor * @ param unknown $ path */function _ construct ($ path = '') {if (! Empty ($ path) {$ this-> path = $ path ;}} /** + ratio * proportional scaling function + ratio * @ param unknown $ name: name of the image to be processed * @ param unknown $ width: scaled width * @ param unknown $ height after scaling height * @ param string $ thumb_prixs the scaled prefix name * @ return mixed $ the scaled file name returned by newname */function thumb ($ name, $ width, $ height, $ thumb_prixs = 'th _ ') {// get image information $ Info = $ this-> I MageInfo ($ name); // The image width, height, type, // obtain image resources. You can create resources for various types of images, such as jpg, gif, png $ imagres = $ this-> Img_resouce ($ name, $ Info); // obtain the size after calculating the image proportion, $ size = $ this-> getNewSize ($ name, $ width, $ height, $ Info); // obtain new image resources and process transparent backgrounds $ newimg = $ this-> getImage ($ imagres, $ size, $ Info ); // save as a new image and return the scaled image name $ newname = $ this-> SaveNewImage ($ newimg, $ thumb_prixs. $ name, $ Info); return $ newname;}/** + ----------------------------------------- ---------------------------- * Watermark marking Function + watermark * @ param unknown $ backname background file name * @ param unknown $ watername watermark file name * @ param number $ waterpos watermark position * @ param string $ wa_prixs watermark prefix name * @ return boolean */function waterMark ($ backname, $ watername, $ waterpos = 0, $ wa_prixs = 'wa _ ') {if (file_exists ($ this-> path. $ backname) & file_exists ($ this-> path. $ Watername) {$ backinfo = $ this-> ImageInfo ($ backname); $ waterinfo = $ this-> ImageInfo ($ watername); if (! $ Pos = $ this-> getPos ($ backinfo, $ waterinfo, $ waterpos) {echo "watermark image should not be smaller than the background image"; return false ;} $ backimg = $ this-> Img_resouce ($ backname, $ backinfo); $ waterimg = $ this-> Img_resouce ($ watername, $ waterinfo); var_dump ($ backimg ); exit (); $ backimg = $ this-> CopyImage ($ backimg, $ waterimg, $ pos, $ waterinfo); $ this-> SaveNewImage ($ backimg, $ wa_prixs. $ backname, $ backinfo);} else {echo "image or watermark does not exist"; return false ;}/ ** + Functions * for obtaining image information + functions * @ param unknown $ name * @ return unknown */private function ImageInfo ($ img) {$ imageInfo = getimagesize ($ img ); if ($ imageInfo! = False) {$ imageType = strtolower (substr (image_type_to_extension ($ imageInfo [2]), 1); $ imageSize = filesize ($ img ); $ Info = array ("width" => $ imageInfo [0], "height" => $ imageInfo [1], "type" => $ imageType, "size" => $ imageSize, "mime" => $ imageInfo ['mime ']); return $ Info ;}else {return false ;}} /** + -------------------------------------------------------- * Create the original image format Function + ---------------------------------- ---------------------- * @ Param unknown $ name * @ param unknown $ imaginfo * @ return boolean | resource */private function Img_resouce ($ name, $ imageinfo) {$ iamgeres = $ this-> path. $ name; // var_dump ($ iamgeres); exit (); switch ($ imageinfo ['type']) {case 'gif': $ img = imagecreatefromgif ($ name ); break; case 'jpg ': $ img = imagecreatefromjpeg ($ name); break; case 'png': $ img = imagecreatefrompng ($ name); break;} retu Rn $ img ;} /** + proportional * obtain the proportional scaling function + parameter * @ param unknown $ name * @ param unknown $ width * @ param unknown $ height * @ param unknown $ imaginfo * @ return ambigous <unknown, number> */private function getNewSize ($ name, $ width, $ height, $ imaginfo) {$ size ['width'] = $ imaginfo ['width']; $ size ['height'] = $ imaginfo ['height ']; If ($ width <$ imaginfo ['width']) {$ size ['width'] = $ width ;} if ($ height <$ imaginfo ['height']) {$ size ['height'] = $ height ;} // proportional scaling algorithm such as image if ($ imaginfo ['width'] * $ size ['width']> $ imaginfo ['height'] * $ size ['height']) {$ size ['height'] = round ($ imaginfo ['height'] * $ size ['width']/$ imaginfo ['width']);} else {$ size ['width'] = round ($ imaginfo ['width'] * $ size ['height']/$ imaginfo ['height']);} return $ size;} privat E function getImage ($ imageres, $ size, $ imageinfo) {// create a true color image $ newimg = imagecreatetruecolor ($ size ['width'], $ size ['height']); // defines a color as transparent $ otsc = imagecolortransparent ($ imageres ); // obtain the color Quantity of the image palette if ($ otsc> = 0 & $ otsc <= imagecolorstotal ($ imageres )) {// obtain the color of an index $ stran = imagecolorsforindex ($ imageres, $ otsc); // assign a color to the image $ newt = imagecolorallocate ($ imageres, $ stran ['red'], $ stran ['green'], $ stran ['blue']); // Imagefill ($ newimg, 0, 0, $ newt); // defines the transparent imagecolortransparent ($ newimg, $ newt) for the image;} imagecopyresized ($ newimg, $ imageres, 0, 0, 0, 0, $ size ['width'], $ size ['height'], $ imageinfo ['width'], $ imageinfo ['height']); imagedestroy ($ imageres); return $ newimg;}/** + functions * Save image + functions * @ param unknown $ newimg * @ para M unknown $ newname * @ param unknown $ imageinfo * @ return unknown */private function SaveNewImage ($ newimg, $ newname, $ imageinfo) {switch ($ imageinfo ['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 $ newna Me ;} /** + watermark * watermark position Function + watermark * @ param unknown $ backinfo background information * @ param unknown $ waterinfo watermark information * @ param unknown $ waterpos watermark position * @ return boolean | multitype: number returns the coordinate array */private function getPos ($ backinfo, $ waterinfo, $ waterpos) {if ($ backinfo ['width'] <$ waterinfo ['width'] | $ Backinfo ['height'] <$ waterinfo ['height']) {return false;} switch ($ waterpos) {case 1: // $ posX = 0 in the upper left corner; $ posY = 0; break; case 2: // $ posX = $ backinfo ['width']-$ waterinfo ['width']/2; $ posY = 0; break; case 3: // $ posX = $ backinfo ['width']-$ waterinfo ['width']; $ posY = 0; break; case 4: // left China $ posX = 0; $ posY = $ backinfo ['height']-$ waterinfo ['height']/2; break; case 5: // In the middle, $ posX = $ backinfo ['width']-$ waterinfo ['w Idth ']/2; $ posY = $ backinfo ['height']-$ waterinfo ['height']/2; break; case 6: // right side $ posX = $ backinfo ['width']-$ waterinfo ['width']; $ posY = $ backinfo ['height']-$ waterinfo ['height']/2; break; case 7: // bottom to the left $ posX = 0; $ posY = $ backinfo ['height']-$ waterinfo ['height']; break; case 8: // center at the bottom $ posX = $ backinfo ['width']-$ waterinfo ['width']/2; $ posY = $ backinfo ['height']-$ waterinfo ['height']; break; case 9: // bottom right $ posX = $ B Ackinfo ['width']-$ waterinfo ['width']; $ posY = $ backinfo ['height']-$ waterinfo ['height']; break; case 0: default: $ posX = rand (0, $ backinfo ['width']-$ waterinfo ['width']); $ posY = rand (0, $ backinfo ['height']-$ waterinfo ['height']); break;} return array ('posx' => $ posX, 'posy '=> $ posY);}/** + ------------------------------------------------------------------- * copy image + -------------------------------------------- ----------------------- * @ Param unknown $ backimg background resource * @ param unknown $ waterimg watermark resource * @ param unknown $ pos watermark position * @ param unknown $ waterinfo watermark information * @ return unknown */private function CopyImage ($ backimg, $ waterimg, $ pos, $ waterinfo) {imagecopy ($ backimg, $ waterimg, $ pos ['posx'], $ pos ['posy '], 0, 0, $ waterinfo ['width'], $ waterinfo ['height']); imagedestroy ($ waterimg); return $ backimg ;}}?>
The above is all the content of this article. I hope it will help you learn PHP programming.