您现在的位置是:群英 > 开发技术 > PHP语言
PHP如何实现图片压缩功能,实现方法是什么?
Admin发表于 2021-12-25 17:53:162396 次浏览

    这篇文章给大家分享的是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有帮助,想要了解更多可以继续浏览群英网络其他相关的文章。

文本转载自脚本之家

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

相关信息推荐
2022-06-02 17:17:36 
摘要:转换方法:1、使用“array_merge_recursive(数组1,数组2,数组3...)”语句将多个数组合并为一个数组;2、使用json_encode()将合并后的数组转为json数据,语法“json_encode(合并后的数组)”。
2022-09-03 17:55:13 
摘要:本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了正则表达式的相关问题,正则表达式是一个特定的格式化模式,用于验证各种字符串是否匹配这个特征,进而实现高级的文本查找、替换、截取内容等操作,希望对大家有帮助。
2022-09-16 17:55:36 
摘要:jquery是一个JavaScript函数库;jquery是一个轻量级的、快速、简洁的JavaScript库,jquery极大地简化了JavaScript编程,封装了JavaScript常用的功能代码,提供一种简便的JavaScript设计模式,优化HTML文档操作、事件处理、动画设计和Ajax交互。
云活动
推荐内容
热门关键词
热门信息
群英网络助力开启安全的云计算之旅
立即注册,领取新人大礼包
  • 联系我们
  • 24小时售后:4006784567
  • 24小时TEL :0668-2555666
  • 售前咨询TEL:400-678-4567

  • 官方微信

    官方微信
Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
微信公众号
返回顶部
返回顶部 返回顶部