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; }