-
Notifications
You must be signed in to change notification settings - Fork 378
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 filter to perform a relative resize, thus keeping the aspect ratio, and not exceeding a max width and a max height #206
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Liip\ImagineBundle\Imagine\Filter\Loader; | ||
|
||
use Imagine\Image\ImageInterface; | ||
|
||
use Liip\ImagineBundle\Imagine\Filter\Loader\LoaderInterface; | ||
use Liip\ImagineBundle\Imagine\Filter\TwoWayRelativeResize; | ||
|
||
/** | ||
* Loader for this bundle's two-way relative resize filter. | ||
* | ||
* @author Robert-Jan Bijl <[email protected]> | ||
*/ | ||
class TwoWayRelativeResizeFilterLoader implements LoaderInterface | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function load(ImageInterface $image, array $options = array()) | ||
{ | ||
$newHeight = isset($options['height']) ? $options['height'] : null; | ||
$newWidth = isset($options['width']) ? $options['width'] : null; | ||
|
||
// if no resize is given, just return the original image | ||
if (null === $newHeight && null === $newWidth) { | ||
return $image; | ||
} | ||
|
||
$filter = new TwoWayRelativeResize($newHeight, $newWidth); | ||
return $filter->apply($image); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
namespace Liip\ImagineBundle\Imagine\Filter; | ||
|
||
use Imagine\Filter\FilterInterface; | ||
use Imagine\Image\Box; | ||
use Imagine\Image\ImageInterface; | ||
|
||
/** | ||
* Filter for resizing an image, by keeping the aspect ratio and not exceeding the maxWidth and maxHeight parameters | ||
* | ||
* @author Robert-Jan Bijl <[email protected]> | ||
*/ | ||
class TwoWayRelativeResize implements FilterInterface | ||
{ | ||
/** | ||
* @var int | ||
*/ | ||
private $newWidth; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please make all |
||
|
||
/** | ||
* @var int | ||
*/ | ||
private $newHeight; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param int $newHeight | ||
* @param int $newWidth | ||
*/ | ||
public function __construct($newHeight, $newWidth) | ||
{ | ||
$this->newHeight = $newHeight; | ||
$this->newWidth = $newWidth; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function apply(ImageInterface $image) | ||
{ | ||
// if we don't have any new dimensions, return the original | ||
if (null === $this->newHeight && null === $this->newWidth) { | ||
return $image; | ||
} | ||
|
||
list($newWidth, $newHeight) = $this->calculateNewDimensions( | ||
$image->getSize()->getWidth(), | ||
$image->getSize()->getHeight(), | ||
$this->newWidth, | ||
$this->newHeight | ||
); | ||
|
||
// resize the image accordingly | ||
return $image->resize(new Box($newWidth, $newHeight)); | ||
} | ||
|
||
/** | ||
* Calculates new width and height for an image by keeping the aspect ratio, | ||
* not exceeding the maxWidth and maxHeight parameters | ||
* | ||
* @param int $width | ||
* @param int $height | ||
* @param int $maxWidth | ||
* @param int $maxHeight | ||
* @return array | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing empty line before |
||
*/ | ||
private function calculateNewDimensions($width, $height, $maxWidth, $maxHeight) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method should be In addition, this method has to be tested. |
||
{ | ||
$widthFactor = $maxWidth / $width; | ||
$heightFactor = $maxHeight / $height; | ||
|
||
// if one of the factors is 0, we need the other one. | ||
if ($widthFactor === 0) { | ||
$factor = $heightFactor; | ||
} elseif ($heightFactor === 0) { | ||
$factor = $widthFactor; | ||
} else { | ||
$factor = $widthFactor >= $heightFactor ? $heightFactor : $widthFactor; | ||
} | ||
|
||
// none of the new dimensions should be 0, so take care of that... | ||
// this could happen, for example, if we scale done an image with a width of 1 px | ||
$newWidth = max(array(intval($width * $factor), 1)); | ||
$newHeight = max(array(intval($height * $factor), 1)); | ||
|
||
return array($newWidth, $newHeight); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing empty line before
return
.