From f1313a0014f25eaac072fadd3f03051a0a3246eb Mon Sep 17 00:00:00 2001 From: Jordan Lev Date: Sat, 6 May 2017 21:39:28 -0500 Subject: [PATCH 1/2] add function --- src/Grafika/EditorInterface.php | 14 +++++++++++++- src/Grafika/Gd/Editor.php | 24 ++++++++++++++++++++++++ src/Grafika/Imagick/Editor.php | 24 ++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/Grafika/EditorInterface.php b/src/Grafika/EditorInterface.php index e240942..9c69131 100644 --- a/src/Grafika/EditorInterface.php +++ b/src/Grafika/EditorInterface.php @@ -151,7 +151,7 @@ public function open( &$image, $imageFile ); * @param ImageInterface $image Instance of Image. * @param int $newWidth Width in pixels. * @param int $newHeight Height in pixels. - * @param string $mode Resize mode. Possible values: "exact", "exactHeight", "exactWidth", "fill", "fit". + * @param string $mode Resize mode. Possible values: "exact", "exactHeight", "exactWidth", "fill", "fit", "letterbox". * * @return EditorInterface An instance of Editor. */ @@ -210,6 +210,18 @@ public function resizeFill( &$image, $newWidth, $newHeight ); */ public function resizeFit( &$image, $newWidth, $newHeight ); + /** + * Resize an image to fit within the given width and height, then fill out the remaining space with a background color. The re-sized image + color fill will exactly match the given dimensions. Useful if you want to preserve the aspect ratio but also fill out an exact size. + * + * @param ImageInterface $image Instance of Image. + * @param int $newWidth Width in pixels. + * @param int $newHeight Width in pixels. + * @param Color $color The Color object containing the background color. + * + * @return EditorInterface An instance of Editor. + */ + public function resizeLetterbox( &$image, $newWidth, $newHeight, $color); + /** * Rotate an image counter-clockwise. * diff --git a/src/Grafika/Gd/Editor.php b/src/Grafika/Gd/Editor.php index 1fb29a5..b404392 100644 --- a/src/Grafika/Gd/Editor.php +++ b/src/Grafika/Gd/Editor.php @@ -665,6 +665,30 @@ public function resizeFit(&$image, $newWidth, $newHeight) return $this; } + /** + * Resize image to fit within the given width and height, then fill out the remaining space with a background color. + * + * @param Image $image + * @param int $newWidth Width in pixels. + * @param int $newHeight Height in pixels. + * @param Color $color The Color object containing the background color. + * + * @return Editor + */ + public function resizeLetterbox(&$image, $newWidth, $newHeight, $color) + { + + $canvas = Grafika::createBlankImage($newWidth, $newHeight); + $this->fill($canvas, $color); + + $this->resizeFit($image, $newWidth, $newHeight); + + $this->blend($canvas, $image, 'normal', 1, 'center'); + $image = $canvas; + + return $this; + } + /** * Rotate an image counter-clockwise. * diff --git a/src/Grafika/Imagick/Editor.php b/src/Grafika/Imagick/Editor.php index 36d698f..6df68c2 100644 --- a/src/Grafika/Imagick/Editor.php +++ b/src/Grafika/Imagick/Editor.php @@ -563,6 +563,30 @@ public function resizeFit(&$image, $newWidth, $newHeight) return $this; } + /** + * Resize image to fit within the given width and height, then fill out the remaining space with a background color. + * + * @param Image $image + * @param int $newWidth Width in pixels. + * @param int $newHeight Height in pixels. + * @param Color $color The Color object containing the background color. + * + * @return Editor + */ + public function resizeLetterbox(&$image, $newWidth, $newHeight, $color) + { + + $canvas = Grafika::createBlankImage($newWidth, $newHeight); + $this->fill($canvas, $color); + + $this->resizeFit($image, $newWidth, $newHeight); + + $this->blend($canvas, $image, 'normal', 1, 'center'); + $image = $canvas; + + return $this; + } + /** * Rotate an image counter-clockwise. * From 5debb04dc6c034991c8fc09146739e22a763c200 Mon Sep 17 00:00:00 2001 From: Jordan Lev Date: Sat, 6 May 2017 21:43:10 -0500 Subject: [PATCH 2/2] add letterbox mode to generic resize function --- src/Grafika/Gd/Editor.php | 5 ++++- src/Grafika/Imagick/Editor.php | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Grafika/Gd/Editor.php b/src/Grafika/Gd/Editor.php index b404392..a750e44 100644 --- a/src/Grafika/Gd/Editor.php +++ b/src/Grafika/Gd/Editor.php @@ -504,7 +504,7 @@ public function open(&$image, $imageFile){ * @param Image $image * @param int $newWidth Width in pixels. * @param int $newHeight Height in pixels. - * @param string $mode Resize mode. Possible values: "exact", "exactHeight", "exactWidth", "fill", "fit". + * @param string $mode Resize mode. Possible values: "exact", "exactHeight", "exactWidth", "fill", "fit", "letterbox". * * @return Editor * @throws \Exception @@ -533,6 +533,9 @@ public function resize(&$image, $newWidth, $newHeight, $mode = 'fit') case 'fit': $this->resizeFit($image, $newWidth, $newHeight); break; + case 'letterbox': + $this->resizeLetterbox($image, $newWidth, $newHeight, new Color('#FFFFFF')); + break; default: throw new \Exception(sprintf('Invalid resize mode "%s".', $mode)); } diff --git a/src/Grafika/Imagick/Editor.php b/src/Grafika/Imagick/Editor.php index 6df68c2..985517a 100644 --- a/src/Grafika/Imagick/Editor.php +++ b/src/Grafika/Imagick/Editor.php @@ -402,7 +402,7 @@ public function open(&$image, $imageFile){ * @param Image $image * @param int $newWidth Width in pixels. * @param int $newHeight Height in pixels. - * @param string $mode Resize mode. Possible values: "exact", "exactHeight", "exactWidth", "fill", "fit". + * @param string $mode Resize mode. Possible values: "exact", "exactHeight", "exactWidth", "fill", "fit", "letterbox". * * @return Editor * @throws \Exception @@ -431,6 +431,9 @@ public function resize(&$image, $newWidth, $newHeight, $mode = 'fit') case 'fit': $this->resizeFit($image, $newWidth, $newHeight); break; + case 'letterbox': + $this->resizeLetterbox($image, $newWidth, $newHeight, new Color('#FFFFFF')); + break; default: throw new \Exception(sprintf('Invalid resize mode "%s".', $mode)); }