国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

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

2024-05-04 23:10:52
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:


<?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 "無法創建背景圖"; //@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 為此文件。。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乌海市| 乌鲁木齐市| 芒康县| 曲沃县| 宜兰市| 兴国县| 潜山县| 聂拉木县| 肇州县| 襄城县| 岗巴县| 漾濞| 阳高县| 盐山县| 吉木乃县| 兴海县| 大理市| 海兴县| 广南县| 灵寿县| 荆州市| 宜君县| 尼玛县| 通江县| 陕西省| 武胜县| 富宁县| 宣恩县| 犍为县| 安仁县| 昌邑市| 乌兰浩特市| 普兰店市| 平阳县| 孟村| 内乡县| 金堂县| 墨脱县| 云梦县| 潮安县| 渝中区|