Looking to resize images to set dimensions of (1080×1350) without cropping, just fill the dead space with black boarders/buffers.
As we can’t control what size and aspect ratios users uploads images. Im looking to create a php function that creates a ‘canvas’ that has set dimensions of 1080×1350.
I then place the users image in the middle this ‘canvas’ (without cropping) and fill any dead space with black boards.
Here’s what I’ve developed so far:
// orignal image size
$width = imageSX($image);
$height = imageSY($image);
// canvas size
$thumb_width = 1080;
$thumb_height = 1350;
$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;
if ( $original_aspect >= $thumb_aspect ) {
$new_height = $thumb_height;
$new_width = $width / ($height / $thumb_height);
} else {
$new_width = $thumb_width;
$new_height = $height / ($width / $thumb_width);
}
$tmp = imagecreatetruecolor( $thumb_width, $thumb_height );
imagealphablending($tmp, false);
imagesavealpha($tmp, true);
$transparent = imagecolorallocatealpha($tmp, 255, 255, 255, 127);
imagefilledrectangle($tmp, 0, 0, round($new_width), round($new_height), $transparent);
// Resize and crop
imagecopyresampled($tmp, $image, round(0 - ($new_width - $thumb_width) / 2), round(0 - ($new_height - $thumb_height) / 2), 0, 0, round($new_width), round($new_height), round($width), round($height));
what changes would I need to achieve the expected results. I think i’m close, but just would like to bounce some ideas around.
Thank you.