代码如下

 <?php

 namespace App\Contract;

 use Carbon\Carbon;

 /**
* 图片压缩封装类
* @author jackie <2019.11.20>
*/
class CompressImageContract
{
/**
* desription 压缩图片
* @param sting $imgsrc 图片路径(服務器絕對路徑)
* @param string $imgdst 压缩后保存路径(服務器絕對路徑)
*/
public function image_png_size_add($imgsrc, $imgdst)
{
$zip_width = config("config.zip_width");//讀取默認最小寬度
$zip_height = config("config.zip_height");//讀取默認最小高度
$zip_per = config("config.zip_per");//讀取默認壓縮比例 list($width, $height, $type) = getimagesize($imgsrc);
$new_width = ceil(($width > 600 ? 600 : $width) * $zip_per);
$new_height = ceil(($height > 600 ? 600 : $height) * $zip_per);
/*$width = 3000;
$height = 150;
$new_width = $width * $zip_per;
$new_height = $height * $zip_per;*/ //寬度、高度都小於最小值,取最大比例值
if ($new_width < $zip_width && $new_height < $zip_height) {
$formatWidth = sprintf("%.1f", $zip_width / $width);
$perWidth = $formatWidth + 0.1; $formatHeight = sprintf("%.1f", $zip_height / $height);
$perHeight = $formatHeight + 0.1; $per = $perWidth >= $perHeight ? $perWidth : $perHeight;
$per = $per < 1 ? $per : 1;//壓縮比例不能大於1
$new_width = ceil($width * $per);
$new_height = ceil($height * $per);
//return $new_width.'/'.$new_height.$per.'/**1';
} //寬度小於最小值,高度正常,計算寬度最小比例
if ($new_width < $zip_width && $new_height > $zip_height) {
$formatWidth = sprintf("%.1f", $zip_width / $width);
$per = $formatWidth + 0.1;
$per = $per < 1 ? $per : 1;//壓縮比例不能大於1
$new_width = ceil($width * $per);
$new_height = ceil($height * $per);
//return $new_width . '/' . $new_height . '/' . $per . '/**2';
}
//寬度正常,高度小於最小值,計算高度最小比例
if ($new_width > $zip_width && $new_height < $zip_height) {
$formatHeight = sprintf("%.1f", $zip_height / $height);
$per = $formatHeight + 0.1;
$per = $per < 1 ? $per : 1;//壓縮比例不能大於1
$new_width = ceil($width * $per);
$new_height = ceil($height * $per);
//return $new_width . '/' . $new_height . '/' . $per . '/**3';
}
//return $new_width . '/' . $new_height . '/' . $per . '/**4'; switch ($type) {
case 1:
$giftype = check_gifcartoon($imgsrc);
if ($giftype) {
header('Content-Type:image/gif');
$image_wp = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromgif($imgsrc);
imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_wp, $imgdst, 75);
imagedestroy($image_wp);
}
break;
case 2:
header('Content-Type:image/jpeg');
$image_wp = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($imgsrc);
imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_wp, $imgdst, 75);
imagedestroy($image_wp);
break;
case 3:
header('Content-Type:image/png');
$image_wp = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefrompng($imgsrc);
imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_wp, $imgdst, 75);
imagedestroy($image_wp);
break;
}
} /**
* desription 判断是否gif动画
* @param sting $image_file图片路径
* @return boolean t 是 f 否
*/
public function check_gifcartoon($image_file)
{
$fp = fopen($image_file, 'rb');
$image_head = fread($fp, 1024);
fclose($fp);
return preg_match("/" . chr(0x21) . chr(0xff) . chr(0x0b) . 'NETSCAPE2.0' . "/", $image_head) ? false : true;
}
}

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

