Posted in PHP onMarch 09, 2021
/*
$uploaded - 已上传的文件,可以理解为原图片
$uptype - 图片类型
$filename - 生成的缩略图的文件名(可包含路径)
$a_width - 缩略图宽度
$a_height - 缩略图高度
*/
function creat_thumb($uploaded,$uptype,$filename,$a_width,$a_height)
{
$im = '';
if($uptype == 'image/pjpeg' || $uptype == 'image/jpeg')
{
$im = imagecreatefromjpeg($uploaded);
}
else if($uptype == 'image/x-png' || $uptype == 'image/png')
{
$im = imagecreatefrompng($uploaded);
}
else if($uptype == 'image/gif')
{
$im = imagecreatefromgif($uploaded);
}
$width = imagesx($im);
$height = imagesy($im);
//确保原图比要生成的缩略图宽高要大
//计算宽高比例,哪个值大就按照哪个作为基准
//如果宽高相等,则忽略
if($width > $a_width || $height > $a_height)
{
if($width >= $height)
{
$newwidth = $a_width;
$newheight = ($height * $a_width) / $width;
$nx = 0;
$ny = 0;
}
else
{
$newheight = $a_height;
$newwidth = ($width * $a_height) / $height;
$nx = 0;
$ny = 0;
}
if(function_exists("imagecopyresampled"))
{
$newim = imagecreatetruecolor($newwidth, $newheight);
if($uptype == 'image/x-png' || $uptype == 'image/png')
{
$alpha = imagecolorallocatealpha($newim, 0, 0, 0, 127);
imagefill($newim, 0, 0, $alpha);
}
imagecopyresampled($newim, $im, 0, 0,$nx,$ny, $newwidth, $newheight, $width, $height);
}
else
{
$newim = imagecreate($newwidth, $newheight);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
if($uptype == 'image/x-png' || $uptype == 'image/png')
{
imagesavealpha($newim, true);
imagepng($newim,$filename);
}
else
{
imagejpeg($newim,$filename);
}
imagedestroy($newim);
}
}
PHP 实现缩略图
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@