1. 程式人生 > 程式設計 >php實現圖片壓縮處理

php實現圖片壓縮處理

本文例項為大家分享了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,$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,$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,$newwidth,$newheight,$pic_width,$pic_height);
    } else {
      $newim = imagecreate($newwidth,$newheight);
      imagecopyresized($newim,$pic_height);
    }

    imagejpeg($newim,$dest);
    imagedestroy($newim);
  } else {
    imagejpeg($im,$dest);
  }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。