我将图像大小调整为 100 x 100 图像,效果很好。
但是.....我想保持原来的纵横比。因此,我需要确定原始图像的宽度或高度是否更大,然后用透明度填充 100 x 100 图像的其余部分。
因此,如果纵向,高度将为 100,图像的右侧和左侧将是透明度。
横向,宽度将是 100,图像的顶部和底部将是透明的。
这可能吗?
这是我的功能
// This function will proportionally resize image
function resizeImage($CurWidth,$CurHeight,$MaxSize,$DestFolder,$SrcImage,$Quality,$ImageType)
{
//Check Image size is not 0
if($CurWidth <= 0 || $CurHeight <= 0)
{
return false;
}
//Check Image size is not 0
else if($CurWidth >= 1 && $CurWidth <= 100 || $CurHeight >= 1 && $CurHeight <= 100)
{
//Construct a proportional size of new image
$NewWidth = $CurWidth;
$NewHeight = $CurHeight;
$NewCanves = imagecreatetruecolor($NewWidth, $NewHeight);
}
else if($CurWidth >= 101 || $CurHeight >= 101)
{
//Construct a proportional size of new image
$ImageScale = min($MaxSize/$CurWidth, $MaxSize/$CurHeight);
$NewWidth = ceil($ImageScale*$CurWidth);
$NewHeight = ceil($ImageScale*$CurHeight);
$NewCanves = imagecreatetruecolor($NewWidth, $NewHeight);
}
/* fix PNG transparency issues */
imagefill($NewCanves, 0, 0, IMG_COLOR_TRANSPARENT);
imagesavealpha($NewCanves, true);
imagealphablending($NewCanves, true);
// Resize Image
if(imagecopyresampled($NewCanves, $SrcImage,0, 0, 0, 0, $NewWidth, $NewHeight, $CurWidth, $CurHeight))
{
switch(strtolower($ImageType))
{
case 'image/PNG':
case 'image/png':
imagepng($NewCanves,$DestFolder);
break;
case 'image/gif':
imagegif($NewCanves,$DestFolder);
break;
case 'image/JPG':
case 'image/JPEG':
case 'image/jpeg':
case 'image/pjpeg':
imagejpeg($NewCanves,$DestFolder,$Quality);
break;
default:
return false;
}
//Destroy image, frees memory
if(is_resource($NewCanves)) {imagedestroy($NewCanves);}
return true;
}
}