色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

基于PHP服務(wù)端圖片生成縮略圖的方法詳解

復(fù)制代碼 代碼如下:
<?php
//定義縮略圖片尺寸
$picSize = array(
              '100_100'=> 1,
              '200_100'=> 1
           );
$imagePath = "../image/";
function parseUrl($url){
   preg_match("/(?P<name>[/w/d]+)_w(?P<width>/d+)_h(?P<height>/d+)/.(?P<ext>/w+)/",$url,$match);
   return $match;
}
$urlArr = explode("/",$_SERVER['REQUEST_URI']);
$imgName = $urlArr[count($urlArr)-1];
$picInfo = parseUrl($imgName);
//錯(cuò)誤尺寸
if(empty($picInfo['width']) || empty($picInfo['height']) ||
!array_key_exists($picInfo['width'].'_'.$picInfo['height'],$picSize)) die('不存在該尺寸圖片');
$originalPic = $imagePath.$picInfo['name'].'/'.$picInfo['name'].'.'.$picInfo['ext'];
//原始圖不存在
if(!file_exists($originalPic)) die("圖片不存在!");
/**
 *等比例壓縮圖片
 */
switch($picInfo['ext']){
   case 'jpg':
      $orgImg = ImageCreateFromJpeg($originalPic);
      break;
   default:
      break;
}
$owidth  =  ImageSX($orgImg); //原始尺寸
$oheight =  ImageSY($orgImg);
$tW = $picInfo['width'];
$tH = $picInfo['height'];
//獲取縮略圖尺寸
if($owidth/$oheight > $tW/$tH){
    $tH = intval($tW * $oheight/$owidth);
}else{
     $tW = intval($tH * $owidth/$oheight);
}
//生成背景圖
$new_img = ImageCreateTrueColor($picInfo['width'], $picInfo['height']);
$bgColor = imagecolorallocate($new_img,255,255,255);
if (!@imagefilledrectangle($new_img, 0, 0, $picInfo['width']-1, $picInfo['height']-1, $bgColor)) {
    echo "無法創(chuàng)建背景圖";  //@todo記錄日志
    exit(0);
}
if (!@imagecopyresampled($new_img, $orgImg, ($picInfo['width']-$tW)/2, ($picInfo['height']-$tH)/2, 0, 0, $tW, $tH, $owidth, $oheight)) {
    echo "生成圖片失敗";
    exit(0);
}
//生成圖片
ob_start();
imagejpeg($new_img);
$_newImg = ob_get_contents();
ob_end_clean();
file_put_contents($imagePath.$picInfo['name']."/".$imgName, $_newImg);
header("Content-type:image/jpeg; charset=utf-8");
imagejpeg($new_img);
?>

使用時(shí)候綁定apache conf 的 documentError 404 的handler 為此文件。。

php技術(shù)基于PHP服務(wù)端圖片生成縮略圖的方法詳解,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 盐城市| 铁岭市| 图木舒克市| 察雅县| 基隆市| 余干县| 潍坊市| 新绛县| 文安县| 夏邑县| 沂水县| 电白县| 巴楚县| 大足县| 云浮市| 滦南县| 孝感市| 同德县| 衡水市| 温宿县| 汉沽区| 温宿县| 拉孜县| 昭平县| 五莲县| 东山县| 北京市| 汉寿县| 青冈县| 固原市| 陆良县| 专栏| 永安市| 紫金县| 八宿县| 玉门市| 夏河县| 延庆县| 安顺市| 西乡县| 平顶山市|