Im resizing an image to a 100 x 100 image, this is working great.
BUT..... I would like to keep the original aspect ratio. Therefore I need to determine whether the width or the height of the original image is larger and then fill the rest of the 100 x 100 image with transparency.
So if portrait, height would be 100 and to the right and left of the image would be transparency.
landscape, width would be 100 and to the top and bottom of the image would be transparency.
Is this possible?
here is my function
// 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;
}
}