生成缩略图的PHP类
/** * 获得图像的缩略图 * @name get_minImg_class.php * **/ class getMinImg{ public $imgSize=array();//存储img尺寸数组$imgSize['width'],$imgSize['height'] public $thum=array();//缩略图数组$thum['width'],$thum['height'] public $newImgSize=array();//生成新图片的大小$newImgSize['width'],$newImgSize['height'] private $imgfunc;//调用的图片函数名 private $handle;//文件句柄 public $filename;//文件名 public $imgInfo=array();//文件信息$imgInfo[0]代表文件路径及名称(无后缀),$imgInfo[1]代表文件类型 private $newHandle;//新的文件句柄 function __construct($filename,$thum=array('width'=>150,'height'=>100)){ $this->filename=$filename; if(!(boolean)$this->filename){ return false;} $this->thum=$thum; $this->getImgType(); $this->getHandle(); $this->getImgSize(); $this->getNewImgSize(); } private function getHandle(){//获得文件句柄 switch ($this->imgInfo[1]) { case 'jpg': $this->handle=imagecreatefromjpeg($this->filename); $this->imgfunc="imagejpeg"; break; case 'jpeg': $this->handle=imagecreatefromjpeg($this->filename); $this->imgfunc="imagejpeg"; break; case 'png': $this->handle=imagecreatefrompng($this->filename); $this->imgfunc="imagepng"; break; case 'gif': $this->handle=imagecreatefromgif($this->filename); $this->imgfunc="imagegif"; break; default:$this->handle=imagecreatefromjpeg($this->filename); $this->imgfunc="imagejpeg"; break; } } public function getImgType(){//获得图像的类型 $ext=array(); $ext=explode(".", $this->filename); $this->imgInfo=$ext; } public function getImgSize(){//获得图像大小 $imgSize=array(); $arr=getimagesize($this->filename); $imgSize['width']=$arr[0]; $imgSize['height']=$arr[1]; //var_dump($imgSize); $this->imgSize=$imgSize; } public function getNewImgSize(){//获得新图像的大小 $newImgSize=array(); if($this->imgSize['width']>$this->thum['width'] || $this->imgSize['height']>$this->thum['height']){ if($this->imgSize['width']>$this->imgSize['height']){ $newImgSize['width']=$this->thum['width']; $newImgSize['height']=$this->imgSize['height']*($this->thum['width']/$this->imgSize['width']); } if($this->imgSize['height']>$this->imgSize['width']){ $newImgSize['height']=$this->thum['height'];; $newImgSize['width']=$this->imgSize['width']*($this->thum['height']/$this->imgSize['height']); } if($this->imgSize['height']===$this->imgSize['width']){ $newImgSize['height']=$this->thum['height']; $newImgSize['width']=$this->thum['width']; } //echo "big"; }else{ $newImgSize['height']=$this->imgSize['height']; $newImgSize['width']=$this->imgSize['width']; } $this->newImgSize=$newImgSize; } public function getAndSaveImg(){//重新生成图片 $this->newHandle=imagecreatetruecolor($this->newImgSize['width'],$this->newImgSize['height']); imagecopyresized($this->newHandle, $this->handle, 0, 0, 0, 0, $this->newImgSize['width'], $this->newImgSize['height'], $this->imgSize['width'], $this->imgSize['height']); call_user_func_array($this->imgfunc,array($this->newHandle,"newImg/0001.png")); } function __destruct(){ imagedestroy($this->handle); imagedestroy($this->newHandle); } }