Skip to content
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 FilterManager::applyFilter #150

Merged
merged 1 commit into from
Feb 13, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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