PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > PHP教程 > 正文

    php生成图片缩略图类或者说任意缩放图片

    作者:admin来源:B5教程网浏览:时间:2020-09-30 00:07:50我要评论
    导读:等比例缩放的,如果你指定的尺寸和缩放的比例不一致,会用纯色填补。 本人不才,第一次分享代码,欢迎拍砖!
    等比例缩放的,如果你指定的尺寸和缩放的比例不一致,会用纯色填补。本人不才,第一次分享代

    码,欢迎拍砖!


     
    复制代码代码如下:
    1. <?php
    2. /**
    3.  * @abstract 生成图片的缩略图,可以指定任意尺寸,生成的图片为png格式
    4.  * @example
    5.  * $file = 'test.png';
    6.  * $th =new Thumbnail();
    7.  * $th->GenerateThumbnail($file, 400, 500);
    8.  *
    9.  */
    10. class Thumbnail{
    11.      /**
    12.      * @var string $from 源图片
    13.      */
    14.      private $from;
    15.      /**
    16.      * @var string $name 缩略图的文件名
    17.      */
    18.      private $name = '';
    19.      /**
    20.      * @var 原图宽
    21.      */
    22.      private $rWidth;
    23.      /**
    24.      * @var 原图高
    25.      */
    26.      private $rHeight;
    27.      /**
    28.      * @var 缩略图宽
    29.      */
    30.      private $tWidth;
    31.      /**
    32.      * @var 缩略图高
    33.      */
    34.      private $tHeight;
    35.      /**
    36.      * @var 实际缩放到的宽度
    37.      */
    38.      private $width;
    39.      /**
    40.      * @var 实际缩放到的高度
    41.      */
    42.      private $height;
    43.  
    44.      public function __construct(){
    45.          try{
    46.              if(!function_exists('gd_info')){
    47.                  throw new Exception('Must GD extension is enabled');
    48.              }
    49.          }
    50.          catch(Exception $e){
    51.              $msg = 'class ' . __CLASS__ . ' Error:' . $e->getMessage();
    52.              echo $msg;
    53.              exit;
    54.          }
    55.      }
    56.      /**
    57.      * @var $from 原图像
    58.      * @var $width 生成的缩略图的宽
    59.      * @var $height 生成缩略图的高
    60.      * @var $name 生成的缩略图的文件名,不带后缀
    61.      * @return string 生成的缩略图
    62.      */
    63.      public function GenerateThumbnail($from, $width, $height, $name=''){
    64.          try{
    65.              if(!file_exists($from)){
    66.                  throw new Exception('File does not exist');
    67.              }
    68.              if($width <= 0){
    69.                  throw new Exception('The width is invalid');
    70.              }
    71.              if($height <= 0){
    72.                  throw new Exception('The height is invalid');
    73.              }
    74.              $this->from = $from;
    75.              $this->tWidth = $width;
    76.              $this->tHeight = $height;
    77.              if(!empty($name)){
    78.                  $this->name = $name;
    79.              }
    80.              else{
    81.                  $this->name = date('Ymd') . mt_rand(0, 9999);
    82.              }
    83.              $this->createThumbnail();
    84.          }
    85.          catch(Exception $e){
    86.              $msg = 'class ' . __CLASS__ . ' Error:' . $e->getMessage();
    87.              echo $msg;
    88.              exit;
    89.          }
    90.      }
    91.  
    92.      public function getThumbnail(){
    93.          return $this->name;
    94.      }
    95.  
    96.      /**
    97.      * 生成缩略图文件
    98.      */
    99.      private function createThumbnail(){
    100.          try{
    101.              //读取原始图像信息
    102.              $sourceInfo = getimagesize($this->from);
    103.              $this->rWidth = $sourceInfo[0];
    104.              $this->rHeight = $sourceInfo[1];
    105.              //创建缩略图图像资源句柄
    106.              $new_pic = imagecreatetruecolor($this->tWidth, $this->tHeight);
    107.              //原图绘制到缩略图的x、y坐标
    108.              $= 0;
    109.              $= 0;
    110.              //创建原始图像资源句柄
    111.              $source_pic = '';
    112.              switch ($sourceInfo[2]){
    113.                  case 1: $source_pic = imagecreatefromgif($this->from); //gif
    114.                          break;
    115.                  case 2: $source_pic = imagecreatefromjpeg($this->from); //jpg
    116.                          break;
    117.                  case 3: $source_pic = imagecreatefrompng($this->from); //png
    118.                          break;
    119.                  default: throw new Exception('Does not support this type of image');
    120.              }
    121.              //计算缩放后图像实际大小
    122.              //原图宽高均比缩略图大
    123.              if($this->rWidth > $this->tWidth && $this->rHeight > $this->tHeight){
    124.                  $midw = ($this->rWidth - $this->tWidth) / $this->rWidth; //宽缩小的比例
    125.                  $midh = ($this->rHeight - $this->tHeight) / $this->rHeight; //高缩小的比例
    126.                  //那个缩小的比例大以那个为准
    127.                  if($midw > $midh){
    128.                      $this->width = $this->tWidth;
    129.                      $this->height = $this->rHeight - floor($this->rHeight * $midw);
    130.                      $= ($this->tHeight - $this->height) / 2;
    131.                  }
    132.                  else{
    133.                      $this->width = $this->rWidth - floor($this->rWidth * $midh);
    134.                      $this->height = $this->tHeight;
    135.                      $= ($this->tWidth - $this->width) / 2;
    136.                  }
    137.              }
    138.              //原图宽高均比缩略图小
    139.              elseif($this->rWidth < $this->tWidth && $this->rHeight < $this->tHeight){
    140.                  $midw = ($this->tWidth - $this->rWidth) / $this->rWidth; //宽放大的比例
    141.                  $midh = ($this->tHeight - $this->rHeight) / $this->rHeight; //高放大的比例
    142.                  //那个放大的比例小以那个为准
    143.                  if($midw < $midh){
    144.                      $this->width = $this->tWidth;
    145.                      $this->height = $this->rHeight + floor($this->rHeight * $midw);
    146.                      $= ($this->tHeight - $this->height) / 2;
    147.                  }
    148.                  else{
    149.                      $this->width = $this->rWidth + floor($this->rWidth * $midh);
    150.                      $this->height = $this->tHeight;
    151.                      $= ($this->tWidth - $this->width) / 2;
    152.                  }
    153.              }
    154.              //原图宽小于缩略图宽,原图高大于缩略图高
    155.              elseif($this->rWidth < $this->tWidth && $this->rHeight > $this->tHeight){
    156.                  $mid = ($this->rHeight - $this->tHeight) / $this->rHeight; //高缩小的比例
    157.                  $this->width = $this->rWidth - floor($this->rWidth * $mid);
    158.                  $this->height = $this->rHeight - floor($this->rHeight * $mid);
    159.                  $= ($this->tWidth - $this->width) / 2;
    160.                  $= ($this->tHeight - $this->height) / 2;
    161.              }
    162.              //原图宽大于缩略图宽,原图高小于缩略图高
    163.              elseif($this->rWidth > $this->tWidth && $this->rHeight < $this->tHeight){
    164.                  $mid = ($this->rWidth - $this->tWidth) / $this->rWidth; //宽缩小的比例
    165.                  $this->width = $this->rWidth - floor($this->rWidth * $mid);
    166.                  $this->height = $this->rHeight - floor($this->rHeight * $mid);
    167.                  $= ($this->tWidth - $this->width) / 2;
    168.                  $= ($this->tHeight - $this->height) / 2;
    169.              }
    170.              else{
    171.                  throw new Exception('Resize error');
    172.              }
    173.  
    174.              //给缩略图添加白色背景
    175.              $bg = imagecolorallocate($new_pic, 255, 255, 255);
    176.              imagefill($new_pic, 0, 0, $bg);
    177.              //缩小原始图片到新建图片
    178.              imagecopyresampled($new_pic, $source_pic, $x, $y, 0, 0, $this->width, $this->height, $this->rWidth, $this->rHeight);
    179.              //输出缩略图到文件
    180.              imagepng($new_pic, $this->name.'.png');
    181.              imagedestroy($new_pic);
    182.              imagedestroy($source_pic);
    183.          }
    184.          catch(Exception $e){
    185.              $msg = 'class ' . __CLASS__ . ' Error:' . $e->getMessage();
    186.              echo $msg;
    187.              exit;
    188.          }
    189.      }
    190. }
    191.  

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-10-182-1.html
    相关热词搜索: 缩略图 缩放 php类
    下一篇:php文件上传类