<?php//thumbnail class class thumb{public static $thumb _prefix = ";//thumbnail prefix public static $thumb _savepath = ";//thumbnail save path public static $thumb _width = ";// Thumbnail width public static $thumb _height = ";//thumbnail height public static function save ($ spath) {//Save as thumbnail, pass in an argument $imgarr = self::getimageinfo ($sPath);//Get Picture Information if (Is_array ($IMGARR)) {//Three static attributes are split into arrays separated by commas to achieve the generation of multiple thumbnails $thumb_prefix_arr = explode (', ', self:: $thumb _prefix); $thumb _ Width_arr = explode (', ', self:: $thumb _width); $thumb _height_arr = explode (', ', Self:: $thumb _height); $countP = count ($thumb _prefix_arr); $countW = count ($thumb _width_arr); $countH = count ($thumb _height_arr);if ($countP >0 && $countP = = $countW && $countW = = $countH) {//iterates through an array, generating multiple thumbnails $rearr = array ();foreach ($thumb _prefix _arr as $key => $value) {//Generate blank canvas and fill color $image = imagecreatetruecolor ($thumb _width_arr[$key], $thumb _height_arr[$ Key]); $color = imagecolorallocate ($image, 255, 255, 255);//White Imagefill ($image, 0 , 0, $color);//Calculate the $width_scale = $imgArr [' width ']/$thumb _width_arr[$key];//aspect ratio $height_ scale = $imgArr ["Height"]/$thumb _height_arr[$key];//$scale = $width _scale> $height _ scale ? $width _scale : $height _scale;//calculate the thumbnail width height $width = $imgArr ["width"]/$scale; $ height = $imgArr ["height"]/$scale;//position to target picture $dst_x = ($thumb _width_arr[$key]-$width)/2;$ dst_y = ($thumb _height_arr[$key]-$height)/2;//get the original picture resource and copy $src_image = $IMGARR [" Createfunname "] ($sPath);//Get Raw Picture resource imagecopyresized ($image, $src _image, $dst _x, $dst _y, 0, 0, $width, $height, $imgArr ["width"], $imgArr ["height"]);//Save and return value $re = $ imgarr[' Savefunname ' ($image, SElf:: $thumb _savepath. ' /'. $thumb _prefix_arr[$key]. $imgArr [' name ']; $REARR [$key] = $re;} Destroy thumbnail resources and original image resources, free memory Imagedestroy ($image), Imagedestroy ($src _image);return $reArr;//return array}else{return false;}} Else{return false;}} Protected static function getimageinfo ($path) {if (Is_file ($path)) {$IMGARR = getimagesize ($path);if (Is_array ($IMGARR)) {//If $imgarr is an array, then $path is a real picture//Judging different picture types, using variable functions , Generate Picture resource: $createFunName, save Picture resource: $saveFunNameswitch ($imgArr [' MIME ']) {case ' image/jpeg ':case ' Image/pjpeg ': $createFunName = ' imagecreatefromjpeg '; $saveFunName = ' imagejpeg '; break;case ' image/png ': $createFunName = ' imagecreatefrompng '; $saveFunName = ' imagepng '; break; case ' image/gif ': $createFunName = ' imagecreatefromgif '; $saveFunName = ' imagegif '; Break;default:return false;} Get the original picture name $oldname = pathinfo ($path, pathinfo_basename);//Returns an array of picture information Return&nbsP;array (' name ' = = $oldName, ' type ' and ' = ' $imgArr [' mime '], ' width ' + $imgArr [0], ' height ' = = $IMGARR [1], ' Createfunname ' = $createFunName, ' savefunname ' = $saveFunName);}} Else{return false;}}}
PHP thumbnail class