From 44150548c9cfb3c5d6ff239319e361d8d7e116b5 Mon Sep 17 00:00:00 2001 From: Toni Uebernickel Date: Wed, 13 Feb 2013 17:34:33 +0100 Subject: [PATCH] add FilterManager::applyFilter --- Imagine/Filter/FilterManager.php | 39 +++++++++++++++++----- Tests/Imagine/Filter/FilterManagerTest.php | 35 +++++++++++++++++++ 2 files changed, 65 insertions(+), 9 deletions(-) diff --git a/Imagine/Filter/FilterManager.php b/Imagine/Filter/FilterManager.php index 9e9999216..a678aa72c 100644 --- a/Imagine/Filter/FilterManager.php +++ b/Imagine/Filter/FilterManager.php @@ -54,6 +54,8 @@ public function getFilterConfiguration() /** * Returns a response containing the given image after applying the given filter on it. * + * @uses FilterManager::applyFilterSet + * * @param Request $request * @param string $filter * @param ImageInterface $image @@ -65,15 +67,7 @@ public function get(Request $request, $filter, ImageInterface $image, $localPath { $config = $this->getFilterConfiguration()->get($filter); - foreach ($config['filters'] as $filter => $options) { - if (!isset($this->loaders[$filter])) { - throw new \InvalidArgumentException(sprintf( - 'Could not find filter loader for "%s" filter type', $filter - )); - } - - $image = $this->loaders[$filter]->load($image, $options); - } + $image = $this->applyFilter($image, $filter); if (empty($config['format'])) { $format = pathinfo($localPath, PATHINFO_EXTENSION); @@ -93,4 +87,31 @@ public function get(Request $request, $filter, ImageInterface $image, $localPath return new Response($image, 200, array('Content-Type' => $contentType)); } + + /** + * Apply the provided filter set on the given Image. + * + * @param ImageInterface $image + * @param string $filter + * + * @return ImageInterface + * + * @throws \InvalidArgumentException + */ + public function applyFilter(ImageInterface $image, $filter) + { + $config = $this->getFilterConfiguration()->get($filter); + + foreach ($config['filters'] as $eachFilter => $eachOptions) { + if (!isset($this->loaders[$eachFilter])) { + throw new \InvalidArgumentException(sprintf( + 'Could not find filter loader for "%s" filter type', $eachFilter + )); + } + + $image = $this->loaders[$eachFilter]->load($image, $eachOptions); + } + + return $image; + } } diff --git a/Tests/Imagine/Filter/FilterManagerTest.php b/Tests/Imagine/Filter/FilterManagerTest.php index fb973a966..aa0307b18 100644 --- a/Tests/Imagine/Filter/FilterManagerTest.php +++ b/Tests/Imagine/Filter/FilterManagerTest.php @@ -181,6 +181,41 @@ public function testGetRequestKnowsContentType() $this->assertEquals('image/jpeg', $response->headers->get('Content-Type')); } + public function testApplyFilterSet() + { + $image = $this->getMockImage(); + + $thumbConfig = array( + 'size' => array(180, 180), + 'mode' => 'outbound', + ); + + $config = $this->getMockFilterConfiguration(); + $config + ->expects($this->atLeastOnce()) + ->method('get') + ->with('thumbnail') + ->will($this->returnValue(array( + 'filters' => array( + 'thumbnail' => $thumbConfig, + ), + ))) + ; + + $loader = $this->getMockLoader(); + $loader + ->expects($this->once()) + ->method('load') + ->with($image, $thumbConfig) + ->will($this->returnValue($image)) + ; + + $filterManager = new FilterManager($config); + $filterManager->addLoader('thumbnail', $loader); + + $this->assertSame($image, $filterManager->applyFilter($image, 'thumbnail')); + } + protected function getMockLoader() { return $this->getMock('Liip\ImagineBundle\Imagine\Filter\Loader\LoaderInterface');