我将图像尺寸调整为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;
}
}