浅谈关于PHP解决图片无损压缩的问题


Posted in PHP onSeptember 01, 2017

本文介绍了关于PHP解决图片无损压缩的问题,分享给大家,具体如下:

代码如下:

header("Content-type: image/jpeg"); 
$file = "111.jpg"; 
$percent = 1.5; //图片压缩比 
list($width, $height) = getimagesize($file); //获取原图尺寸 
//缩放尺寸 
$newwidth = $width * $percent; 
$newheight = $height * $percent; 
$src_im = imagecreatefromjpeg($file); 
$dst_im = imagecreatetruecolor($newwidth, $newheight); 
imagecopyresized($dst_im, $src_im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 
imagejpeg($dst_im); //输出压缩后的图片 
imagedestroy($dst_im); 
imagedestroy($src_im);

我发现用php的imagecopyresized把大图片缩成小图片时,图片会变得很模糊,这时候要提升清晰度不如用 imagecopyresampled 代替 imagecopyresized也许会更好。

注:压缩有损失是必然的,看的清楚与否实际上就是是否接受这个范围的问题.比如你图像上原图有些点是2px,但是你压缩5倍,那么这些点就会消失。

<?php  
/** 
* desription 压缩图片 
* @param sting $imgsrc 图片路径 
* @param string $imgdst 压缩后保存路径 
*/ 
function image_png_size_add($imgsrc,$imgdst){  
 list($width,$height,$type)=getimagesize($imgsrc);  
 $new_width = ($width>600?600:$width)*0.9;  
 $new_height =($height>600?600:$height)*0.9;  
 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 否 
*/ 
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;  
}  
?>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

PHP 相关文章推荐
动态网站web开发 PHP、ASP还是ASP.NET
Oct 09 PHP
php中对xml读取的相关函数的介绍一
Jun 05 PHP
使用迭代器 遍历文件信息的详解
Jun 08 PHP
php stream_get_meta_data返回值
Sep 29 PHP
laravel 4安装及入门图文教程
Oct 29 PHP
php使用Jpgraph绘制饼状图的方法
Jun 10 PHP
php抽象类用法实例分析
Jul 07 PHP
Yii2组件之多图上传插件FileInput的详细使用教程
Jun 20 PHP
验证坐标在某坐标区域内php代码
Oct 08 PHP
thinkPHP批量删除的实现方法分析
Nov 09 PHP
php preg_match的匹配不同国家语言实例
Dec 29 PHP
Yii框架实现记录日志到自定义文件的方法
May 23 PHP
phpStudy配置多站点多域名和多端口的方法
Sep 01 #PHP
[原创]PHP获取数组表示的路径方法分析【数组转字符串】
Sep 01 #PHP
使用PHP连接数据库_实现用户数据的增删改查的整体操作示例
Sep 01 #PHP
php插件Xajax使用方法详解
Aug 31 #PHP
php数据库的增删改查 php与javascript之间的交互
Aug 31 #PHP
php注册系统和使用Xajax即时验证用户名是否被占用
Aug 31 #PHP
PHP文字转图片功能原理与实现方法分析
Aug 31 #PHP
You might like
剖析 PHP 中的输出缓冲
2006/12/21 PHP
php $_SERVER[&quot;REQUEST_URI&quot;]获取值的通用解决方法
2010/06/21 PHP
PHPMailer ThinkPHP实现自动发送邮件功能
2018/06/10 PHP
JavaScript触发器详解
2007/03/10 Javascript
javascript 控制弹出窗口
2007/04/10 Javascript
javascript 面向对象编程基础:继承
2009/08/21 Javascript
jQuery EasyUI NumberBox(数字框)的用法
2010/07/08 Javascript
让浏览器非阻塞加载javascript的几种方法小结
2011/04/25 Javascript
jQuery getJSON()+.ashx 实现分页(改进版)
2013/03/28 Javascript
jQuery实现自定义下拉列表
2015/01/05 Javascript
使用jQuery实现返回顶部
2015/01/26 Javascript
javascript实用方法总结
2015/02/06 Javascript
JS延时提示框实现方法详解
2015/11/26 Javascript
jQuery循环遍历子节点并获取值的方法
2016/04/14 Javascript
使用jQuery Mobile框架开发移动端Web App的入门教程
2016/05/17 Javascript
通过javascript进行UTF-8编码的实现方法
2016/06/27 Javascript
JS中数组重排序方法
2016/11/11 Javascript
微信小程序 Buffer缓冲区的详解
2017/07/06 Javascript
使用javaScript实现鼠标拖拽事件
2020/04/03 Javascript
jquery实现联想词搜索框和搜索结果分页的示例
2018/10/10 jQuery
javascript定时器的简单应用示例【控制方块移动】
2019/06/17 Javascript
在vue中使用Echarts利用watch做动态数据渲染操作
2020/07/20 Javascript
python操作MongoDB基础知识
2013/11/01 Python
python3中bytes和string之间的互相转换
2017/02/09 Python
Django查询数据库的性能优化示例代码
2017/09/24 Python
理解python中生成器用法
2017/12/20 Python
python编写Logistic逻辑回归
2020/12/30 Python
python 制作自定义包并安装到系统目录的方法
2018/10/27 Python
python暴力解压rar加密文件过程详解
2019/07/05 Python
Django+uni-app实现数据通信中的请求跨域的示例代码
2019/10/12 Python
Python 中的函数装饰器和闭包详解
2021/02/06 Python
详解CSS3选择器:nth-child和:nth-of-type之间的差异
2017/09/18 HTML / CSS
html2canvas把div保存图片高清图的方法示例
2018/03/05 HTML / CSS
Rentalcars.com中国:世界上最大的在线汽车租赁服务
2019/08/22 全球购物
博士论文答辩开场白
2015/06/01 职场文书
医院感染管理制度
2015/08/05 职场文书