PHP PHP的图像缩略图生成器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP PHP的图像缩略图生成器相关的知识,希望对你有一定的参考价值。
$upfile = $_REQUEST['p'];
$max_width = $_REQUEST["w"];
$max_height = $_REQUEST["h"];
$def_width = $_REQUEST["wh"];
$def_height = $_REQUEST["hw"];
if(empty($def_width) && empty($def_height)) {
$def_width = '245';
$def_height = '245';
}
if ($def_height=='') $def_height = $def_width;
//IMAGE PATH
$exportPng = false;
if(!file_exists($_SERVER['DOCUMENT_ROOT'] . $upfile)) $upfile = $_SERVER['DOCUMENT_ROOT'] . $upfile;
else {
$upfile = "img/nophoto.jpg";
$exportPng = true;
}
//since we already have predefined values of extension
$ext = strtolower(substr($upfile, -3));
//INCREASE MEMORY LIMIT WHEN WORKING WITH
ini_set('memory_limit', '64M');
//MAKE NEW IMAGE OUT OF EXTENSION
if ($ext=="gif") { $src = @ImageCreateFromGif($upfile); header("Content-Type: image/gif"); }
if ($ext=="jpg") { $src = @ImageCreateFromJpeg($upfile); header("Content-Type: image/jpeg"); }
if ($ext=="png") { $src = @ImageCreateFromPng($upfile); header("Content-Type: image/png"); }
$size = GetImageSize($upfile);
$width = $size[0];
$height = $size[1];
if ($def_width == '') {
// Proportionally resize the image to the max sizes
if (($max_width == '')&&($max_height == '')) {
$max_width = $width;
$max_height = $height;
}
if ($max_width == '') $max_width = ($max_height*$width) / $height;
if ($max_height == '') $max_height = ($max_width*$height) / $width;
if ($max_height > $max_width) $highter_dimension=$max_height;
else $highter_dimension = $max_width;
if ($width == 0) $x_ratio = $max_width;
else $x_ratio = $max_width / $width;
if ($height == 0) $y_ratio = $max_height;
else $y_ratio = $max_height / $height;
if( ($width <= $max_width) && ($height <= $max_height) ) {
$tn_width = $width;
$tn_height = $height;
} elseif (($x_ratio * $height) < $max_height) {
$tn_height = ceil($x_ratio * $height);
$tn_width = $max_width;
} else {
$tn_width = ceil($y_ratio * $width);
$tn_height = $max_height;
}
$dst = ImageCreateTrueColor($tn_width, $tn_height);
ImageCopyResized($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);
}
if($def_width != '') {
$highter_dimension = $def_width;
if ($def_width < $def_height) $highter_dimension = $def_height;
$factor_w = $width / $def_width;
$factor_h = $height / $def_height;
if ($factor_w > $factor_h) {
$new_height = floor($def_height * $factor_h);
$new_width = floor($def_width * $factor_h);
} else {
$new_height = floor($def_height * $factor_w);
$new_width = floor($def_width * $factor_w);
}
$src_x = ceil(($width - $new_width) / 2);
$src_y = ceil(($height - $new_height) / 2);
$dst = ImageCreateTrueColor($def_width, $def_height);
@ImageCopyResized($dst, $src, 0, 0, $src_x, $src_y, $def_width, $def_height, $new_width, $new_height);
}
if(!$exportPng) Header("Content-type: image/jpeg");
else Header("Content-type: image/png");
//OUTPUT INTO PNG TO GET HIGHER QUALITY IMAGE
if(!$exportPng) ImageJpeg($dst);
else ImagePng($dst);
@ImageDestroy($src);
@ImageDestroy($dst);
以上是关于PHP PHP的图像缩略图生成器的主要内容,如果未能解决你的问题,请参考以下文章
PHP 缩略图图像生成器缓存:如何在 PHP 中正确设置 If-Last-Modified/Max-Age/Last-Modified HEADERS?