最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP實(shí)現(xiàn)圖片壓縮

 更新時(shí)間:2020年09月09日 13:39:20   作者:江南極客  
這篇文章主要為大家詳細(xì)介紹了PHP實(shí)現(xiàn)圖片壓縮,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

/**
 * 生成圖片
 * @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);
 }
}
 
/**
 * 圖片壓縮處理
 * @param string $sFile 源圖片路徑
 * @param int $iWidth 自定義圖片寬度
 * @param int $iHeight 自定義圖片高度
 * @return string 壓縮后的圖片路徑
 */
function getThumb($sFile,$iWidth,$iHeight){
 //圖片公共路徑
 $public_path = '';
 //判斷該圖片是否存在
 if(!file_exists($public_path.$sFile)) return $sFile;
 //判斷圖片格式(圖片文件后綴)
 $extend = explode("." , $sFile);
 $attach_fileext = strtolower($extend[count($extend) - 1]);
 if (!in_array($attach_fileext, array('jpg','png','jpeg'))){
 return '';
 }
 //壓縮圖片文件名稱
 $sFileNameS = str_replace(".".$attach_fileext, "_".$iWidth.'_'.$iHeight.'.'.$attach_fileext, $sFile);
 //判斷是否已壓縮圖片,若是則返回壓縮圖片路徑
 if(file_exists($public_path.$sFileNameS)){
 return $sFileNameS;
 }
 
 //生成壓縮圖片,并存儲(chǔ)到原圖同路徑下
 resizeImage($public_path.$sFile, $public_path.$sFileNameS, $iWidth, $iHeight);
 if(!file_exists($public_path.$sFileNameS)){
 return $sFile;
 }
 return $sFileNameS;
}

使用實(shí)例:

//原圖 img/img.jpg
//生成壓縮圖 img/img_300_300.jpg
getThumb('img/img.jpg',300,300);

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家,關(guān)注腳本之家公眾號(hào)的更多精彩內(nèi)容。

相關(guān)文章

最新評(píng)論

广宗县| 治多县| 深州市| 敦煌市| 无锡市| 偏关县| 黄骅市| 姜堰市| 杭锦后旗| 南丰县| 郎溪县| 洪湖市| 常州市| 卢龙县| 岗巴县| 谷城县| 柏乡县| 郴州市| 黄浦区| 嘉峪关市| 桑植县| 应用必备| 绿春县| 黄梅县| 明光市| 青海省| 闽清县| 从化市| 海晏县| 乐亭县| 合肥市| 百色市| 加查县| 永泰县| 大渡口区| 固阳县| 新建县| 黔西县| 宁海县| 应城市| 罗田县|