php实现图片压缩处理


Posted in PHP onSeptember 09, 2020

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

说明

在项目中,经常会遇到在前端页面展示用户自己上传的图片。当部分图片尺寸过大,页面图片过多的情况下(如论坛里需要显示用户头像),会引起页面加载缓慢的问题。由于用户图片已存储导数据库,无法改变库里的图片大小,只能在获取图片路径时,压缩图片

示例

以下函数为图片压缩方法

/**
 * 图片压缩处理
 * @param string $sFile 图片路径
 * @param int $iWidth 自定义图片宽度
 * @param int $iHeight 自定义图片高度
 */
function getThumb($sFile,$iWidth,$iHeight){
  //判断该图片是否存在
  if(!file_exists(public_path().$sFile)) return $sFile;
  //判断图片格式
  $attach_fileext = get_filetype($sFile);
  if (!in_array($attach_fileext, array('jpg','png','jpeg'))){
    return $sFile;
  }
  //压缩图片
  $sFileNameS = str_replace(".".$attach_fileext, "_".$iWidth.'_'.$iHeight.'.'.$attach_fileext, $sFile);
  //判断是否已压缩图片,若是则返回压缩图片路径
  if(file_exists(public_path().$sFileNameS)){
    return $sFileNameS;
  }
  //解决手机端上传图片被旋转问题
  if (in_array($attach_fileext, array('jpeg')) ){
    adjustPicOrientation(public_path().$sFile);
  }
  //生成压缩图片,并存储到原图同路径下
  resizeImage(public_path().$sFile, public_path().$sFileNameS, $iWidth, $iHeight);
  if(!file_exists(public_path().$sFileNameS)){
    return $sFile;
  }
  return $sFileNameS;
}

/**
 *获取文件后缀名
 */
function get_filetype($filename) {
  $extend = explode("." , $filename);
  return strtolower($extend[count($extend) - 1]);
}

/**
 * 解决手机上传图片被旋转问题
 * @param string $full_filename 文件路径
 */
function adjustPicOrientation($full_filename){
  $exif = exif_read_data($full_filename);
  if($exif && isset($exif['Orientation'])) {
    $orientation = $exif['Orientation'];
    if($orientation != 1){
      $img = imagecreatefromjpeg($full_filename);

      $mirror = false;
      $deg  = 0;

      switch ($orientation) {
        case 2:
          $mirror = true;
          break;
        case 3:
          $deg = 180;
          break;
        case 4:
          $deg = 180;
          $mirror = true;
          break;
        case 5:
          $deg = 270;
          $mirror = true;
          break;
        case 6:
          $deg = 270;
          break;
        case 7:
          $deg = 90;
          $mirror = true;
          break;
        case 8:
          $deg = 90;
          break;
      }
      if ($deg) $img = imagerotate($img, $deg, 0);
      if ($mirror) $img = _mirrorImage($img);
      //$full_filename = str_replace('.jpg', "-O$orientation.jpg", $full_filename);新文件名
      imagejpeg($img, $full_filename, 95);
    }
  }
  return $full_filename;
}

resizeImage(public_path().$sFile, public_path().$sFileNameS, $iWidth, $iHeight);

/**
 * 生成图片
 * @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);
  }
}

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

PHP 相关文章推荐
用函数读出数据表内容放入二维数组
Oct 09 PHP
关于Iframe如何跨域访问Cookie和Session的解决方法
Apr 15 PHP
8个必备的PHP功能实例代码
Oct 27 PHP
ioncube_loader_win_5.2.dll的错误解决方法
Jan 04 PHP
PHP安全上传图片的方法
Mar 21 PHP
PHP文件操作方法汇总
Jul 01 PHP
php获取本机真实IP地址实例代码
Mar 31 PHP
分享PHP-pcntl 实现多进程代码
Sep 30 PHP
php自定义截取中文字符串-utf8版
Feb 27 PHP
php中通过eval实现字符串格式的计算公式
Mar 18 PHP
php 猴子摘桃的算法
Jun 20 PHP
PHP实现的日历功能示例
Sep 01 PHP
如何在PHP中读写文件
Sep 07 #PHP
PHP延迟静态绑定使用方法实例解析
Sep 05 #PHP
PHP autoload使用方法及步骤详解
Sep 05 #PHP
PHP数组访问常用方法解析
Sep 05 #PHP
XAMPP升级PHP版本实现步骤解析
Sep 04 #PHP
php使用Swoole实现毫秒级定时任务的方法
Sep 04 #PHP
Laravel Reponse响应客户端示例详解
Sep 03 #PHP
You might like
php创建基本身份认证站点的方法详解
2013/06/08 PHP
浅析php过滤html字符串,防止SQL注入的方法
2013/07/02 PHP
通过table标签,PHP输出EXCEL的实现方法
2013/07/24 PHP
php缓存技术详细总结
2013/08/07 PHP
php实现指定字符串中查找子字符串的方法
2015/03/17 PHP
php自定义函数实现JS的escape的方法示例
2016/07/07 PHP
php实现每日签到功能
2018/11/29 PHP
定位地理位置PHP判断员工打卡签到经纬度是否在打卡之内
2019/05/23 PHP
PHP pthreads v3下同步处理synchronized用法示例
2020/02/21 PHP
prototype 中文参数乱码解决方案
2009/11/09 Javascript
js和jquery批量绑定事件传参数一(新猪猪原创)
2010/06/23 Javascript
javascript-表格排序(降序/反序)实现介绍(附图)
2013/05/30 Javascript
jquery验证表单中的单选与多选实例
2013/08/18 Javascript
jquery配合css简单实现返回顶部效果
2013/09/30 Javascript
选择复选框按钮置灰否则按钮可用
2014/05/22 Javascript
轻松创建nodejs服务器(1):一个简单nodejs服务器例子
2014/12/18 NodeJs
jquery文档操作wrap()方法实例简述
2015/01/10 Javascript
jquery实现textarea输入框限制字数的方法
2015/01/15 Javascript
js实现仿Windows风格选项卡和按钮效果实例
2015/05/13 Javascript
JavaScript设置表单上传时文件个数的方法
2015/08/11 Javascript
网页中右键功能的实现方法之contextMenu的使用
2017/02/20 Javascript
vue2实现移动端上传、预览、压缩图片解决拍照旋转问题
2017/04/13 Javascript
基于nodejs+express4.X实现文件下载的实例代码
2017/07/13 NodeJs
Typescript的三种运行方式(小结)
2019/09/18 Javascript
Python实现七彩蟒蛇绘制实例代码
2018/01/16 Python
Python流程控制常用工具详解
2020/02/24 Python
Django DRF路由与扩展功能的实现
2020/06/03 Python
python实现企业微信定时发送文本消息的示例代码
2020/11/24 Python
python Zmail模块简介与使用示例
2020/12/19 Python
css3+jq创作含苞待放的荷花
2014/02/20 HTML / CSS
HUGO BOSS美国官方网上商店:世界知名奢侈品牌
2017/08/04 全球购物
过滤器的用法
2013/10/08 面试题
建筑专业自荐信范文
2014/01/05 职场文书
撤诉书怎么写
2015/05/19 职场文书
golang判断key是否在map中的代码
2021/04/24 Golang
解析redis hash应用场景和常用命令
2021/08/04 Redis