• php 图片调整大小 封装类【转载】


      1 <?php
      2 class ImageResize
      3 {
      4     private $image;
      5     private $img_des;
      6     private $image_type;
      7     private $permissions;
      8     private $compression;
      9  
     10     /**
     11      * 构造函数
     12      * @param string $img_src     源图片
     13      * @param string $img_des     保存图片对象
     14      * @param int    $compression 压缩比率,范围0-100(0:压缩比最高,100:压缩比最低)
     15      * @param null   $permissions 改变文件模式
     16      *                            mode 参数由 4 个数字组成:
     17      *                            第一个数字永远是 0
     18      *                            第二个数字规定所有者的权限
     19      *                            第二个数字规定所有者所属的用户组的权限
     20      *                            第四个数字规定其他所有人的权限
     21      *                            1 - 执行权限,2 - 写权限,4 - 读权限
     22      */
     23     function __construct($img_src, $img_des = NULL, $compression = 75, $permissions = null)
     24     {
     25         $image_info = getimagesize($img_src);
     26  
     27         $this->img_des = $img_des;
     28         $this->image_type = $image_info[2];
     29         $this->compression = $compression;
     30         $this->permissions = $permissions;
     31  
     32         if ($this->image_type == IMAGETYPE_JPEG) {
     33             $this->image = ImageCreateFromJPEG($img_src);
     34         } elseif ($this->image_type == IMAGETYPE_GIF) {
     35             $this->image = ImageCreateFromGIF($img_src);
     36         } elseif ($this->image_type == IMAGETYPE_PNG) {
     37             $this->image = ImageCreateFromPNG($img_src);
     38         }
     39     }
     40  
     41     /**
     42      * 保存图片
     43      */
     44     function save()
     45     {
     46         if ($this->image_type == IMAGETYPE_JPEG) {
     47             imagejpeg($this->image, $this->img_des, $this->compression);
     48         } elseif ($this->image_type == IMAGETYPE_GIF) {
     49             imagegif($this->image, $this->img_des);
     50         } elseif ($this->image_type == IMAGETYPE_PNG) {
     51             imagepng($this->image, $this->img_des);
     52         }
     53  
     54         if ($this->permissions != null) {
     55             chmod($this->image, $this->compression);
     56         }
     57     }
     58  
     59     /**
     60      * 做为图片流直接输出
     61      */
     62     function output()
     63     {
     64         if ($this->image_type == IMAGETYPE_JPEG) {
     65             header('Content-Type: image/jpg');
     66             imagejpeg($this->image);
     67         } elseif ($this->image_type == IMAGETYPE_GIF) {
     68             header('Content-Type: image/gif');
     69             imagegif($this->image);
     70         } elseif ($this->image_type == IMAGETYPE_PNG) {
     71             header('Content-Type: image/png');
     72             imagepng($this->image);
     73         }
     74     }
     75  
     76     /**
     77      * 获取图片宽度
     78      * @return int 图片宽度
     79      */
     80     function getWidth()
     81     {
     82         return imagesx($this->image);
     83     }
     84  
     85     /*
     86      * 获取图片高度
     87      * @return int 图片高度
     88      */
     89     function getHeight()
     90     {
     91         return imagesy($this->image);
     92     }
     93  
     94     /**
     95      * 按照固定高度缩放图片
     96      * @param $height 需要改变大小的高度
     97      */
     98     function resizeToHeight($height)
     99     {
    100         $ratio = $height / $this->getHeight();
    101         $width = $this->getWidth() * $ratio;
    102         $this->resize($width, $height);
    103     }
    104  
    105     /**
    106      * 按照固定宽度缩放图片
    107      * @param $width 指定宽度
    108      */
    109     function resizeToWidth($width)
    110     {
    111         $ratio = $width / $this->getWidth();
    112         $height = $this->getheight() * $ratio;
    113         $this->resize($width, $height);
    114     }
    115  
    116     /**
    117      * 等比缩放图片
    118      * @param int $scale 缩放比例
    119      */
    120     function scale($scale)
    121     {
    122         $width = $this->getWidth() * $scale / 100;
    123         $height = $this->getheight() * $scale / 100;
    124         $this->resize($width, $height);
    125     }
    126  
    127  
    128     /**
    129      * 指定宽度和高度缩放图片
    130      * @param int $width  缩放宽度
    131      * @param int $height 缩放高度
    132      * @return 缩放后图片对象
    133      */
    134     function resize($width, $height)
    135     {
    136         $new_image = imagecreatetruecolor($width, $height);
    137  
    138         if ($this->image_type == IMAGETYPE_GIF || $this->image_type == IMAGETYPE_PNG) {
    139             $current_transparent = imagecolortransparent($this->image);
    140             if ($current_transparent != -1) {
    141                 $transparent_color = imagecolorsforindex($this->image, $current_transparent);
    142                 $current_transparent = imagecolorallocate($new_image, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
    143                 imagefill($new_image, 0, 0, $current_transparent);
    144                 imagecolortransparent($new_image, $current_transparent);
    145             } elseif ($this->image_type == IMAGETYPE_PNG) {
    146                 imagealphablending($new_image, false);
    147                 $color = imagecolorallocatealpha($new_image, 0, 0, 0, 127);
    148                 imagefill($new_image, 0, 0, $color);
    149                 imagesavealpha($new_image, true);
    150             }
    151         }
    152  
    153         imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
    154         $this->image = $new_image;
    155     }
    156 }
    157  
    158 ?>

    使用方法:

    1 $image = new ImageResize($tmp_image_name, NULL, 100);
    2 $image->output();
  • 相关阅读:
    webpack进阶(二)
    webpack的loader和plugin的区别
    接口和面向接口编程
    设计原则与编程技巧汇总
    W3C的盒子模型和IE的盒子模型
    React的组件
    React的路由react-router
    三种编程命名规则:驼峰命名法,帕斯卡命名法,匈牙利命名法
    gulp 构建 demo
    antd 表单的两种校验方式
  • 原文地址:https://www.cnblogs.com/itafter/p/4120729.html
Copyright © 2020-2023  润新知