Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 696|回复: 0

php图片无损压缩的问题解决(推荐)

[复制链接]
  • TA的每日心情
    奋斗
    2024-4-6 11:05
  • 签到天数: 748 天

    [LV.9]以坛为家II

    2034

    主题

    2092

    帖子

    70万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    705612
    发表于 2021-6-1 08:38:11 | 显示全部楼层 |阅读模式

    代码如下

      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

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-19 12:09 , Processed in 0.068052 second(s), 30 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表