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

[WIP][1.0][FilterManager] Filter manager drop get method. Add ability to configure at runtime. #288

Closed
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
5 changes: 4 additions & 1 deletion Controller/ImagineController.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ public function filterAction(Request $request, $path, $filter)
$binary = $this->dataManager->find($filter, $path);
$image = $this->imagine->load($binary->getContent());

$response = $this->filterManager->get($request, $filter, $image, $path);
$filteredImage = $this->filterManager->applyFilter($image, $filter, array(
'format' => $binary->getFormat()
));
$response = $this->filterManager->get($request, $filter, $filteredImage, $path);

return $this->cacheManager->store($response, $path, $filter);
}
Expand Down
22 changes: 11 additions & 11 deletions Imagine/Cache/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,21 +140,21 @@ public function getBrowserPath($path, $filter, $absolute = false)
*/
public function generateUrl($path, $filter, $absolute = false)
{
$config = $this->filterConfig->get($filter);

if (isset($config['format'])) {
$pathinfo = pathinfo($path);
$config = $this->filterConfig->get($filter, array(
'format' => pathinfo($path, PATHINFO_EXTENSION),
));

// the extension should be forced and a directory is detected
if ((!isset($pathinfo['extension']) || $pathinfo['extension'] !== $config['format'])
&& isset($pathinfo['dirname'])) {
$pathinfo = pathinfo($path);

if ('\\' === $pathinfo['dirname']) {
$pathinfo['dirname'] = '';
}
// the extension should be forced and a directory is detected
if ((!isset($pathinfo['extension']) || $pathinfo['extension'] !== $config['format'])
&& isset($pathinfo['dirname'])) {

$path = $pathinfo['dirname'].'/'.$pathinfo['filename'].'.'.$config['format'];
if ('\\' === $pathinfo['dirname']) {
$pathinfo['dirname'] = '';
}

$path = $pathinfo['dirname'].'/'.$pathinfo['filename'].'.'.$config['format'];
}

$params = array('path' => ltrim($path, '/'));
Expand Down
34 changes: 28 additions & 6 deletions Imagine/Filter/FilterConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,40 @@ class FilterConfiguration
*/
public function __construct(array $filters = array())
{
$this->filters = $filters;
foreach ($filters as $filter => $config) {
$this->set($filter, $config);
}
}

/**
* Gets a previously configured filter.
*
* @param string $filter
* @param array $runtimeConfig
*
* @return array
*
* @throws \RuntimeException
*/
public function get($filter)
public function get($filter, array $runtimeConfig = array())
{
if (empty($this->filters[$filter])) {
throw new \RuntimeException('Filter not defined: '.$filter);
if (false == array_key_exists($filter, $this->filters)) {
throw new \RuntimeException(sprintf('Could not find configuration for a filter: %s', $filter));
}

$defaultConfig = $this->filters[$filter];
$config = array_replace_recursive($defaultConfig, $runtimeConfig);

// $defaultConfig['format'] means we always want to format an image to this format.
// So this adds BC with previous versions.
if ($defaultConfig['format']) {
$config['format'] = $defaultConfig['format'];
}
if (empty($config['format'])) {
$config['format'] = 'png';
}

return $this->filters[$filter];
return $config;
}

/**
Expand All @@ -45,6 +60,13 @@ public function get($filter)
*/
public function set($filter, array $config)
{
return $this->filters[$filter] = $config;
$this->filters[$filter] = array_replace(
array(
'quality' => 100,
'format' => null,
'filters' => array(),
),
$config
);
}
}
52 changes: 26 additions & 26 deletions Imagine/Filter/FilterManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Liip\ImagineBundle\Imagine\Filter;

use Imagine\Image\ImageInterface;
use Imagine\Image\ImagineInterface;
use Liip\ImagineBundle\Imagine\Filter\Loader\LoaderInterface;

use Symfony\Component\HttpFoundation\Request;
Expand All @@ -20,14 +21,20 @@ class FilterManager
*/
protected $loaders = array();

/**
* @var ImagineInterface
*/
protected $imagine;

/**
* Constructor.
*
* @param FilterConfiguration $filterConfig
*/
public function __construct(FilterConfiguration $filterConfig)
public function __construct(FilterConfiguration $filterConfig, ImagineInterface $imagine)
{
$this->filterConfig = $filterConfig;
$this->imagine = $imagine;
}

/**
Expand All @@ -52,56 +59,47 @@ public function getFilterConfiguration()
}

/**
* Returns a response containing the given image after applying the given filter on it.
* @deprecated
*
* @uses FilterManager::applyFilterSet
* Returns a response containing the given filtered image.
*
* @param Request $request
* @param string $filter
* @param ImageInterface $image
* @param string $localPath
* @param ImageInterface $filteredImage
* @param string $path
*
* @return Response
*/
public function get(Request $request, $filter, ImageInterface $image, $localPath)
public function get(Request $request, $filter, ImageInterface $filteredImage, $path)
{
$config = $this->getFilterConfiguration()->get($filter);
$config = $this->getFilterConfiguration()->get($filter, array(
'format' => pathinfo($path, PATHINFO_EXTENSION),
));

$image = $this->applyFilter($image, $filter);
$filteredImage = $filteredImage->get($config['format'], array('quality' => $config['quality']));

if (empty($config['format'])) {
$format = pathinfo($localPath, PATHINFO_EXTENSION);
$format = $format ?: 'png';
} else {
$format = $config['format'];
}

$quality = empty($config['quality']) ? 100 : $config['quality'];

$image = $image->get($format, array('quality' => $quality));

$contentType = $request->getMimeType($format);
$contentType = $request->getMimeType($config['format']);
if (empty($contentType)) {
$contentType = 'image/'.$format;
$contentType = 'image/'.$config['format'];
}

return new Response($image, 200, array('Content-Type' => $contentType));
return new Response($filteredImage, 200, array('Content-Type' => $contentType));
}

/**
* Apply the provided filter set on the given Image.
*
* @param ImageInterface $image
* @param string $filter
* @param array $runtimeConfig
*
* @return ImageInterface
*
* @throws \InvalidArgumentException
*/
public function applyFilter(ImageInterface $image, $filter)
public function applyFilter(ImageInterface $image, $filter, array $runtimeConfig = array())
{
$config = $this->getFilterConfiguration()->get($filter);

$config = $this->getFilterConfiguration()->get($filter, $runtimeConfig);
foreach ($config['filters'] as $eachFilter => $eachOptions) {
if (!isset($this->loaders[$eachFilter])) {
throw new \InvalidArgumentException(sprintf(
Expand All @@ -112,6 +110,8 @@ public function applyFilter(ImageInterface $image, $filter)
$image = $this->loaders[$eachFilter]->load($image, $eachOptions);
}

return $image;
return $this->imagine->load(
$image->get($config['format'], array('quality' => $config['quality']))
);
}
}
1 change: 1 addition & 0 deletions Resources/config/imagine.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@

<service id="liip_imagine.filter.manager" class="%liip_imagine.filter.manager.class%">
<argument type="service" id="liip_imagine.filter.configuration" />
<argument type="service" id="liip_imagine" />
</service>

<service id="liip_imagine.data.manager" class="%liip_imagine.data.manager.class%">
Expand Down
3 changes: 2 additions & 1 deletion Resources/doc/filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ public function filterAction(Request $request, $path, $filter)
$config['filters']['thumbnail']['size'] = array(300, 100);
$filterConfig->set($filter, $config);

$response = $this->filterManager->get($request, $filter, $image, $path);
$filteredImage = $this->filterManager->applyFilter($image, $filter);
$response = $this->filterManager->get($request, $filter, $filteredImage, $path);

$this->cacheManager->store($response, $path, $filter);

Expand Down
4 changes: 2 additions & 2 deletions Tests/Controller/ImagineControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function testFilterActionLive()

$filterLoader = new ThumbnailFilterLoader();

$filterManager = new FilterManager($this->configuration);
$filterManager = new FilterManager($this->configuration, $this->imagine);
$filterManager->addLoader('thumbnail', $filterLoader);

$webPathResolver = new WebPathResolver($this->filesystem);
Expand Down Expand Up @@ -146,7 +146,7 @@ public function testFilterDelegatesResolverResponse()
$extensionGuesser = ExtensionGuesser::getInstance();

$dataManager = $this->getMock('Liip\ImagineBundle\Imagine\Data\DataManager', array(), array($mimeTypeGuesser, $extensionGuesser, $this->configuration));
$filterManager = $this->getMock('Liip\ImagineBundle\Imagine\Filter\FilterManager', array(), array($this->configuration));
$filterManager = $this->getMock('Liip\ImagineBundle\Imagine\Filter\FilterManager', array(), array($this->configuration, $this->imagine));

$controller = new ImagineController($dataManager, $filterManager, $cacheManager, $this->imagine);

Expand Down
11 changes: 4 additions & 7 deletions Tests/Imagine/Cache/CacheManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Liip\ImagineBundle\Tests\Imagine\Cache;

use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration;
use Liip\ImagineBundle\Tests\AbstractTest;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -106,6 +107,7 @@ public function testFilterActionUrlGeneratedAndReturnIfResolverReturnNullOnGetBr
->method('get')
->with('thumbnail')
->will($this->returnValue(array(
'format' => 'png',
'size' => array(180, 180),
'mode' => 'outbound',
'cache' => null,
Expand Down Expand Up @@ -242,13 +244,8 @@ public function generateUrlProvider()
*/
public function testGenerateUrl($filterConfig, $path, $expectedPath)
{
$config = $this->getMockFilterConfiguration();
$config
->expects($this->once())
->method('get')
->with('thumbnail')
->will($this->returnValue($filterConfig))
;
$config = new FilterConfiguration;
$config->set('thumbnail', $filterConfig);

$router = $this->getMockRouter();
$router
Expand Down
Loading