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

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

 更新時間:2013年06月20日 16:34:33   作者:  
本篇文章是對PHP服務(wù)端圖片生成縮略圖的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
復(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);
//錯誤尺寸
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);
?>

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

相關(guān)文章

最新評論

两当县| 苍山县| 林口县| 根河市| 乐都县| 阿拉善左旗| 同德县| 大连市| 瑞金市| 南丹县| 筠连县| 中宁县| 宁河县| 黑龙江省| 昌都县| 铜陵市| 三穗县| 宕昌县| 阿克苏市| 监利县| 万年县| 嫩江县| 乌海市| 神池县| 永登县| 桐梓县| 三亚市| 宁陵县| 朝阳市| 阜新| 海原县| 富源县| 上饶县| 扎赉特旗| 车致| 沐川县| 嘉善县| 榆中县| 泰宁县| 葵青区| 周口市|