• PHP按一定比例压缩图片


    图片压缩是我们日常开发中经常使用的操作,在如今需求很多的情况往往,上传的一张图片会被压缩成不同比例的图片,每次去操作也是一件非常繁琐的事情,于是进行了封装了一个压缩图片的操作类,希望大家遇到后,不用再为写很多压缩图片代码烦恼了。

     1 <?php
     2 
     3 /**
     4  * 图片压缩操作类
     5  * v1.0
     6  */
     7 class Image
     8 {
     9     private $src;
    10     private $imageinfo;
    11     private $image;
    12     public $percent = 0.1;
    13 
    14     public function __construct($src)
    15     {
    16         $this->src = $src;
    17     }
    18 
    19     /**
    20      * 打开图片
    21      */
    22     public function openImage()
    23     {
    24         list($width, $height, $type, $attr) = getimagesize($this->src);
    25         $this->imageinfo = array(
    26             'width' => $width,
    27             'height' => $height,
    28             'type' => image_type_to_extension($type, false),
    29             'attr' => $attr
    30         );
    31         $fun = "imagecreatefrom" . $this->imageinfo['type'];
    32         $this->image = $fun($this->src);
    33     }
    34 
    35     /**
    36      * 操作图片
    37      */
    38     public function thumpImage()
    39     {
    40         $new_width = $this->imageinfo['width'] * $this->percent;
    41         $new_height = $this->imageinfo['height'] * $this->percent;
    42         $image_thump = imagecreatetruecolor($new_width, $new_height);
    43         //将原图复制带图片载体上面,并且按照一定比例压缩,极大的保持了清晰度
    44         imagecopyresampled($image_thump, $this->image, 0, 0, 0, 0, $new_width, $new_height, $this->imageinfo['width'], $this->imageinfo['height']);
    45 //        imagedestroy($this->image);
    46         $this->image = $image_thump;
    47     }
    48 
    49     /**
    50      * 输出图片
    51      */
    52     public function showImage()
    53     {
    54         header('Content-Type: image/' . $this->imageinfo['type']);
    55         $funcs = "image" . $this->imageinfo['type'];
    56         $funcs($this->image);
    57     }
    58 
    59     /**
    60      * 保存图片到硬盘
    61      * @param $savePaht 保存的文件夹路径
    62      * @param $zip_name 压缩后图片名
    63      * quality 为可选项,范围从 0(最差质量,文件更小)到 100(最佳质量,文件最大)。默认为 IJG 默认的质量值(大约 75)。
    64      */
    65     public function saveImage($savePaht, $zip_name, $quality = 75)
    66     {
    67         if (!file_exists($savePaht)) {
    68             mkdir($savePaht, 0777, true);
    69         }
    70 
    71         imagejpeg($this->image, $savePaht . $zip_name . '.' . $this->imageinfo['type'], $quality);
    72         //$funcs = "image" . $this->imageinfo['type'];
    73         //$funcs($this->image, $name . '.' . $this->imageinfo['type']);
    74     }
    75 
    76     /**
    77      * 销毁图片
    78      */
    79     public function __destruct()
    80     {
    81         imagedestroy($this->image);
    82     }
    83 }
    84 
    85 ?>

    测试:

     1 <?php
     2 date_default_timezone_set("Asia/Shanghai");
     3 require 'Image.php';
     4 $src = "./img/7_02M龙虾.jpg";//要压缩的的图片
     5 $zip_name = 'zip_' . date("YmdHis");//压缩后图片名称
     6 $savePaht = "img/";//压缩后保存路径(称服務器絕對路徑)
     7 
     8 $image = new Image($src);
     9 $image->percent = 0.2;
    10 $image->openImage();
    11 $image->thumpImage();
    12 //$image->showImage();//输出图片
    13 $image->saveImage($savePaht, $zip_name, 75);
    14 ?>

    图片:压缩前7M,压缩后83k

    参考:https://mp.weixin.qq.com/s/n_Yt_Lif8SW2eR3bxerQAA

    推荐采用我另一篇博客的方法:https://www.cnblogs.com/clubs/p/11911525.html

  • 相关阅读:
    还原大备份mysql文件失败的解决方法
    第一次招聘
    Data Transformation Services (DTS) Package Example
    .net Page 执行事件顺序
    批量修改相同結構的table layout
    js 中exec、test、match、search、replace、split用法
    存储过程分页,按多条件排序(原创)
    Lisp 的本质 [转]
    【原创】jQuery方法的写法不同导致的兼容性
    WinForm 图片变灰方法
  • 原文地址:https://www.cnblogs.com/clubs/p/12101857.html
Copyright © 2020-2023  润新知