Skip to content

Commit

Permalink
Merge pull request #150 from havvg/master
Browse files Browse the repository at this point in the history
add FilterManager::applyFilter
  • Loading branch information
havvg committed Feb 13, 2013
2 parents 74e1d9d + 4415054 commit 3b232e3
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 9 deletions.
39 changes: 30 additions & 9 deletions Imagine/Filter/FilterManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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;
}
}
35 changes: 35 additions & 0 deletions Tests/Imagine/Filter/FilterManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 3b232e3

Please sign in to comment.