zoukankan      html  css  js  c++  java
  • PHP缩略图类

      1 class ThumbImages{
      2     /**
      3      * 生成缩略图
      4      * prorate 按比例缩放
      5      * distortion 扭曲型缩图
      6      * cut 最小裁剪后的缩图
      7      * backFill 背景填充图
      8      * clssname ThumbImages
      9      * createtime:2014-1-14下午04:44:28
     10      * author:lhm
     11      * for example
     12      * include 'ThumbImages.class.php'
     13      * $th=new ThumbImages(array('imagePre'=>'',
     14      *                           'imagePath'=>'./uploads/thumb/',
     15      *                           'echoType'=>'file'));
     16      * $th->setThumb($srcImage, 384, 300, 'prorate');
     17      */
     18     private $imagepre;               //缩略图名称前缀
     19     private $imagepath;             //缩略图存放路径
     20     private $srcImage='';          //源图路径
     21     private $newImageName;        //缩略图名称
     22     private $imageType;           //图片类型
     23     private $echotype;          //输出图片类型,link--直接输出到浏览器;file--保存为文件
     24     private $im='';            //临时变量
     25     private $originName='';   //源图名称
     26     private $srcW='';        //源图宽
     27     private $srcH='';       //源图高
     28     private $errorNum=0;   //错误号
     29     private $errorMess='';//用来提供错误报告
     30     
     31  
     32     public function __construct($options = array()){
     33         $allowOption = array('imagepre','imagepath','echotype');
     34         foreach($options as $key=>$val){
     35             $key = strtolower($key);
     36             //查看用户参数中数组的下标是否和成员属性名相同
     37             //if(!in_array($key,get_class_vars(get_class($this)))){
     38             if(!in_array($key, $allowOption)){
     39                 $this->errorNum = -3;
     40                 $this->errorMess = $this->getError();
     41                 continue 1;
     42             }
     43             $this->$key = $val;
     44         }
     45     }
     46     /**
     47      * 
     48      * 判断源文件路径、缩略图存放路径是否正确
     49      * 判断GD库是否支持缩略图格式
     50      * 初始化图片高宽、缩略图存放路径、缩略图生成方式
     51      * @param string $srcImage
     52      * @param int $toW
     53      * @param int $toH
     54      * @param string $method
     55      * @param string $echotype
     56      */
     57     public function setThumb($srcImage = '', $toW = 0, $toH = 0, $method = 'distortion'){
     58         $this->srcImage = $srcImage;
     59         $imageName = explode('/', $this->srcImage);
     60         $this->originName = $imageName[2];
     61            //检查源文件是否存在
     62         if(empty($srcImage) || filetype($srcImage) != 'file'){
     63             $this->errorNum = 4;
     64             $this->errorMess = $this->getError();
     65             return false;
     66         }
     67         //检查文件上传路径
     68         if ($this->echotype == 'file'){
     69             if(!$this->checkImagePath()){
     70                 $this->errorMess = $this->getError();
     71                 return false;
     72             }
     73         }
     74         $info = '';
     75         $data = getimagesize($this->srcImage, $info);//获取图像大小
     76         $this->srcW = $data[0];//
     77         $this->srcH = $data[1];// 78          //检查GD库
     79         if(!$this->checkGD($data[2])){
     80             $this->errorMess = $this->getError();
     81             return false;
     82         }
     83         $this->setImageName();//设置缩略图名称
     84         $toFile = $this->imagepath.$this->newImageName;//缩略图存放路径
     85         $return = $this->createImageMethod($method, $toFile, $toW, $toH);
     86           return $return;
     87     }
     88     /**
     89      * 
     90      * 初始化缩略图生成方式
     91      * prorate 按比例缩放
     92      * distortion 扭曲型缩图
     93      * cut 最小裁剪后的缩图
     94      * backFill 背景填充图
     95      * @param string $method
     96      * @param string $toFile
     97      * @param int $toW
     98      * @param int $toH
     99      */
    100     private function createImageMethod($method, $toFile, $toW, $toH){
    101         switch ($method){
    102               case 'prorate':
    103                   $return = $this->prorate($toFile, $toW, $toH);
    104                   break;
    105               case 'cut':
    106                   $return = $this->cut($toFile, $toW, $toH);
    107                   break;
    108               case 'backFill':
    109                   $return = $this->backFill($toFile, $toW, $toH);
    110                   break;
    111               default:
    112                   $return = $this->distortion($toFile, $toW, $toH);
    113            }
    114            return $return;
    115     }
    116     //生成扭曲型缩图
    117     function distortion($toFile='', $toW=0, $toH=0){
    118         $cImg=$this->creatImage($this->im, $toW, $toH, 0, 0, 0, 0, $this->srcW, $this->srcH);
    119         return $this->echoImage($cImg, $toFile);
    120         imagedestroy($cImg);
    121     }
    122     
    123     //生成按比例缩放的缩图
    124     function prorate($toFile, $toW, $toH){
    125         $toWH = $toW / $toH;
    126         $srcWH = $this->srcW / $this->srcH;
    127         if($toWH <= $srcWH){
    128             $ftoW = $toW;
    129             $ftoH = $ftoW * ($this->srcH / $this->srcW);
    130         }else{
    131               $ftoH = $toH;
    132               $ftoW = $ftoH * ($this->srcW / $this->srcH);
    133         }
    134         if($this->srcW > $toW || $this->srcH > $toH){
    135             $cImg = $this->creatImage($this->im, $ftoW, $ftoH, 0, 0, 0, 0, $this->srcW, $this->srcH);
    136             return $this->echoImage($cImg, $toFile);
    137             imagedestroy($cImg);
    138         }else{
    139             $cImg = $this->creatImage($this->im, $this->srcW, $this->srcH, 0, 0, 0, 0, $this->srcW, $this->srcH);
    140             return $this->echoImage($cImg, $toFile);
    141             imagedestroy($cImg);
    142         }
    143     }
    144     
    145     //生成最小裁剪后的缩图
    146     private function cut($toFile, $toW, $toH){
    147           $toWH = $toW/$toH;
    148           $srcWH = $this->srcW / $this->srcH;
    149           if($toWH <= $srcWH){
    150                $ctoH = $toH;
    151                $ctoW = $ctoH * ($this->srcW / $this->srcH);
    152           }else{
    153               $ctoW = $toW;
    154               $ctoH = $ctoW * ($this->srcH / $this->srcW);
    155           } 
    156         $allImg = $this->creatImage($this->im, $ctoW, $ctoH, 0, 0, 0, 0, $this->srcW, $this->srcH);
    157         $cImg = $this->creatImage($allImg, $toW, $toH, 0, 0, ($ctoW-$toW) / 2, ($ctoH-$toH) / 2, $toW, $toH);
    158         return $this->echoImage($cImg, $toFile);
    159         imagedestroy($cImg);
    160         imagedestroy($allImg);
    161     }
    162  
    163     //生成背景填充的缩图
    164     private function backFill($toFile, $toW, $toH, $bk1=255, $bk2=255, $bk3=255){
    165         $toWH = $toW / $toH;
    166         $srcWH = $this->srcW / $this->srcH;
    167         if($toWH <= $srcWH){
    168             $ftoW = $toW;
    169             $ftoH = $ftoW * ($this->srcH / $this->srcW);
    170         }else{
    171               $ftoH = $toH;
    172               $ftoW = $ftoH*($this->srcW / $this->srcH);
    173         }
    174         if(function_exists("imagecreatetruecolor")){
    175             @$cImg = imagecreatetruecolor($toW,$toH);
    176             if(!$cImg){
    177                 $cImg = imagecreate($toW,$toH);
    178             }
    179         }else{
    180             $cImg = imagecreate($toW,$toH);
    181         }
    182         $backcolor = imagecolorallocate($cImg, $bk1, $bk2, $bk3);        //填充的背景颜色
    183         imagefilledrectangle($cImg,0,0,$toW,$toH,$backcolor);
    184         if($this->srcW > $toW || $this->srcH > $toH){
    185             $proImg = $this->creatImage($this->im,$ftoW,$ftoH,0,0,0,0,$this->srcW,$this->srcH);
    186             if($ftoW < $toW){
    187                  imagecopy($cImg, $proImg, ($toW - $ftoW) / 2, 0, 0, 0, $ftoW, $ftoH);
    188             }else if($ftoH < $toH){
    189                  imagecopy($cImg, $proImg, 0, ($toH-$ftoH) / 2, 0, 0, $ftoW, $ftoH);
    190             }else{
    191                  imagecopy($cImg, $proImg, 0, 0, 0, 0, $ftoW, $ftoH);
    192             }
    193         }else{
    194              imagecopymerge($cImg, $this->im, ($toW - $ftoW) / 2,($toH - $ftoH) / 2, 0, 0, $ftoW, $ftoH, 100);
    195         }
    196         return $this->echoImage($cImg, $toFile);
    197         imagedestroy($cImg);
    198     }
    199     //创建图像
    200     private function creatImage($img, $creatW, $creatH, $dstX, $dstY, $srcX, $srcY, $srcImgW, $srcImgH){
    201         if(function_exists("imagecreatetruecolor")){
    202             @$creatImg = imagecreatetruecolor($creatW, $creatH);
    203             if($creatImg){
    204                 imagecopyresampled($creatImg, $img, $dstX, $dstY, $srcX, $srcY, $creatW, $creatH, $srcImgW, $srcImgH);
    205             }else{
    206                 $creatImg=imagecreate($creatW,$creatH);
    207                 imagecopyresized($creatImg, $img, $dstX, $dstY, $srcX, $srcY, $creatW, $creatH, $srcImgW, $srcImgH);
    208             }
    209          }else{
    210             $creatImg=imagecreate($creatW, $creatH);
    211             imagecopyresized($creatImg, $img, $dstX, $dstY, $srcX, $srcY, $creatW, $creatH, $srcImgW, $srcImgH);
    212          }
    213          return $creatImg;
    214     }
    215     
    216     //输出图片,link---只输出,不保存文件。file--保存为文件
    217     function echoImage($img, $toFile){
    218         switch($this->echotype){
    219             case 'link':
    220                 if(function_exists('imagejpeg')) 
    221                     return imagejpeg($img);
    222                 else 
    223                     return imagepng($img);
    224                 break;
    225             case 'file':
    226                 if(function_exists('imagejpeg')) 
    227                     return imagejpeg($img,$toFile);
    228                 else 
    229                     return imagepng($img,$toFile);
    230                 break;
    231         }
    232     }
    233     /**
    234      * 
    235      * 设置随机文件名称
    236      * @access private
    237      * @return string
    238      */
    239     private function setRandName(){
    240         $fileName = date("YmdHis").rand(100,999);
    241         return $fileName.'.'.$this->imageType;
    242     }
    243     private function setImageName(){
    244         if ($this->imagepre != ''){
    245             $this->newImageName = $this->imagepre.'_'.$this->setRandName();
    246         }else {
    247             $this->newImageName = $this->setRandName();
    248         }
    249     }
    250     /**
    251      * 
    252      * 用来检查文件上传路径
    253      * @access private
    254      * @return bool
    255      */
    256     private function checkImagePath(){
    257         if(empty($this->imagepath)) {
    258             $this->errorNum = -2;
    259             return false;
    260         }
    261 
    262         if(!file_exists($this->imagepath) || !is_writable($this->imagepath)){
    263             if(!@mkdir($this->imagepath, 0755)){
    264                 $this->errorNum = -1;
    265                 return false;
    266             }
    267         }
    268         return true;
    269     }
    270     private function checkGD($imageType){
    271         switch ($imageType){
    272              case 1:
    273                if(!function_exists("imagecreatefromgif")){
    274                        $this->errorNum = 1;
    275                        return false;
    276                }
    277                $this->imageType = 'gif';
    278                $this->im = imagecreatefromgif($this->srcImage);
    279                break;
    280              case 2:
    281                if(!function_exists("imagecreatefromjpeg")){
    282                      $this->errorNum = 2;
    283                      return false;
    284                 }
    285                 $this->imageType = 'jpg';
    286                 $this->im = imagecreatefromjpeg($this->srcImage);    
    287                 break;
    288              case 3:
    289                 if(!function_exists("imagecreatefrompng")){
    290                       $this->errorNum = 3;
    291                       return false;
    292                  }
    293                  $this->imageType = 'png';
    294                  $this->im = imagecreatefrompng($this->srcImage);    
    295                  break;
    296               default:
    297                  $this->errorNum = 0;
    298                  return false;
    299           }
    300           return true;
    301     }
    302     /**
    303      * 
    304      * 用于获取上传后缩略图片的文件名
    305      * @access public
    306      * @return string
    307      */
    308     public function getNewImageName(){
    309         return $this->newImageName;
    310     }
    311 
    312     /**
    313      * 
    314      * 获取上传错误信息
    315      * @access private
    316      * @return string
    317      */
    318     private function getError(){
    319         $str='生成缩略图<font color="red">'.$this->originName.'</font>时出错:';
    320 
    321         switch($this->errorNum){
    322             case 4: $str .= '没有找到需要缩略的图片'; 
    323                 break;
    324             case 3: $str .= '你的GD库不能使用png格式的图片,请使用其它格式的图片!'; 
    325                 break;
    326             case 2: $str .= '你的GD库不能使用jpeg格式的图片,请使用其它格式的图片!';
    327                 break;
    328             case 1: $str .= '你的GD库不能使用GIF格式的图片,请使用Jpeg或PNG格式!';
    329                 break;
    330             case -1: $str .= '建立存放缩略图目录失败,请重新指定缩略图目录';
    331                 break;
    332             case -2: $str .= '必须指定缩略图的路径';
    333                 break;
    334             case -3: $str .= '初始化参数出错';
    335                 break;
    336             default: $str .= '末知错误';
    337         }
    338 
    339         return $str.'<br>';
    340     }
    341     public function getErrorMsg() {
    342         return $this->errorMess;
    343     }
    344 }
  • 相关阅读:
    单元测试
    现代软件工程构建之法
    安卓的编码规范
    如何用代码编写四则运算?
    springboot之mybatisplus,mp的简单理解
    appache 端口 更改
    visual studio进程或线程自上一个步骤以来已更改
    小白CSS学习日记杂乱无序记录(3)
    shiro之初探
    mybatisplus 代码生成器 generator的小坑
  • 原文地址:https://www.cnblogs.com/shlhm/p/3522006.html
Copyright © 2011-2022 走看看