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..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)); } @@ -665,6 +668,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..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)); } @@ -563,6 +566,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. *