浅谈关于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 相关文章推荐
php代码优化及php相关问题总结
Oct 09 PHP
PHP中的类-什么叫类
Nov 20 PHP
PHP采集相关教程之一 CURL函数库
Feb 15 PHP
php标签云的实现代码
Oct 10 PHP
php中的Base62类(适用于数值转字符串)
Aug 12 PHP
php获取表单中多个同名input元素的值
Mar 20 PHP
php根据某字段对多维数组进行排序的方法
Mar 07 PHP
PHP常用的小程序代码段
Nov 14 PHP
PHP封装的非对称加密RSA算法示例
May 28 PHP
php使用QueryList轻松采集js动态渲染页面方法
Sep 11 PHP
php+mysql开发的最简单在线题库(在线做题系统)完整案例
Mar 30 PHP
thinkPHP框架通过Redis实现增删改查操作的方法详解
May 13 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操作JSON格式数据的实现代码
2011/12/24 PHP
支持生僻字且自动识别utf-8编码的php汉字转拼音类
2014/06/27 PHP
PHP四种基本排序算法示例
2015/04/09 PHP
php返回当前日期或者指定日期是周几
2015/05/21 PHP
详解PHP错误日志的获取方法
2015/07/20 PHP
php $_SESSION会员登录实例分享
2021/01/19 PHP
PHP检查网站是否宕机的方法示例
2017/07/24 PHP
PHP对称加密算法(DES/AES)类的实现代码
2017/11/14 PHP
掌握PHP垃圾回收机制详解
2019/03/13 PHP
Javascript 原型和继承(Prototypes and Inheritance)
2009/04/01 Javascript
用js实现的检测浏览器和系统的函数
2009/04/09 Javascript
jQuery 性能优化指南(2)
2009/05/21 Javascript
JS图片浏览组件PhotoLook的公开属性方法介绍和进阶实例代码
2010/11/09 Javascript
基于jquery的获取浏览器窗口大小的代码
2011/03/28 Javascript
jquery下jstree简单应用 - v1.0
2011/04/14 Javascript
jquery动态分页效果堪比时光网
2014/09/25 Javascript
node.js中的querystring.stringify方法使用说明
2014/12/10 Javascript
js的三种继承方式详解
2017/01/21 Javascript
angular ng-repeat数组中的数组实例
2017/02/18 Javascript
分析JS中this引发的bug
2017/12/12 Javascript
React中如何引入Angular组件详解
2018/08/09 Javascript
微信小程序自定义可滑动日历界面
2018/12/28 Javascript
从0到1搭建element后台框架优化篇(打包优化)
2019/05/12 Javascript
原生JS实现pc端轮播图效果
2020/12/21 Javascript
Python的Django框架中TEMPLATES项的设置教程
2015/05/29 Python
Python使用cx_Oracle调用Oracle存储过程的方法示例
2017/10/07 Python
Python 字符串与数字输出方法
2018/07/16 Python
Python实现计算字符串中出现次数最多的字符示例
2019/01/21 Python
Python3获取拉勾网招聘信息的方法实例
2019/04/03 Python
对pyqt5之menu和action的使用详解
2019/06/20 Python
Django中Middleware中的函数详解
2019/07/18 Python
CSS3实现水平居中、垂直居中、水平垂直居中的实例代码
2020/02/27 HTML / CSS
医院信息公开实施方案
2014/05/09 职场文书
酒店前台岗位职责
2015/04/16 职场文书
失恋33天观后感
2015/06/11 职场文书
2016年暑期教师培训心得体会
2016/01/09 职场文书