Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resizeLetterbox method #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/Grafika/EditorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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.
*
Expand Down
29 changes: 28 additions & 1 deletion src/Grafika/Gd/Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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.
*
Expand Down
29 changes: 28 additions & 1 deletion src/Grafika/Imagick/Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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.
*
Expand Down