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.MVC的模板标签系统(三)
Sep 05 PHP
php实现的MySQL通用查询程序
Mar 11 PHP
深入解析php之apc
May 15 PHP
PHP判断表单复选框选中状态完整例子
Jun 24 PHP
php+ajax实现文章自动保存的方法
Dec 30 PHP
php实现比较两个文件夹异同的方法
Jun 18 PHP
Zend Framework教程之Zend_Config_Ini用法分析
Mar 23 PHP
PHP更安全的密码加密机制Bcrypt详解
Jun 18 PHP
php curl上传、下载、https登陆实现代码
Jul 23 PHP
PHP实现的回溯算法示例
Aug 15 PHP
php实现构建排除当前元素的乘积数组方法
Oct 06 PHP
php设计模式之适配器模式实例分析【星际争霸游戏案例】
Apr 07 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中的unset究竟会不会释放内存
2013/07/18 PHP
php获取汉字首字母的函数
2013/11/07 PHP
php中文乱码怎么办如何让浏览器自动识别utf-8
2014/01/15 PHP
PHP小教程之实现双向链表
2014/06/12 PHP
浅析PHP中strlen和mb_strlen的区别
2014/08/31 PHP
php实现的RSS生成类实例
2015/04/23 PHP
Thinkphp 中 distinct 的用法解析
2016/12/14 PHP
JavaScript的单例模式 (singleton in Javascript)
2010/06/11 Javascript
prettify 代码高亮着色器google出品
2010/12/28 Javascript
JS实现QQ图片一闪一闪的效果小例子
2013/07/31 Javascript
解析offsetHeight,clientHeight,scrollHeight之间的区别
2013/11/20 Javascript
jQuery前端分页示例分享
2015/02/10 Javascript
九种原生js动画效果
2015/11/11 Javascript
jquery插件bootstrapValidator数据验证详解
2016/11/09 Javascript
基于bootstrap实现收缩导航条
2017/03/17 Javascript
Angularjs cookie 操作实例详解
2017/09/27 Javascript
鸿蒙系统中的 JS 开发框架
2020/09/18 Javascript
举例讲解Python中的list列表数据结构用法
2016/03/12 Python
浅析Python编写函数装饰器
2016/03/18 Python
解决Python requests 报错方法集锦
2017/03/19 Python
Python操作MySQL数据库的三种方法总结
2018/01/30 Python
Python使用pickle模块报错EOFError Ran out of input的解决方法
2018/08/16 Python
Python对CSV、Excel、txt、dat文件的处理
2018/09/18 Python
Django 模型类(models.py)的定义详解
2019/07/19 Python
使用Pandas对数据进行筛选和排序的实现
2019/07/29 Python
如何使用Python破解ZIP或RAR压缩文件密码
2020/01/09 Python
selenium+python配置chrome浏览器的选项的实现
2020/03/18 Python
新西兰演唱会和体育门票网站:Ticketmaster新西兰
2017/10/07 全球购物
匈牙利墨盒和碳粉购买网站:CDRmarket
2018/04/14 全球购物
YBF Beauty官网:美丽挚友,美国知名彩妆品牌
2020/11/22 全球购物
联想智利官方网站:Lenovo Chile
2020/06/03 全球购物
教师自荐信
2013/12/10 职场文书
生日派对邀请函
2014/01/13 职场文书
win10清理dns缓存
2022/04/19 数码科技
MySQL的prepare使用以及遇到的bug
2022/05/11 MySQL
MySQL索引失效十种场景与优化方案
2023/05/08 MySQL