php图片无损压缩的问题解决

假如想象 提交于 2019-12-05 06:49:02

代码如下

  1 <?php
  2 
  3 namespace App\Contract;
  4 
  5 use Carbon\Carbon;
  6 
  7 /**
  8  * 图片压缩封装类
  9  * @author jackie <2019.11.20>
 10  */
 11 class CompressImageContract
 12 {
 13     /**
 14      * desription 压缩图片
 15      * @param sting $imgsrc 图片路径(服務器絕對路徑)
 16      * @param string $imgdst 压缩后保存路径(服務器絕對路徑)
 17      */
 18     public function image_png_size_add($imgsrc, $imgdst)
 19     {
 20         $zip_width = config("config.zip_width");//讀取默認最小寬度
 21         $zip_height = config("config.zip_height");//讀取默認最小高度
 22         $zip_per = config("config.zip_per");//讀取默認壓縮比例
 23 
 24         list($width, $height, $type) = getimagesize($imgsrc);
 25         $new_width = ceil(($width > 600 ? 600 : $width) * $zip_per);
 26         $new_height = ceil(($height > 600 ? 600 : $height) * $zip_per);
 27         /*$width = 3000;
 28         $height = 150;
 29         $new_width = $width * $zip_per;
 30         $new_height = $height * $zip_per;*/
 31 
 32         //寬度、高度都小於最小值,取最大比例值
 33         if ($new_width < $zip_width && $new_height < $zip_height) {
 34             $formatWidth = sprintf("%.1f", $zip_width / $width);
 35             $perWidth = $formatWidth + 0.1;
 36 
 37             $formatHeight = sprintf("%.1f", $zip_height / $height);
 38             $perHeight = $formatHeight + 0.1;
 39 
 40             $per = $perWidth >= $perHeight ? $perWidth : $perHeight;
 41             $per = $per < 1 ? $per : 1;//壓縮比例不能大於1
 42             $new_width = ceil($width * $per);
 43             $new_height = ceil($height * $per);
 44             //return $new_width.'/'.$new_height.$per.'/**1';
 45         }
 46 
 47         //寬度小於最小值,高度正常,計算寬度最小比例
 48         if ($new_width < $zip_width && $new_height > $zip_height) {
 49             $formatWidth = sprintf("%.1f", $zip_width / $width);
 50             $per = $formatWidth + 0.1;
 51             $per = $per < 1 ? $per : 1;//壓縮比例不能大於1
 52             $new_width = ceil($width * $per);
 53             $new_height = ceil($height * $per);
 54             //return $new_width . '/' . $new_height . '/' . $per . '/**2';
 55         }
 56         //寬度正常,高度小於最小值,計算高度最小比例
 57         if ($new_width > $zip_width && $new_height < $zip_height) {
 58             $formatHeight = sprintf("%.1f", $zip_height / $height);
 59             $per = $formatHeight + 0.1;
 60             $per = $per < 1 ? $per : 1;//壓縮比例不能大於1
 61             $new_width = ceil($width * $per);
 62             $new_height = ceil($height * $per);
 63             //return $new_width . '/' . $new_height . '/' . $per . '/**3';
 64         }
 65         //return $new_width . '/' . $new_height . '/' . $per . '/**4';
 66 
 67         switch ($type) {
 68             case 1:
 69                 $giftype = check_gifcartoon($imgsrc);
 70                 if ($giftype) {
 71                     header('Content-Type:image/gif');
 72                     $image_wp = imagecreatetruecolor($new_width, $new_height);
 73                     $image = imagecreatefromgif($imgsrc);
 74                     imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
 75                     imagejpeg($image_wp, $imgdst, 75);
 76                     imagedestroy($image_wp);
 77                 }
 78                 break;
 79             case 2:
 80                 header('Content-Type:image/jpeg');
 81                 $image_wp = imagecreatetruecolor($new_width, $new_height);
 82                 $image = imagecreatefromjpeg($imgsrc);
 83                 imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
 84                 imagejpeg($image_wp, $imgdst, 75);
 85                 imagedestroy($image_wp);
 86                 break;
 87             case 3:
 88                 header('Content-Type:image/png');
 89                 $image_wp = imagecreatetruecolor($new_width, $new_height);
 90                 $image = imagecreatefrompng($imgsrc);
 91                 imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
 92                 imagejpeg($image_wp, $imgdst, 75);
 93                 imagedestroy($image_wp);
 94                 break;
 95         }
 96     }
 97 
 98     /**
 99      * desription 判断是否gif动画
100      * @param sting $image_file图片路径
101      * @return boolean t 是 f 否
102      */
103     public function check_gifcartoon($image_file)
104     {
105         $fp = fopen($image_file, 'rb');
106         $image_head = fread($fp, 1024);
107         fclose($fp);
108         return preg_match("/" . chr(0x21) . chr(0xff) . chr(0x0b) . 'NETSCAPE2.0' . "/", $image_head) ? false : true;
109     }
110 }

 

链接:https://www.php.cn/php-weizijiaocheng-377677.html

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!