Skip to content

Commit

Permalink
Merge pull request #297 from formapro-forks/filter-use-binary
Browse files Browse the repository at this point in the history
[1.0][filter-manager] make use of binary object.
  • Loading branch information
havvg committed Jan 13, 2014
2 parents 99c407f + 3810d19 commit 8a119fd
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 125 deletions.
20 changes: 8 additions & 12 deletions Controller/ImagineController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,44 +28,40 @@ class ImagineController
*/
protected $cacheManager;

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

/**
* @param DataManager $dataManager
* @param FilterManager $filterManager
* @param CacheManager $cacheManager
* @param ImagineInterface $imagine
*/
public function __construct(DataManager $dataManager, FilterManager $filterManager, CacheManager $cacheManager, ImagineInterface $imagine)
public function __construct(DataManager $dataManager, FilterManager $filterManager, CacheManager $cacheManager)
{
$this->dataManager = $dataManager;
$this->filterManager = $filterManager;
$this->cacheManager = $cacheManager;
$this->imagine = $imagine;
}

/**
* This action applies a given filter to a given image, optionally saves the image and outputs it to the browser at the same time.
*
* @param Request $request
* @param string $path
* @param string $filter
*
* @return Response
*/
public function filterAction(Request $request, $path, $filter)
public function filterAction($path, $filter)
{
if ($this->cacheManager->isStored($path, $filter)) {
return new RedirectResponse($this->cacheManager->resolve($path, $filter), 301);
}

$binary = $this->dataManager->find($filter, $path);
$image = $this->imagine->load($binary->getContent());

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

// Usage of response will be replaced by next PR.
$response = new Response($filteredBinary->getContent(), 200, array(
'Content-Type' => $filteredBinary->getMimeType(),
));

return $this->cacheManager->store($response, $path, $filter);
}
Expand Down
69 changes: 23 additions & 46 deletions Imagine/Filter/FilterManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
namespace Liip\ImagineBundle\Imagine\Filter;

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

use Liip\ImagineBundle\Model\Binary;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

Expand All @@ -15,19 +18,24 @@ class FilterManager
*/
protected $filterConfig;

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

/**
* @var LoaderInterface[]
*/
protected $loaders = array();

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

/**
Expand All @@ -52,54 +60,19 @@ public function getFilterConfiguration()
}

/**
* Returns a response containing the given image after applying the given filter on it.
* Apply the provided filter set on the given binary.
*
* @uses FilterManager::applyFilterSet
* @param BinaryInterface $binary
* @param string $filter
*
* @param Request $request
* @param string $filter
* @param ImageInterface $image
* @param string $localPath
* @throws \InvalidArgumentException
*
* @return Response
* @return BinaryInterface
*/
public function get(Request $request, $filter, ImageInterface $image, $localPath)
public function applyFilter(BinaryInterface $binary, $filter)
{
$config = $this->getFilterConfiguration()->get($filter);
$image = $this->imagine->load($binary->getContent());

$image = $this->applyFilter($image, $filter);

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);
if (empty($contentType)) {
$contentType = 'image/'.$format;
}

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) {
Expand All @@ -112,6 +85,10 @@ public function applyFilter(ImageInterface $image, $filter)
$image = $this->loaders[$eachFilter]->load($image, $eachOptions);
}

return $image;
$filteredContent = $image->get($binary->getFormat(), array(
'quality' => array_key_exists('quality', $config) ? $config['quality'] : 100
));

return new Binary($filteredContent, $binary->getMimeType(), $binary->getFormat());
}
}
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ class MyController extends Controller
$imagemanagerResponse = $this->container
->get('liip_imagine.controller')
->filterAction(
$this->getRequest(),
'uploads/foo.jpg', // original image you want to apply a filter to
'my_thumb' // filter defined in config.yml
);
Expand Down
2 changes: 1 addition & 1 deletion 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 All @@ -92,7 +93,6 @@
<argument type="service" id="liip_imagine.data.manager" />
<argument type="service" id="liip_imagine.filter.manager" />
<argument type="service" id="liip_imagine.cache.manager" />
<argument type="service" id="liip_imagine" />
</service>

<!-- Route Loader -->
Expand Down
16 changes: 9 additions & 7 deletions Resources/doc/filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,24 +170,26 @@ is of course "bogus" since hardcoded values could just as well be set in the con
but it illustrates the core idea.

``` php
public function filterAction(Request $request, $path, $filter)
public function filterAction($path, $filter)
{
if ($response = $this->cacheManager->resolve($path, $filter)) {
return $response;
if ($this->cacheManager->isStored($path, $filter)) {
return new RedirectResponse($this->cacheManager->resolve($path, $filter), 301);
}
$image = $this->dataManager->find($filter, $path);
$binary = $this->dataManager->find($filter, $path);
$filterConfig = $this->filterManager->getFilterConfiguration();
$config = $filterConfig->get($filter);
$config['filters']['thumbnail']['size'] = array(300, 100);
$filterConfig->set($filter, $config);
$response = $this->filterManager->get($request, $filter, $image, $path);
$filteredBinary = $this->filterManager->applyFilter($binary, $filter);
$this->cacheManager->store($response, $path, $filter);
$response = new Response($filteredBinary->getContent(), 200, array(
'Content-Type' => $filteredBinary->getMimeType(),
));
return $response;
return $this->cacheManager->store($response, $path, $filter);
}
```

Expand Down
10 changes: 4 additions & 6 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 All @@ -116,7 +116,7 @@ public function testFilterActionLive()

$webPathResolver->setRequest($request);

$response = $controller->filterAction($request, 'cats.jpeg', 'thumbnail');
$response = $controller->filterAction('cats.jpeg', 'thumbnail');

$filePath = realpath($this->webRoot).'/media/cache/thumbnail/cats.jpeg';

Expand Down Expand Up @@ -146,13 +146,11 @@ 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);

$request = Request::create('/media/cache/thumbnail/cats.jpeg');

$response = $controller->filterAction($request, 'cats.jpeg', 'thumbnail');
$response = $controller->filterAction('cats.jpeg', 'thumbnail');

$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
$this->assertEquals('http://foo.com/a/path/image.jpg', $response->headers->get('Location'));
Expand Down
1 change: 0 additions & 1 deletion Tests/DependencyInjection/LiipImagineExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public function testLoadWithDefaults()
new Reference('liip_imagine.data.manager'),
new Reference('liip_imagine.filter.manager'),
new Reference('liip_imagine.cache.manager'),
new Reference('liip_imagine')
)
);
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/Functional/Imagine/DataManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class DataManagerTest extends WebTestCase
public function testCouldBeGetFromContainerAsService()
{
$this->createClient();
$service =self::$kernel->getContainer()->get('liip_imagine.data.manager');
$service = self::$kernel->getContainer()->get('liip_imagine.data.manager');

$this->assertInstanceOf('Liip\ImagineBundle\Imagine\Data\DataManager', $service);
}
Expand Down
15 changes: 15 additions & 0 deletions Tests/Functional/Imagine/FilterManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
namespace Liip\ImagineBundle\Tests\Functional\Imagine;

use Liip\ImagineBundle\Tests\Functional\WebTestCase;

class FilterManagerTest extends WebTestCase
{
public function testCouldBeGetFromContainerAsService()
{
$this->createClient();
$service = self::$kernel->getContainer()->get('liip_imagine.filter.manager');

$this->assertInstanceOf('Liip\ImagineBundle\Imagine\Filter\FilterManager', $service);
}
}
Loading

0 comments on commit 8a119fd

Please sign in to comment.