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 相关文章推荐
《PHP边学边教》(02.Apache+PHP环境配置――下篇)
Dec 13 PHP
PHP 金额数字转换成英文
May 06 PHP
ecshop 批量上传(加入自定义属性)
Mar 20 PHP
php正则取img标记中任意属性(正则替换去掉或改变图片img标记中的任意属性)
Aug 13 PHP
PHP图片自动裁切应付不同尺寸的显示
Oct 16 PHP
php的curl封装类用法实例
Nov 07 PHP
thinkPHP3.1验证码的简单实现方法
Apr 22 PHP
Yii+MYSQL锁表防止并发情况下重复数据的方法
Jul 14 PHP
thinkphp整合微信支付代码分享
Nov 24 PHP
PHP全功能无变形图片裁剪操作类与用法示例
Jan 10 PHP
laravel 5.4 + vue + vux + element的环境搭配过程介绍
Apr 26 PHP
为Plesk PHP7启用Oracle OCI8扩展方法总结
Mar 29 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
咖啡的传说和历史
2021/03/03 新手入门
PHP4和PHP5性能测试和对比 测试代码与环境
2007/08/17 PHP
PHP实现搜索相似图片
2015/09/22 PHP
php封装的page分页类完整实例
2016/10/18 PHP
静态的动态续篇之来点XML
2006/08/15 Javascript
jQuery 图像裁剪插件Jcrop的简单使用
2009/05/22 Javascript
动态样式类封装JS代码
2009/09/02 Javascript
JS将表单导出成EXCEL的实例代码
2013/11/11 Javascript
javascript 获取元素样式必杀技
2014/05/04 Javascript
使用jQuery的attr方法来修改onclick值
2014/07/07 Javascript
jQuery获得子元素个数的方法
2015/04/14 Javascript
后端接收不到AngularJs中$http.post发送的数据原因分析及解决办法
2016/07/05 Javascript
利用ES6语法重构React组件详解
2017/03/02 Javascript
Vue Spa切换页面时更改标题的实例代码
2017/07/15 Javascript
JavaScript 基础表单验证示例(纯Js实现)
2017/07/20 Javascript
JS eval代码快速解密实例解析
2020/04/23 Javascript
使用Python的Scrapy框架编写web爬虫的简单示例
2015/04/17 Python
Python输出由1,2,3,4组成的互不相同且无重复的三位数
2018/02/01 Python
python实现自主查询实时天气
2018/06/22 Python
python多线程下信号处理程序示例
2019/05/31 Python
Python为何不能用可变对象作为默认参数的值
2019/07/01 Python
使用 python pyautogui实现鼠标键盘控制功能
2019/08/04 Python
关于python字符串方法分类详解
2019/08/20 Python
python字符串替换re.sub()实例解析
2020/02/09 Python
sklearn和keras的数据切分与交叉验证的实例详解
2020/06/19 Python
Python同时处理多个异常的方法
2020/07/28 Python
HTML5+CSS3实现拖放(Drag and Drop)示例
2014/07/07 HTML / CSS
HTML5 WebSocket实现点对点聊天的示例代码
2018/01/31 HTML / CSS
适合各种场合的美食礼品:Harry & David
2016/08/03 全球购物
英国安全产品购物网站:The Safe Shop
2017/03/20 全球购物
Intimissimi德国网上商店:意大利知名内衣品牌
2018/04/03 全球购物
俄罗斯女装店:12storeez
2019/10/25 全球购物
多媒体编辑专业毕业生推荐信
2013/11/05 职场文书
学校安全工作制度
2014/01/19 职场文书
团员个人年度总结
2015/02/26 职场文书
Nginx反向代理多个服务器的实现方法
2021/03/31 Servers