From 1b919549ccb674e4f3ba9fea182bdb6cfcba82ce Mon Sep 17 00:00:00 2001 From: Andy Heathershaw Date: Fri, 9 Sep 2016 15:41:10 +0100 Subject: [PATCH] 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 --- app/Helpers/ImageHelper.php | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/app/Helpers/ImageHelper.php b/app/Helpers/ImageHelper.php index d4ade8e..3f18d5b 100644 --- a/app/Helpers/ImageHelper.php +++ b/app/Helpers/ImageHelper.php @@ -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; }