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”,否则你按比例缩放后的图片放 ...
随机推荐
- Linux重定向命令(stdout, stdin, stderr)
ls -l /usr/bin > ls-output.txt 将输出结果重定向到 ls-output.txt 文件.注意:再次使用> ls-output.txt会默认覆盖源文件.如果要追加 ...
- ImageField 字段的使用
Django模型中的ImageField和FileField的upload_to选项是必填项,其存储路径是相对于MEIDA_ROOT而来的.
- 单词计数-MapReduceJob
pom文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3. ...
- linux0.11内核源码——进程各状态切换的跟踪
准备工作 1.进程的状态有五种:新建(N),就绪或等待(J),睡眠或阻塞(W),运行(R),退出(E),其实还有个僵尸进程,这里先忽略 2.编写一个样本程序process.c,里面实现了一个函数 /* ...
- BZOJ 3772: 精神污染(dfs序+主席树)
传送门 解题思路 比较神仙的一道题.首先计算答案时可以每条路径所包含的路径数,对于\(x,y\)这条路径,可以在\(x\)这处开个\(vector\)存\(y\),然后计算时只需要算这个路径上每个点的 ...
- [CSP-S模拟测试]:次芝麻(数学)
题目描述 小$K$和小$X$都是小次货.身为小次货,最重要的事情就是次啦!所以他们正在纠结如何分芝麻次.一开始,小$K$有$n$个芝麻,小$X$有$m$个芝麻.因为他们都想次更多芝麻,所以每次手中芝麻 ...
- vijos 1243 生产产品
貌似两年前联赛复习的时候就看过这题 然而当时大概看看了 感觉太难 便没有去做 如今再去做的时候 发现其实也并不容易 ------------------------------------------ ...
- CSS最基础的语法和三种引入方式
**CSS语法** CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明.选择器通常是您需要改变样式的 HTML 元素. selector {declaration1; declaration ...
- Linux操作系统(四)_部署MySQL
一.部署过程 1.当前服务器的内核版本和发行版本 cat /etc/issue uname -a 2.检查系统有没有自带mysql,并卸载自带版本 yum list installed | grep ...
- 【读书笔记】:MIT线性代数(4):Independence, Basis and Dimension
Independence: The columns of A are independent when the nullspace N (A) contains only the zero vecto ...