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中判断一个字符串包含另一个字符串的方法
Mar 19 PHP
php at(@)符号的用法简介
Jul 11 PHP
第七章 php自定义函数实现代码
Dec 30 PHP
PHPMailer的主要功能特点和简单使用说明
Feb 17 PHP
Destoon旺旺无法正常显示,点击提示“会员名不存在”的解决办法
Jun 21 PHP
php上传图片之时间戳命名(保存路径)
Aug 15 PHP
19个Android常用工具类汇总
Dec 30 PHP
php购物车实现方法
Jan 03 PHP
php中array_multisort对多维数组排序的方法
Jun 21 PHP
分享微信扫码支付开发遇到问题及解决方案-附Ecshop微信支付插件
Aug 23 PHP
基于php编程规范(详解)
Aug 17 PHP
php设计模式之模板模式实例分析【星际争霸游戏案例】
Mar 24 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+.htaccess实现全站静态HTML文件GZIP压缩传输(一)
2007/02/15 PHP
php通过ajax实现双击table修改内容
2014/04/28 PHP
使用PHPMailer实现邮件发送代码分享
2014/10/23 PHP
PHP MYSQL实现登陆和模糊查询两大功能
2016/02/05 PHP
ThinkPHP打水印及设置水印位置的方法
2016/10/14 PHP
php接口技术实例详解
2016/12/07 PHP
PHP iconv()函数字符编码转换的问题讲解
2019/03/22 PHP
laravel 获取当前url的别名方法
2019/10/11 PHP
javascript 网页跳转的方法
2008/12/24 Javascript
js 内存释放问题
2010/04/25 Javascript
javascript利用apply和arguments复用方法
2013/11/25 Javascript
AngularJS iframe跨域打开内容时报错误的解决办法
2015/01/26 Javascript
vue组件watch属性实例讲解
2017/11/07 Javascript
vue组件父与子通信详解(一)
2017/11/07 Javascript
vue短信验证性能优化如何写入localstorage中
2018/04/25 Javascript
解决Vue在封装了Axios后手动刷新页面拦截器无效的问题
2018/11/08 Javascript
Javascript实现一朵从含苞到绽放的玫瑰
2019/03/30 Javascript
js实现移动端tab切换时下划线滑动效果
2019/09/08 Javascript
使用layui的layer组件做弹出层的例子
2019/09/27 Javascript
vue 组件内获取actions的response方式
2019/11/08 Javascript
ES2020 已定稿,真实场景案例分析
2020/05/25 Javascript
跟老齐学Python之集合的关系
2014/09/24 Python
Python 查看文件的编码格式方法
2017/12/21 Python
python自动12306抢票软件实现代码
2018/02/24 Python
Python第三方Window模块文件的几种安装方法
2018/11/22 Python
python把1变成01的步骤总结
2019/02/27 Python
前端制作动画的几种方式(css3,js)
2016/12/12 HTML / CSS
CSS3圆角和渐变2种常用功能详解
2016/01/06 HTML / CSS
UGG雪地靴德国官网:UGG德国
2016/11/19 全球购物
女儿十岁生日答谢词
2014/01/27 职场文书
国际贸易专业求职信
2014/06/04 职场文书
幼儿园秋季开学通知
2015/07/16 职场文书
运动会1000米加油稿
2015/07/21 职场文书
忠诚教育学习心得体会
2016/01/23 职场文书
Go 中的空白标识符下划线
2022/03/25 Golang
vue特效之翻牌动画
2022/04/20 Vue.js