PHP实现图片压缩


Posted in PHP onSeptember 09, 2020

本文实例为大家分享了PHP实现图片压缩的具体代码,供大家参考,具体内容如下

/**
 * 生成图片
 * @param string $im 源图片路径
 * @param string $dest 目标图片路径
 * @param int $maxwidth 生成图片宽
 * @param int $maxheight 生成图片高
 */
function resizeImage($im, $dest, $maxwidth, $maxheight) {
 $img = getimagesize($im);
 switch ($img[2]) {
 case 1:
  $im = @imagecreatefromgif($im);
  break;
 case 2:
  $im = @imagecreatefromjpeg($im);
  break;
 case 3:
  $im = @imagecreatefrompng($im);
  break;
 }
 
 $pic_width = imagesx($im);
 $pic_height = imagesy($im);
 $resizewidth_tag = false;
 $resizeheight_tag = false;
 if (($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight)) {
 if ($maxwidth && $pic_width > $maxwidth) {
  $widthratio = $maxwidth / $pic_width;
  $resizewidth_tag = true;
 }
 
 if ($maxheight && $pic_height > $maxheight) {
  $heightratio = $maxheight / $pic_height;
  $resizeheight_tag = true;
 }
 
 if ($resizewidth_tag && $resizeheight_tag) {
  if ($widthratio < $heightratio)
  $ratio = $widthratio;
  else
  $ratio = $heightratio;
 }
 
 
 if ($resizewidth_tag && !$resizeheight_tag)
  $ratio = $widthratio;
 if ($resizeheight_tag && !$resizewidth_tag)
  $ratio = $heightratio;
 $newwidth = $pic_width * $ratio;
 $newheight = $pic_height * $ratio;
 
 if (function_exists("imagecopyresampled")) {
  $newim = imagecreatetruecolor($newwidth, $newheight);
  imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
 } else {
  $newim = imagecreate($newwidth, $newheight);
  imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
 }
 
 imagejpeg($newim, $dest);
 imagedestroy($newim);
 } else {
 imagejpeg($im, $dest);
 }
}
 
/**
 * 图片压缩处理
 * @param string $sFile 源图片路径
 * @param int $iWidth 自定义图片宽度
 * @param int $iHeight 自定义图片高度
 * @return string 压缩后的图片路径
 */
function getThumb($sFile,$iWidth,$iHeight){
 //图片公共路径
 $public_path = '';
 //判断该图片是否存在
 if(!file_exists($public_path.$sFile)) return $sFile;
 //判断图片格式(图片文件后缀)
 $extend = explode("." , $sFile);
 $attach_fileext = strtolower($extend[count($extend) - 1]);
 if (!in_array($attach_fileext, array('jpg','png','jpeg'))){
 return '';
 }
 //压缩图片文件名称
 $sFileNameS = str_replace(".".$attach_fileext, "_".$iWidth.'_'.$iHeight.'.'.$attach_fileext, $sFile);
 //判断是否已压缩图片,若是则返回压缩图片路径
 if(file_exists($public_path.$sFileNameS)){
 return $sFileNameS;
 }
 
 //生成压缩图片,并存储到原图同路径下
 resizeImage($public_path.$sFile, $public_path.$sFileNameS, $iWidth, $iHeight);
 if(!file_exists($public_path.$sFileNameS)){
 return $sFile;
 }
 return $sFileNameS;
}

使用实例:

//原图 img/img.jpg
//生成压缩图 img/img_300_300.jpg
getThumb('img/img.jpg',300,300);

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

PHP 相关文章推荐
AJAX的跨域访问-两种有效的解决方法介绍
Jun 22 PHP
PHP set_error_handler()函数使用详解(示例)
Nov 12 PHP
smarty模板中拼接字符串的方法
Feb 14 PHP
PHP FATAL ERROR: CALL TO UNDEFINED FUNCTION BCMUL()解决办法
May 04 PHP
php实现在服务器上创建目录的方法
Mar 16 PHP
Laravel 5 框架入门(一)
Apr 09 PHP
php基础教程
Aug 26 PHP
最新版本PHP 7 vs HHVM 多角度比较
Feb 14 PHP
基于jQueryUI和Corethink实现百度的搜索提示功能
Nov 09 PHP
php中html_entity_decode实现HTML实体转义
Jun 13 PHP
PHP面向对象程序设计重载(overloading)操作详解
Jun 13 PHP
PHP开发api接口安全验证操作实例详解
Mar 26 PHP
PHP获取数据库表中的数据插入新的表再原删除数据方法
Oct 12 #PHP
thinkphp5.0整合phpsocketio完整攻略(绕坑)
Oct 12 #PHP
PHP解析url并得到url参数方法总结
Oct 11 #PHP
详细对比php中类继承和接口继承
Oct 11 #PHP
PHP JWT初识及其简单示例
Oct 10 #PHP
php-fpm.conf配置文件中文说明详解及重要参数说明
Oct 10 #PHP
php实现单笔转账到支付宝功能
Oct 09 #PHP
You might like
php intval的测试代码发现问题
2008/07/27 PHP
关于PHP5 Session生命周期介绍
2010/03/02 PHP
分享一下贝贝成长进度的php代码
2012/09/14 PHP
PHP实现向关联数组指定的Key之前插入元素的方法
2017/06/06 PHP
utf-8编码引起js输出中文乱码的解决办法
2010/06/23 Javascript
Jquery.LazyLoad.js修正版下载,实现图片延迟加载插件
2011/03/12 Javascript
javaScript实现浮点数转十六进制字符
2013/10/29 Javascript
js实现特定位取反原理及示例
2014/06/30 Javascript
快速掌握Node.js环境的安装与运行方法
2016/02/16 Javascript
jquery trigger实现联动的方法
2016/02/29 Javascript
判断JS对象是否拥有某属性的方法推荐
2016/05/12 Javascript
关于 jQuery Easyui异步加载tree的问题解析
2016/12/06 Javascript
基于js中style.width与offsetWidth的区别(详解)
2017/11/12 Javascript
在Vue中获取组件声明时的name属性方法
2018/09/12 Javascript
vue项目环境变量配置的实现方法
2018/10/12 Javascript
Python 文件重命名工具代码
2009/07/26 Python
python实现斐波那契递归函数的方法
2014/09/08 Python
详解python进行mp3格式判断
2016/12/23 Python
python实现图片文件批量重命名
2020/03/23 Python
python for循环输入一个矩阵的实例
2018/11/14 Python
python绘制地震散点图
2019/06/18 Python
Python爬虫使用浏览器cookies:browsercookie过程解析
2019/10/22 Python
pandas 中对特征进行硬编码和onehot编码的实现
2019/12/20 Python
python用WxPython库实现无边框窗体和透明窗体实现方法详解
2020/02/21 Python
解决python多线程报错:AttributeError: Can't pickle local object问题
2020/04/08 Python
Vs Code中8个好用的python 扩展插件
2020/10/12 Python
Python 远程开关机的方法
2020/11/18 Python
巧用HTML5给按钮背景设计不同的动画简单实例
2016/08/09 HTML / CSS
Darphin迪梵官网: 来自巴黎,植物和精油调制的护肤品牌
2016/10/11 全球购物
印尼值得信赖的在线交易网站:Bukalapak
2019/03/11 全球购物
党员公开承诺书和承诺事项
2014/03/25 职场文书
相亲活动方案
2014/08/26 职场文书
转让协议书范本
2014/09/13 职场文书
爱护公物主题班会
2015/08/17 职场文书
《比尾巴》教学反思
2016/02/24 职场文书
详解Go与PHP的语法对比
2021/05/29 PHP