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”,否则你按比例缩放后的图片放 ...
随机推荐
- JavaScript之BOM+DOM
BOM 浏览器对象模型, 用于把浏览器相关的组件封装为对象进行操作. BOM是包含了DOM的. window对象 弹出框相关 确认: 取消: 与打开关闭window有关的方法 定时器相关 暂停选老婆 ...
- mui滚动区域的实现
mui框架实现页面中间区域滚动,头部和底部固定不动,要滚动的区域一定要有mui-scroll-wrapper 和 mui-scroll 包裹 <div class="mui-conte ...
- 【RabbitMQ】Concurrency、Prefetch、exclusive
分布式消息中间件 RabbitMQ是用Erlang语言编写的分布式消息中间件,常常用在大型网站中作为消息队列来使用,主要目的是各个子系统之间的解耦和异步处理.消息中间件的基本模型是典型的生产者-消费者 ...
- 重温HTML和CSS3
重温Web前端基础 本篇幅中着重文字,只是记录一些自己的见解,巩固下自身基础 网页结构是什么? 结构层 html 导航,列表,段文字,图片,链接,表示层 css 颜色,大小,位置,行为层 JavaSc ...
- handler消息机制入门
handler消息机制入门 为什么要用handle? 我们在网络上读取图片信息时,是不能把耗时操作放在主线程里面的,当我们在子线程中获取到了图片的消息的时候,我们就需要把这个数据传给主线程. 而直接使 ...
- php中class类文件引入方法汇总
在项目中 总是会用到类文件引入的操作,在此简单总结下: 方法一: 使用 include,require,include_once,require_once. 其中:*_once once意为曾经 ...
- PHP抓取远程图片到本地保存(如何把错误信息用text文件写入)
最近在工作中需要开发了一个用户素材功能,里面需要将网上的各种图片素材进行本地化存储.于是在网上找了一些相关资料,并根据自身开发需要,整理了一下主要的逻辑代码. /** * PHP将网页上的图片攫取到本 ...
- codeforces 557D Vitaly and Cycle
题意简述 给定一个图 求至少添加多少条边使得它存在奇环 并求出添加的方案数 (注意不考虑自环) ---------------------------------------------------- ...
- hdu3518 Boring counting(后缀数组)
Boring counting 题目传送门 解题思路 后缀数组.枚举每种长度,对于每个字符串,记录其最大起始位置和最小起始位置,比较是否重合. 代码如下 #include <bits/stdc+ ...
- HBase最佳实践-读性能优化策略
任何系统都会有各种各样的问题,有些是系统本身设计问题,有些却是使用姿势问题.HBase也一样,在真实生产线上大家或多或少都会遇到很多问题,有些是HBase还需要完善的,有些是我们确实对它了解太少.总结 ...