Portrait image thumbnails are now generated the same dimensions as landscape but centred in the middle of the canvas to allow for the layout to be displayed uniformly on all screen sizes

This commit is contained in:
Andy Heathershaw 2016-09-09 15:41:10 +01:00
parent 3af7708933
commit 1b919549cc
1 changed files with 23 additions and 14 deletions

View File

@ -13,29 +13,37 @@ class ImageHelper
$thumbnailHeight = intval($thumbnailInfo['height']);
// If image is portrait, restrict the height by the relevant aspect ratio
$isPortrait = false;
if ($photo->height > $photo->width)
{
$aspectRatio = $photo->height / $photo->width;
$thumbnailWidth = intval($thumbnailInfo['height']) / $aspectRatio;
$thumbnailHeight = intval($thumbnailInfo['height']);
$isPortrait = true;
}
/*$thumbnailImageResource = imagecreatetruecolor($thumbnailWidth, $thumbnailHeight);
imagecopyresized(
$thumbnailImageResource,
$gdImageResource,
0,
0,
0,
0,
$thumbnailWidth,
$thumbnailHeight,
$photo->width,
$photo->height
);*/
$thumbnailImageResource = imagescale($gdImageResource, $thumbnailWidth, $thumbnailHeight, $this->getInterpolationMethod());
// If portrait, create a new canvas and paint the new picture in the centre
if ($isPortrait)
{
$newThumbnailImageResource = imagecreatetruecolor($thumbnailInfo['width'], $thumbnailInfo['height']);
// Fill in white so the space outside the portrait image is not black!
$whiteColour = imagecolorallocate($newThumbnailImageResource, 255, 255, 255);
imagefill($newThumbnailImageResource, 0, 0, $whiteColour);
imagecolordeallocate($newThumbnailImageResource, $whiteColour);
// Work out the centre position
// TODO: this may be worth implementing as a configuration option (e.g. portrait thumbnails: left-align, centre, right-align)
$leftX = ($thumbnailInfo['width'] - $thumbnailWidth) / 2;
imagecopy($newThumbnailImageResource, $thumbnailImageResource, $leftX, 0, 0, 0, $thumbnailWidth, $thumbnailHeight);
imagedestroy($thumbnailImageResource);
$thumbnailImageResource = $newThumbnailImageResource;
}
// TODO make the /tmp folder configurable
$tempName = tempnam('/tmp', 'btw_thumb_');
$tempNameWithExtension = ($tempName . '.jpg');
@ -43,6 +51,7 @@ class ImageHelper
// TODO make thumbnail quality configurable
imagejpeg($thumbnailImageResource, $tempNameWithExtension, 90);
imagedestroy($thumbnailImageResource);
return $tempNameWithExtension;
}