网站首页> 后端开发> PHP> php实现图片压缩处理

php实现图片压缩处理

时间:2020-10-10 21:49:57 阅读:6589次 来源:互联网

本文实例为大家分享了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);
  }
}

本文地址:https://www.manongw.com/article/36.html

文章来源:转载于CSDN,转载网址为https://blog.csdn.net/kirsten_z/article/details/77256475

版权申明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 ezhongheng@126.com 举报,一经查实,本站将立刻删除。

相关文章
  • 本文主要介绍了深思 PHP 数组遍历的差异(array_diff 的实现)的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...
    2020-09-27 08:54
  • 本文主要介绍了PHP safe_mode开启对于PHP系统函数有什么影响的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...
    2020-11-10 16:48
  • 本文主要介绍了PHP重载基础知识回顾的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...
    2020-10-10 21:33
  • 本文主要介绍了PHP dirname功能及原理实例解析的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...
    2020-10-29 09:17
  • 本文主要介绍了PHP 获取13位时间戳的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...
    2020-09-27 10:33
  • 本文主要介绍了PHP 数组转字符串,与字符串转数组的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...
    2020-09-27 10:43
  • 本文主要介绍了PHP如何通过date() 函数格式化显示时间的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...
    2020-11-13 15:34
  • 本文主要介绍了PHP获取类私有属性的3种方法的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...
    2020-10-10 21:47
  • 本文主要介绍了PHP延迟静态绑定使用方法实例解析的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...
    2020-10-10 21:56
  • 本文主要介绍了php实现图片压缩处理的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...
    2020-10-10 21:49