php图片无损压缩的问题解决的更多相关文章

  1. 使用GitHub-Pages创建博客和图片上传问题解决

    title: 使用GitHub Pages创建博客和图片上传问题解决 date: 2017-10-22 20:44:11 tags: IT 技术 toc: true 搭建博客 博客的搭建过程完全参照小 ...

  2. C#/.net 通过js调用系统相机进行拍照,图片无损压缩后进行二维码识别

    这两天撸了一个需求,通过 JS  调用手机后置相机,进行拍照扫码.前台实现调用手机相机,然后截取图片并上传到后台的功能.后台接收传过来的图片后,通过调用开源二维码识别库 ZXing 进行二维码数据解析 ...

  3. .net 图片无损压缩

    命名空间: using System.Drawing.Imaging; using System.Drawing; using System.Drawing.Drawing2D; #region Ge ...

  4. C# 图片无损压缩

    /// <summary> /// 图像缩略图处理 /// </summary> /// <param name="bytes">图像源数据&l ...

  5. 关于iphone 6 ios8网站背景图片错乱的问题解决办法

    最近公司有个客户的网站用手机safari打开出现背景图片错乱,本来应该显示A图片的却显示B图片,网速越慢的情况下越容易出现这种问题,悲催的是这种情况只在iPhone 6上出现,并且不是一直这样,多刷新 ...

  6. div里嵌套了img底部会有白块问题和图片一像素问题解决

    div里嵌套了img底部会有白块 因为img默认是按基线(baseline)对齐的.对比一下图片和右边的p, q, y等字母,你会发现这三个字母的“小尾巴”和图片下方的空白一样高.下面这张图中的黑线就 ...

  7. win8以上windows系统eclipse环境下图片显示乱码问题解决

    相信升级了win10系统的诸多安卓开发者在用eclipse时会发现一个很不爽的地方,就是原本win7环境下能正常打开的图片文件现在成了一页乱码,我曾多次碰到这个问题,在网上也很难找到行之有效的具体解决 ...

  8. iOS裁剪,缩放图片白边问题解决办法

    几年没来了,感觉还是要写点啥,以后碰见问题 解决就写这吧,当是一个随时的笔记也好. iOS裁剪,缩放图片的代码网上也很多了,但是笔者出现了右边和下边出现白边的情况.出现白边的原因是给的size中的CG ...

  9. Android ImageView的几种对图片的缩放处理 解决imageview放大图片后失真问题解决办法

    我的解决办法: 1 首先设置android:layout_width=”wrap_content”和android:layout_height=”wrap_content”,否则你按比例缩放后的图片放 ...

随机推荐

  1. Servlet学习request对象总结

    一.servletContext对象和request对象的比较 ServletContext 何时创建:服务器启动 何时销毁:服务器关闭 域的作用范围:整个web应用 request 何时创建:访问时 ...

  2. 使用字节流(InputStream、OutputStream)简单完成对文件的复制

    文件的复制 import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; imp ...

  3. 编写mapreduce的程序的套路

    https://blog.csdn.net/qq_42881421/article/details/83543926 给出下面6个经典案例: http://www.cnblogs.com/xia520 ...

  4. 51nod 1661: 黑板上的游戏(sg函数 找规律)

    题目链接 先打一个sg函数的表,找找规律,发现sg函数可以递归求解 打表代码如下 #include<bits/stdc++.h> using namespace std; typedef ...

  5. 51nod 1122:机器人走方格 V4 (矩阵快速幂)

    题目链接 昨天上随机信号分析讲马氏链的时候突然想到这题的解法,今天写一下 定义矩阵A,Ans=A^n,令A[i][j]表示,经过1次变换后,第i个位置上的机器人位于第j个位置的情况数,则Ans[i][ ...

  6. 偏函数(partial)

    from functools import partial def add(a,b,c,d): return a+b+c+d add = partial(add,1,2) print(add(3,4) ...

  7. webpack对html模板的处理

    一.打包html模板到相应目录并且引入js 需要安装 html-webpack-plugin 然后在plugins里实例化 new HtmlWebpackPlugin({ template:'./sr ...

  8. hbase 1.4 部署

    centos 7 部署 hbase 1.4.11 版本 在部署 hbase 前,首先安装好 jdk 1.7 和 zookeeper 3.4.14 软件. 部署 zookeeper 软件,可以参考 zk ...

  9. 使用 jQuery 实现 radio 的选中与反选

    使用 jQuery 实现 radio 的选中与反选 我们知道在 Html 中当我们选中一个radio后,再次点击该 radio,那么该 radio 依然是一个选中的状态,但是有时我们需要实现这样的逻辑 ...

  10. Python笔记(八)_内部函数与闭包

    内部函数 在函数内部定义另一个函数,也就是函数的嵌套 在外部函数的作用域内,外部函数可以随意调用内部函数 由于内部函数的整个定义过程都在外部函数中,所以出了外部函数就无法再被调用了 def outsi ...