php生成图片缩略图的方法

分享一个php生成缩略图片的方法,支持jpg,gif,png格式,支持按原图片的比例进行缩放,可以选择在图片缩放的过程中是否需要对图片进行裁切,加入了图片质量控制,可以实现缩略图片质量最高化。完整类的代码如下:

  1. <?php 
  2. //php生成缩略图片的类 
  3. class ResizeImage{ 
  4.     public $type;//图片类型 
  5.     public $width;//实际宽度 
  6.     public $height;//实际高度 
  7.     public $resize_width;//改变后的宽度 
  8.     public $resize_height;//改变后的高度 
  9.     public $cut;//是否裁图 
  10.     public $srcimg;//源图象   
  11.     public $dstimg;//目标图象地址 
  12.     public $im;//临时创建的图象 
  13.     public $quality;//图片质量 
  14.     //http://www.phpernote.com/php-function/415.html 
  15.     function resizeimage($img,$wid,$hei,$c,$dstpath,$quality=100){ 
  16.         $this->srcimg=$img
  17.         $this->resize_width=$wid
  18.         $this->resize_height=$hei
  19.         $this->cut=$c
  20.         $this->quality=$quality
  21.         $this->type=strtolower(substr(strrchr($this->srcimg,'.'),1));//图片的类型 
  22.         $this->initi_img();//初始化图象 
  23.         $this -> dst_img($dstpath);//目标图象地址 
  24.         $this->width=imagesx($this->im); 
  25.         $this->height=imagesy($this->im); 
  26.         $this->newimg();//生成图象 
  27.         ImageDestroy($this->im); 
  28.     } 
  29.     function newimg(){ 
  30.         $resize_ratio=($this->resize_width)/($this->resize_height);//改变后的图象的比例 
  31.         $ratio=($this->width)/($this->height);//实际图象的比例 
  32.         if(($this->cut)=='1'){//裁图 
  33.             if($img_func==='imagepng'&&(str_replace('.','',PHP_VERSION)>=512)){//针对php版本大于5.12参数变化后的处理情况 
  34.                 $quality=9; 
  35.             } 
  36.             if($ratio>=$resize_ratio){//高度优先 
  37.                 $newimg=imagecreatetruecolor($this->resize_width,$this->resize_height); 
  38.                 imagecopyresampled($newimg,$this->im,0,0,0,0,$this->resize_width,$this->resize_height,(($this->height)*$resize_ratio),$this->height); 
  39.                 imagejpeg($newimg,$this->dstimg,$this->quality); 
  40.             } 
  41.             if($ratio<$resize_ratio){//宽度优先 
  42.                 $newimg=imagecreatetruecolor($this->resize_width,$this->resize_height); 
  43.                 imagecopyresampled($newimg,$this->im,0,0,0,0,$this->resize_width,$this->resize_height,$this->width,(($this->width)/$resize_ratio)); 
  44.                 imagejpeg($newimg,$this->dstimg,$this->quality); 
  45.             } 
  46.         }else{//不裁图 
  47.             if($ratio>=$resize_ratio){ 
  48.                 $newimg=imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio); 
  49.                 imagecopyresampled($newimg,$this->im,0,0,0,0,$this->resize_width,($this->resize_width)/$ratio,$this->width,$this->height); 
  50.                 imagejpeg($newimg,$this->dstimg,$this->quality); 
  51.             } 
  52.             if($ratio<$resize_ratio){ 
  53.                 $newimg=imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height); 
  54.                 imagecopyresampled($newimg,$this->im,0,0,0,0,($this->resize_height)*$ratio,$this->resize_height,$this->width,$this->height); 
  55.                 imagejpeg($newimg,$this->dstimg,$this->quality); 
  56.             } 
  57.         } 
  58.     } 
  59.     function initi_img(){//初始化图象 
  60.         if($this->type=='jpg'){ 
  61.             $this->im=imagecreatefromjpeg($this->srcimg); 
  62.         } 
  63.         if($this->type=='gif'){ 
  64.             $this->im=imagecreatefromgif($this->srcimg); 
  65.         } 
  66.         if($this->type=='png'){ 
  67.             $this->im=imagecreatefrompng($this->srcimg); 
  68.         } 
  69.     } 
  70.     function dst_img($dstpath){//图象目标地址 
  71.         $full_length=strlen($this->srcimg); 
  72.         $type_length=strlen($this->type); 
  73.         $name_length=$full_length-$type_length
  74.         $name=substr($this->srcimg,0,$name_length-1); 
  75.         $this->dstimg=$dstpath
  76.         //echo $this->dstimg; 
  77.     } 

该类的使用方法也很简单,如下就是根据原图片生成一个宽不超过100,高不超过200的缩略图片:

  1. $resizeimage=new resizeimage('www.phpernote.com.jpg','100','200','0','php and mysql development_thumb.jpg'); 

转载请注明:代码家园 » php生成图片缩略图的方法

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)