Php-encapsulated image (thumbnail) processing class complete instance,
This article describes the php-encapsulated image (thumbnail) processing class. We will share this with you for your reference. The details are as follows:
<? Php // Image processing tool class Image {// attribute private $ thumb_width; // width private $ thumb_height of the thumbnail; // error attribute public $ thumb_error; // constructor public function _ construct ($ width = 0, $ height = 0) {$ this-> thumb_width = ($ width = 0 )? $ GLOBALS ['config'] ['admin _ goods_thumb '] ['width']: $ width; $ this-> thumb_height = ($ height = 0 )? $ GLOBALS ['config'] ['admin _ goods_thumb '] ['height']: $ height;}/** create a thumbnail * @ param1 string $ src, source image path, /uploads/20150122101010abcdef.gif * @ param2 string $ path, thumbnail save path/uploads/thumb_20150122101010abcdef.gif * @ return thumbnail name */public function makeThumb ($ src, $ path) {// determine whether the source image exists if (! File_exists ($ src) {$ this-> thumb_error = 'the source image does not exist! '; Return false;} // open the source image resource // obtain the available suffix $ ext = $ this-> getFunctionName ($ src ); // gif // name of the patchwork function $ open = 'imagecreatefrom '. $ ext; // imagecreatefromgif $ save = 'image '. $ ext; // imagegif // if not clear; echo $ open, $ save; exit; // Variable Function to open source image resources $ src_img = $ open ($ src ); // use the variable function to open image resources // imagecreatefromgif ($ src) // The thumbnail resource $ dst_img = imagecreatetruecolor ($ this-> thumb_width, $ this-> thumb_height ); // fill the background color with white $ dst_bg_color = ima Gecolorallocate ($ dst_img, 255,255,255); imagefill ($ dst_img, 0, 0, $ dst_bg_color ); // determine the width and height of the aspect ratio $ dst_size = $ this-> thumb_width/$ this-> thumb_height; // obtain the source image data $ file_info = getimagesize ($ src ); $ src_size = $ file_info [0]/$ file_info [1]; // obtain the thumbnail width and height if ($ src_size> $ dst_size) {// the aspect ratio of the source image is greater than the thumbnail $ width = $ this-> thumb_width; $ height = round ($ width/$ src_size );} else {$ height = $ this-> thumb_height; $ width = round ($ heig Ht * $ src_size);} // locate the starting position of the thumbnail $ dst_x = round ($ this-> thumb_width-$ width)/2; $ dst_y = round ($ this-> thumb_height-$ height)/2; // create a thumbnail if (imagecopyresampled ($ dst_img, $ src_img, $ dst_x, $ dst_y, 0, 0, $ width, $ height, $ file_info [0], $ file_info [1]) {// sampling successful: save, save the file to the corresponding path $ thumb_name = 'thumb _'. basename ($ src); $ save ($ dst_img, $ path. '/'. $ thumb_name); // save successfully. return $ thumb_name;} else {// sample failed $ this-> thumb _ Error = 'thumbnail sampling failed! '; Return false ;}}/** get the name of the function to be called in the file * @ param1 string $ file, file name * @ return function string obtained by file extension */private function getFunctionName ($ file) {// get the file suffix $ file_info = pathinfo ($ file ); $ ext = $ file_info ['extension']; // Suffix: gif, png, jpg, jpeg, pjpeg // imagecreatefromgif, imagecreatefromjpeg, imagecreatefrompng // define an array to save the function name $ func = array ('gif' => 'gif', 'png '=> 'png ', 'jpg '=> 'jpeg', 'jpeg '=> 'jpeg', 'pjpeg '=> 'jpeg '); // return value return $ func [$ ext];}