Permalink
Browse files

extending support for almost all Imagine basic filters

  • Loading branch information...
1 parent 29c5338 commit 9136a8093a14c8fef3811ef3ddd2a4b6539519b1 @inmarelibero committed May 6, 2012
@@ -41,20 +41,24 @@ public function get($filter)
'Filter type for "%s" image filter must be specified', $filter
));
}
-
+
+ /*
if (!isset($this->loaders[$options['type']])) {
throw new InvalidArgumentException(sprintf(
'Could not find loader for "%s" filter type', $options['type']
));
}
-
+ */
+ /*
if (!isset($options['options'])) {
throw new InvalidArgumentException(sprintf(
'Options for filter type "%s" must be specified', $filter
));
}
+ */
- return $this->loaders[$options['type']]->load($options['options']);
+ //return $this->loaders[$options['type']]->load($options['options']);
+ return $this->loaders['basicfilters']->load($options);
inmarelibero
inmarelibero May 6, 2012 Owner

to allow extension with custom filter loaders one solution could be this:

$basicFilters = array('crop', 'resize', 'rotate', 'filpHorizontally', 'flipVertically', 'thumbnail');

if (in_array($options['type'], $basicFilters) {
    return $this->loaders['basicfilters']->load($options);
} else {
    return $this->loaders[$options['type']]->load($options['options']);
}

would be sufficient?

}
public function getOption($filter, $name, $default = null) {
@@ -0,0 +1,88 @@
+<?php
+
+namespace Avalanche\Bundle\ImagineBundle\Imagine\Filter\Loader;
+
+use Imagine\Image\Box;
+use Imagine\Image\Point;
+use Imagine\Image\Color;
+use Imagine\Image\ManipulatorInterface;
+
+use Imagine\Exception\InvalidArgumentException;
+
+class BasicFiltersFilterLoader implements LoaderInterface
+{
+ public function load(array $options = array())
+ {
+ $type = $options['type'];
+
+ $options = (isset($options['options']) ? $options['options'] : array());
+
+ if ('crop' === $type) {
+
+ if (!isset($options['start'])) {
+ throw new InvalidArgumentException(sprintf(
+ 'Option "%s" for filter type "%s" must be specified', 'start', $type
+ ));
+ }
+
+ if (!isset($options['size'])) {
+ throw new InvalidArgumentException(sprintf(
+ 'Option "%s" for filter type "%s" must be specified', 'size', $type
+ ));
+ }
+
+ list($width, $height) = $options['size'];
+ list($start_x, $start_y) = $options['start'];
+ return new \Imagine\Filter\Basic\Crop(new Point($start_x, $start_y), new Box($width, $height));
+
+ } elseif ('resize' === $type) {
+
+ if (!isset($options['size'])) {
+ throw new InvalidArgumentException(sprintf(
+ 'Option "%s" for filter type "%s" must be specified', 'size', $type
+ ));
+ }
+
+ list($width, $height) = $options['size'];
+ return new \Imagine\Filter\Basic\Resize(new Box($width, $height));
+
+ } elseif ('rotate' === $type) {
+
+ if (!isset($options['angle'])) {
+ throw new InvalidArgumentException(sprintf(
+ 'Option "%s" for filter type "%s" must be specified', 'angle', $type
+ ));
+ }
+ $angle = $options['angle'];
+ $background = $options['background'];
+
+ return new \Imagine\Filter\Basic\Rotate($angle, new Color($background));
+ } elseif ('flipHorizontally' === $type) {
+ return new \Imagine\Filter\Basic\flipHorizontally();
+ } elseif ('flipVertically' === $type) {
+ return new \Imagine\Filter\Basic\flipVertically();
+ } elseif ('thumbnail' === $type) {
+
+ if (!isset($options['mode'])) {
+ throw new InvalidArgumentException(sprintf(
+ 'Option "%s" for filter type "%s" must be specified', 'mode', $type
+ ));
+ }
+
+ if (!isset($options['size'])) {
+ throw new InvalidArgumentException(sprintf(
+ 'Option "%s" for filter type "%s" must be specified', 'size', $type
+ ));
+ }
+
+ $mode = ($options['mode'] === 'inset') ? ManipulatorInterface::THUMBNAIL_INSET : ManipulatorInterface::THUMBNAIL_OUTBOUND;
+
+ list($width, $height) = $options['size'];
+ return new \Imagine\Filter\Basic\Thumbnail(new Box($width, $height), $mode);
+ }
+
+ throw new InvalidArgumentException(sprintf(
+ 'Could not find filter for "%s" filter type', $type
+ ));
+ }
+}
@@ -1,21 +0,0 @@
-<?php
-
-namespace Avalanche\Bundle\ImagineBundle\Imagine\Filter\Loader;
-
-use Imagine\Image\Box;
-use Imagine\Image\ManipulatorInterface;
-use Imagine\Filter\Basic\Thumbnail;
-
-class ThumbnailFilterLoader implements LoaderInterface
-{
- public function load(array $options = array())
- {
- $mode = $options['mode'] === 'inset' ?
- ManipulatorInterface::THUMBNAIL_INSET :
- ManipulatorInterface::THUMBNAIL_OUTBOUND;
-
- list($width, $height) = $options['size'];
-
- return new Thumbnail(new Box($width, $height), $mode);
- }
-}
@@ -38,7 +38,7 @@
<!-- Filter loaders' classes -->
- <parameter key="imagine.filter.loader.thumbnail.class">Avalanche\Bundle\ImagineBundle\Imagine\Filter\Loader\ThumbnailFilterLoader</parameter>
+ <parameter key="imagine.filter.loader.basicfilters.class">Avalanche\Bundle\ImagineBundle\Imagine\Filter\Loader\BasicFiltersFilterLoader</parameter>
</parameters>
@@ -99,8 +99,8 @@
<!-- Filter loaders -->
- <service id="imagine.filter.loader.thumbnail" class="%imagine.filter.loader.thumbnail.class%">
- <tag name="imagine.filter.loader" filter="thumbnail" />
+ <service id="imagine.filter.loader.basicfilters" class="%imagine.filter.loader.basicfilters.class%">
+ <tag name="imagine.filter.loader" filter="basicfilters" />
</service>
</services>

0 comments on commit 9136a80

Please sign in to comment.