缩略图生成基于GD库的PHP代码(支持jpg,gif,png格式)

分类:PHP_Python| 发布:佚名| 查看:334 | 发表时间:2014/7/14

代码说明一切,直接上吧:

001<?php
002/**
003 * 缩略图生成类,使用示例:
004 */
005$newimage=new ImageResize();
006$newimage->resize("tu.jpg","tu_lit.jpg",1000,1000);
007echo $newimage->GetLastError();
008 
009class ImageResize{
010  private $localimage;//原图路径
011  private $remoteimage;//缩略图保存路径
012  private $localinfo;//原图属性
013  private $error;
014       
015  function resize($localimg, $remoteimg, $x, $y) {
016    //检测是否支持gd图像处理
017    if(!$this->_checkenv()){
018      return false;
019    }
020    $this->localimage = $localimg;
021    $this->remoteimage = $remoteimg;
022    $this->localinfo = getimagesize($this->localimage); //获取本地图像的信息
023    return $this->_resize($x,$y);
024  }
025  /**
026   * 检测当前环境是否支持GD
027   */
028  private function _checkenv(){
029    if(!function_exists('gd_info')){
030      $this->error[]="当前环境不支持GD图像处理,请先安装GD库并开启PHP相关扩展";
031      return false;
032    }
033    return true;
034  }
035   
036  /**
037   * 生成缩略图主函数
038   * @param int $x 指定的缩略图宽度
039   * @param int $y 指定的缩略图高度
040   * @return boolean
041   */
042  private function _resize($x,$y){
043    if(!$this->localinfo){
044      $this->error[]="本地图像文件不存在";
045      return false;
046    }
047    //创建图像句柄
048    $im=@$this->_create($this->localinfo[2]);
049    if(!$im){
050      $this->error[]="当前GD库不支持图像类型:{$this->localinfo['mime']}";
051      return false;
052    }
053    $dstsize=$this->_dstsize($x, $y);
054    $dstim=@imagecreatetruecolor($dstsize["width"],$dstsize["height"]);
055    $whitecolor=@imagecolorallocatealpha($dstim, 0, 0, 0,127);
056    imagefill($dstim,0,0,$whitecolor);
057    $re=@imagecopyresampled($dstim, $im, 0, 0, 0, 0, $dstsize["width"], $dstsize["height"], $this->localinfo[0], $this->localinfo[1]);
058    if(!$re){
059      $this->error[]="图像重新采样失败";
060      return false;
061    }
062    if(!imagejpeg($dstim, $this->remoteimage)){
063      if(!imagepng($dstim,$this->remoteimage)){
064        if(!imagegif($dstim,$this->remoteimage)){
065          $this->error[]="保存缩略图到{$this->remoteimage}失败,请检查gd环境是否正常和缩略图文件夹的写入权限。";
066          return false;
067        }
068      }
069    }
070    $this->error[]="success";
071    return true;
072  }
073   
074  /**
075   * 根据本地图片类型,创建图片资源
076   * @param 图像类型代码 $code
077   * @return resource/boolean 成功则返回resourse失败则返回false
078   */
079  private function _create($code){
080    $src=$this->localimage;
081    switch ($code){
082      case 1:
083        return imagecreatefromgif($src);
084        break;
085      case 2:
086        return imagecreatefromjpeg($src);
087        break;
088      case 3:
089        return imagecreatefrompng($src);
090        break;
091      default :
092        return false;
093        break;
094    }
095  }
096   
097  /**
098   * 按比例计算合适的宽度
099   * @param int $x 指定的缩略图宽度
100   * @param int $y 指定的缩略图高度
101   * @return array 包含调整后的缩略图宽度和高度
102   */
103  private function _dstsize($x,$y){
104    list($srcwidth,$srcheight)=$this->localinfo;
105    if(($srcwidth/$srcheight)<($x/$y)){
106      $x=floor($y*$srcwidth/$srcheight);
107    }else{
108      $y=floor($x*$srcheight/$srcwidth);
109    }
110    $dstsize["width"]=$x;
111    $dstsize["height"]=$y;
112    return $dstsize;
113  }
114 
115  /**
116   * 获取最后一条错误信息
117   * return string
118   */
119  function GetLastError(){
120    return array_pop($this->error);
121  }
122   
123  /**
124   * 获取所有错误信息
125   * return array
126   */
127  function GetAllError(){
128    return $this->error;
129  }
130}

 

365据说看到好文章不转的人,服务器容易宕机
原创文章如转载,请注明:转载自郑州网建-前端开发 http://camnpr.com/
本文链接:http://camnpr.com/php-python/1465.html