-
Notifications
You must be signed in to change notification settings - Fork 379
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 Resize and RelativeResize filters #37
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
this seems like a really weird way to configure the filter. What if the user sets several entries in the option array ?
another issue is that it introduces a limitation in how it can be used from Twig: hash keys cannot be set dynamically with variables so it is impossible to set the method according to a parameter (which could be useful when making it configurable). Why not using
array('method' => $method, 'parameter' => $parameter)
?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.
I didn't consider how filter parameters are defined for LiipImaigneBundle. For the original bundle, I have the following in my yaml configuration:
Separate
method
andparameter
keys there would have been superfluous. Only one method can specified at a time, and they each have a single parameter. On the subject of combining multiple operations, I think @lsmith told me that would be possible with a filter chain he had in this bundle.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.
Ok, after reading through this bundle's documentation, I still don't see any examples of configuring these filters in Twig. I just see them setup in YAML and then referred to in Twig by name. Can you point me towards an example?
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.
ah, options come from the config, not from the Twig function. Missed this point.
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.
So we're all good here? :)
The
foreach
syntax was just some shorthand to avoid callingkey()
andcurrent)
. I'm definitely not using it for iteration.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.
yeah, but this is quite confusing
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.
@lsmith77: What's your opinion here. Do you want me to use
key()
andcurrent()
or perhaps make the configuration more verbose so that method and parameter must be specified by their own keys?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.
hmm yeah .. i guess
key()
andcurrent()
would indeed be better. could you send another PR with that change .. but more importantly could you fix the README? i already added some docs there, but they are incomplete.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.
Will do. I completely dropped the ball on porting over my README docs, sorry.