php图片无损压缩的问题解决
代码如下
<?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图片无损压缩的问题解决的更多相关文章
- 使用GitHub-Pages创建博客和图片上传问题解决
title: 使用GitHub Pages创建博客和图片上传问题解决 date: 2017-10-22 20:44:11 tags: IT 技术 toc: true 搭建博客 博客的搭建过程完全参照小 ...
- C#/.net 通过js调用系统相机进行拍照,图片无损压缩后进行二维码识别
这两天撸了一个需求,通过 JS 调用手机后置相机,进行拍照扫码.前台实现调用手机相机,然后截取图片并上传到后台的功能.后台接收传过来的图片后,通过调用开源二维码识别库 ZXing 进行二维码数据解析 ...
- .net 图片无损压缩
命名空间: using System.Drawing.Imaging; using System.Drawing; using System.Drawing.Drawing2D; #region Ge ...
- C# 图片无损压缩
/// <summary> /// 图像缩略图处理 /// </summary> /// <param name="bytes">图像源数据&l ...
- 关于iphone 6 ios8网站背景图片错乱的问题解决办法
最近公司有个客户的网站用手机safari打开出现背景图片错乱,本来应该显示A图片的却显示B图片,网速越慢的情况下越容易出现这种问题,悲催的是这种情况只在iPhone 6上出现,并且不是一直这样,多刷新 ...
- div里嵌套了img底部会有白块问题和图片一像素问题解决
div里嵌套了img底部会有白块 因为img默认是按基线(baseline)对齐的.对比一下图片和右边的p, q, y等字母,你会发现这三个字母的“小尾巴”和图片下方的空白一样高.下面这张图中的黑线就 ...
- win8以上windows系统eclipse环境下图片显示乱码问题解决
相信升级了win10系统的诸多安卓开发者在用eclipse时会发现一个很不爽的地方,就是原本win7环境下能正常打开的图片文件现在成了一页乱码,我曾多次碰到这个问题,在网上也很难找到行之有效的具体解决 ...
- iOS裁剪,缩放图片白边问题解决办法
几年没来了,感觉还是要写点啥,以后碰见问题 解决就写这吧,当是一个随时的笔记也好. iOS裁剪,缩放图片的代码网上也很多了,但是笔者出现了右边和下边出现白边的情况.出现白边的原因是给的size中的CG ...
- Android ImageView的几种对图片的缩放处理 解决imageview放大图片后失真问题解决办法
我的解决办法: 1 首先设置android:layout_width=”wrap_content”和android:layout_height=”wrap_content”,否则你按比例缩放后的图片放 ...
随机推荐
- Vue-cli 项目设置每个页面标题
页面标题 在vue-router页面配置中添加meta的title信息,配合vue-router的beforeEach注册一个前置守卫用户获取到页面配置的title const title = '移动 ...
- boost multi array
Boost MultiArray is a library that simplifies using arrays with multiple dimensions. 1. #include < ...
- Linux 统计文件夹下文件个数及目录个数
1. 统计文件夹下文件的个数 ls -l | grep "^-" | wc -l 2.统计文件夹下目录的个数 ls -l | grep "^d" | wc -l ...
- vue 生命周期函数详解
beforeCreate( 创建前 ) 在实例初始化之后,数据观测和事件配置之前被调用,此时组件的选项对象还未创建,el 和 data 并未初始化,因此无法访问methods, data, compu ...
- Redis原理及拓展
Redis是单线程程序.单线程的Redis为何还能这么快? 1.所有的数据都在内存中,所有的运算都是内存级别的运算(因此时间复杂度为O(n)的指令要谨慎使用) 2.单线程操作,避免了频繁的上下文切换 ...
- WIN10无法识别安卓设备,提示Windows 无法验证此设备所需的驱动程序的数字签名
在设备管理器,显示ANDROID设备是感叹号, 不管更新驱动,还是下载什么手机助手自动安装驱动,均不可解. 从属性中查看提示的是“Windows 无法验证此设备所需的驱动程序的数字签名”, 解决办法: ...
- JS中数据结构之二叉查找树
树是一种非线性的数据结构,以分层的方式存储数据.在二叉树上进行查找非常快,为二叉树添加或删除元素也非常快. 一棵树最上面的节点称为根节点,如果一个节点下面连接多个节点,那么该节点称为父节点,它下面的节 ...
- 【LeetCode 57】插入区间
题目链接 [题解] 这题要分四种情况. 第一种.区间在所有区间的前面. 第二种.区间在所有区间的后面. 第三种.区间在某两个区间之间但是没有交集. 第四种.区间和某个区间产生了相交. 对于第四种枚举第 ...
- 【HDU6701】Make Rounddog Happy【权值线段树+双向单调队列】
题意:给你一个序列,求满足要求的子序列个数,其中要求为: 1.子序列的max-子序列长度len<=k 2.子序列中不出现重复的数字 题解:首先看到子序列max,很容易想到枚举最大值然后分治,这个 ...
- Security基础(三):OpenSSL及证书服务、邮件TLS/SSL加密通信
一.OpenSSL及证书服务 目标: 本案例要求熟悉OpenSSL工具的基本使用,完成以下任务操作: 使用OpenSSL加密/解密文件 搭建企业自有的CA服务器,为颁发数字证书提供基础环境 方案: 使 ...