代码如下

 <?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. python3输出中文报错的原因,及解决办法(基于pycharm)

    通常python3里面如果有中文,在不连接其他设备和程序的情况下,报错信息大致如下: SyntaxError: Non-UTF-8 code starting with '\xd6' in file ...

  2. js append()和appendChild()和insertBefore()的区别

    <body> <input type="button" value="删除" id="btn"> <scrip ...

  3. 最强CP!阿里云联手支付宝小程序如何助力双11?

    作为首次“全面上云”的双11,阿里云征服了每秒订单峰值54.4万笔的世界新记录.正是在阿里云的保驾护航下,即使访问量是平时的5到6倍,小程序也鲜少出现卡顿或者宕机的现象,“依靠阿里云,我们整个天猫双1 ...

  4. 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 GSM Base Station Identification (点在多边形内模板)

    In the Personal Communication Service systems such as GSM (Global System for Mobile Communications), ...

  5. tracert命令 -网络管理命令

    Tracert是路由跟踪程序,用于确定 IP 数据报访问目标所经过的路径.Tracert 命令用 IP 生存时间 (TTL) 字段和 ICMP 错误消息来确定从一个主机到网络上其他主机的路由. 在工作 ...

  6. v-show与v-if的区别

    v-show有dom节点像display:none,而v-if隐藏的则没有dom节点.两个共同点都可以显隐

  7. BZOJ 3772: 精神污染(dfs序+主席树)

    传送门 解题思路 比较神仙的一道题.首先计算答案时可以每条路径所包含的路径数,对于\(x,y\)这条路径,可以在\(x\)这处开个\(vector\)存\(y\),然后计算时只需要算这个路径上每个点的 ...

  8. create-react-app 配置 修改

    1.端口号修改:https://www.jianshu.com/p/80a7603dda70(亲测有效)   在 根据 package.json 的启动,node_modules文件夹里面搜索reac ...

  9. 前端每日实战:18# 视频演示如何用纯 CSS 创作 404 文字变形为 NON 文字的交互特效

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/ZoxjXm 可交互视频教程 此视频 ...

  10. 【Java架构:持续交付】一篇文章搞掂:Jenkins

    一.安装 1.使用yum本地安装 1.1.使用yum安装JDK a.检查系统是否有安装open-jdk rpm -qa |grep java rpm -qa |grep jdk rpm -qa |gr ...