基于PHP服务端图片生成缩略图的方法详解
程序员文章站
2023-04-07 18:26:42
复制代码 代码如下:
<?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 为此文件。。
复制代码 代码如下:
<?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 为此文件。。
上一篇: python 自动提交和抓取网页
下一篇: PHP 解决session死锁的方法