佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > php實(shí)現(xiàn)圖片壓縮處理

php實(shí)現(xiàn)圖片壓縮處理

熱門標(biāo)簽:團(tuán)購(gòu)網(wǎng)站 科大訊飛語(yǔ)音識(shí)別系統(tǒng) 服務(wù)器配置 Mysql連接數(shù)設(shè)置 銀行業(yè)務(wù) Linux服務(wù)器 阿里云 電子圍欄

本文實(shí)例為大家分享了php實(shí)現(xiàn)圖片壓縮處理的具體代碼,供大家參考,具體內(nèi)容如下

說(shuō)明

在項(xiàng)目中,經(jīng)常會(huì)遇到在前端頁(yè)面展示用戶自己上傳的圖片。當(dāng)部分圖片尺寸過(guò)大,頁(yè)面圖片過(guò)多的情況下(如論壇里需要顯示用戶頭像),會(huì)引起頁(yè)面加載緩慢的問(wèn)題。由于用戶圖片已存儲(chǔ)導(dǎo)數(shù)據(jù)庫(kù),無(wú)法改變庫(kù)里的圖片大小,只能在獲取圖片路徑時(shí),壓縮圖片

示例

以下函數(shù)為圖片壓縮方法

/**
 * 圖片壓縮處理
 * @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;
  }
  //解決手機(jī)端上傳圖片被旋轉(zhuǎn)問(wèn)題
  if (in_array($attach_fileext, array('jpeg')) ){
    adjustPicOrientation(public_path().$sFile);
  }
  //生成壓縮圖片,并存儲(chǔ)到原圖同路徑下
  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]);
}

/**
 * 解決手機(jī)上傳圖片被旋轉(zhuǎn)問(wèn)題
 * @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 目標(biāo)圖片路徑
 * @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);
  }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • PHP+ajax實(shí)現(xiàn)上傳、刪除、修改單張圖片及后臺(tái)處理邏輯操作詳解
  • php使用高斯算法實(shí)現(xiàn)圖片的模糊處理功能示例
  • php封裝的圖片(縮略圖)處理類完整實(shí)例
  • 分享php多功能圖片處理類
  • php常用圖片處理類
  • 功能強(qiáng)大的PHP圖片處理類(水印、透明度、旋轉(zhuǎn))
  • php+curl 發(fā)送圖片處理代碼分享
  • 常用的php圖片處理類(水印、等比縮放、固定高寬)分享
  • PHP 圖片處理

標(biāo)簽:廣元 衡水 衢州 大理 棗莊 江蘇 蚌埠 萍鄉(xiāng)

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《php實(shí)現(xiàn)圖片壓縮處理》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    武宣县| 磴口县| 青龙| 福安市| 资源县| 全椒县| 宿州市| 安庆市| 手游| 安龙县| 泰州市| 福贡县| 广德县| 紫阳县| 闻喜县| 永福县| 富源县| 乐东| 余姚市| 许昌县| 墨脱县| 开阳县| 新平| 绥阳县| 永川市| 凤山市| 石景山区| 阜阳市| 吴江市| 莱西市| 霍林郭勒市| 永城市| 韶山市| 睢宁县| 右玉县| 西贡区| 博罗县| 米泉市| 商丘市| 井研县| 高邑县|