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 相关文章推荐
几款免费开源的不用数据库的php的cms
Dec 19 PHP
解析PHP处理换行符的问题 \r\n
Jun 13 PHP
解析array splice的移除数组中指定键的值,返回一个新的数组
Jul 02 PHP
PHP实现根据设备类型自动跳转相应页面的方法
Jul 24 PHP
laravel 4安装及入门图文教程
Oct 29 PHP
10个php函数实用却不常见
Oct 13 PHP
PHP5.2中PDO的简单使用方法
Mar 25 PHP
PHP的Yii框架中View视图的使用进阶
Mar 29 PHP
php实现的pdo公共类定义与用法示例
Jul 19 PHP
php curl上传、下载、https登陆实现代码
Jul 23 PHP
PHP使用栈解决约瑟夫环问题算法示例
Aug 27 PHP
PHP中quotemeta()函数的用法讲解
Apr 04 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
第四节--构造函数和析构函数
2006/11/16 PHP
php中用foreach来操作数组的代码
2011/07/17 PHP
php中防止伪造跨站请求的小招式
2011/09/02 PHP
PHP生成图片验证码、点击切换实例
2014/06/25 PHP
php读取文件内容到数组的方法
2015/03/16 PHP
PHP常用技巧汇总
2016/03/04 PHP
jQuery选中select控件 无法设置selected的解决方法
2010/09/01 Javascript
用JavaScript修改CSS属性的代码
2013/05/06 Javascript
jQuery中获取checkbox选中项等操作及注意事项
2013/11/24 Javascript
详解JavaScript中Date.UTC()方法的使用
2015/06/12 Javascript
基于JavaScript代码实现兼容各浏览器的设为首页和加入收藏
2016/01/07 Javascript
jQuery通过deferred对象管理ajax异步
2016/05/20 Javascript
jQuery实现响应鼠标事件的图片透明效果【附demo源码下载】
2016/06/16 Javascript
js数组去重的hash方法
2016/12/22 Javascript
解决nodejs中使用http请求返回值为html时乱码的问题
2017/02/18 NodeJs
基于Require.js使用方法(总结)
2017/10/26 Javascript
vue使用axios时关于this的指向问题详解
2017/12/22 Javascript
Vue 实现一个简单的鼠标拖拽滚动效果插件
2020/12/10 Vue.js
解决await在forEach中不起作用的问题
2021/02/25 Javascript
介绍Python中的一些高级编程技巧
2015/04/02 Python
python中实现精确的浮点数运算详解
2017/11/02 Python
机器学习的框架偏向于Python的13个原因
2017/12/07 Python
Python 判断 有向图 是否有环的实例讲解
2018/02/01 Python
python pandas中对Series数据进行轴向连接的实例
2018/06/08 Python
基于Python的OCR实现示例
2020/04/03 Python
什么是Python中的匿名函数
2020/06/02 Python
python利用tkinter实现图片格式转换的示例
2020/09/28 Python
html5时钟实现代码
2010/10/22 HTML / CSS
威尔逊皮革:Wilsons Leather
2018/12/07 全球购物
为什么要用EJB
2014/04/17 面试题
《值日生》教学反思
2014/02/17 职场文书
临床专业自荐信
2014/06/22 职场文书
我的中国梦演讲稿600字
2014/08/19 职场文书
求职自我推荐信
2015/03/24 职场文书
幼儿园安全工作总结2015
2015/04/20 职场文书
单位实习介绍信
2015/05/05 职场文书