-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from jmikola/resize-filters
Add Resize and RelativeResize filters
- Loading branch information
Showing
4 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Liip\ImagineBundle\Imagine\Filter\Loader; | ||
|
||
use Imagine\Exception\InvalidArgumentException; | ||
use Imagine\Image\ImageInterface; | ||
use Liip\ImagineBundle\Imagine\Filter\RelativeResize; | ||
|
||
/** | ||
* Loader for this bundle's relative resize filter. | ||
* | ||
* @author Jeremy Mikola <jmikola@gmail.com> | ||
*/ | ||
class RelativeResizeFilterLoader implements LoaderInterface | ||
{ | ||
/** | ||
* @see Liip\ImagineBundle\Imagine\Filter\Loader\LoaderInterface::load() | ||
*/ | ||
public function load(ImageInterface $image, array $options = array()) | ||
{ | ||
foreach ($options as $method => $parameter) { | ||
$filter = new RelativeResize($method, $parameter); | ||
|
||
return $filter->apply($image); | ||
} | ||
|
||
throw new InvalidArgumentException('Expected method/parameter pair, none given'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Liip\ImagineBundle\Imagine\Filter\Loader; | ||
|
||
use Imagine\Filter\Basic\Resize; | ||
use Imagine\Image\Box; | ||
|
||
/** | ||
* Loader for Imagine's basic resize filter. | ||
* | ||
* @author Jeremy Mikola <jmikola@gmail.com> | ||
*/ | ||
class ResizeFilterLoader implements LoaderInterface | ||
{ | ||
/** | ||
* @see Liip\ImagineBundle\Imagine\Filter\Loader\LoaderInterface::load() | ||
*/ | ||
public function load(ImageInterface $image, array $options = array()) | ||
{ | ||
list($width, $height) = $options['size']; | ||
|
||
$filter = new Resize(new Box($width, $height)); | ||
|
||
return $filter->apply($image); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Liip\ImagineBundle\Imagine\Filter; | ||
|
||
use Imagine\Exception\InvalidArgumentException; | ||
use Imagine\Filter\FilterInterface; | ||
use Imagine\Image\ImageInterface; | ||
use Imagine\Image\BoxInterface; | ||
|
||
/** | ||
* Filter for resizing an image relative to its existing dimensions. | ||
* | ||
* @author Jeremy Mikola <jmikola@gmail.com> | ||
*/ | ||
class RelativeResize implements FilterInterface | ||
{ | ||
private $method; | ||
private $parameter; | ||
|
||
/** | ||
* Constructs a RelativeResize filter with the given method and argument. | ||
* | ||
* @param string $method BoxInterface method | ||
* @param mixed $parameter Parameter for BoxInterface method | ||
*/ | ||
public function __construct($method, $parameter) | ||
{ | ||
if (!in_array($method, array('heighten', 'increase', 'scale', 'widen'))) { | ||
throw new InvalidArgumentException(sprintf('Unsupported method: ', $method)); | ||
} | ||
|
||
$this->method = $method; | ||
$this->parameter = $parameter; | ||
} | ||
|
||
/** | ||
* @see Imagine\Filter\FilterInterface::apply() | ||
*/ | ||
public function apply(ImageInterface $image) | ||
{ | ||
return $image->resize(call_user_func(array($image->getSize(), $this->method), $this->parameter)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters