Skip to content

Commit

Permalink
Merge pull request #23 from hrevert/feature/image-quality
Browse files Browse the repository at this point in the history
support for image output options like quality
  • Loading branch information
ojhaujjwal committed Feb 20, 2015
2 parents cc80ee1 + 9f9286f commit 132548f
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/Controller/ImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ public function displayAction()
return $this->notFoundAction();
}

return new ImageModel($imageData['image'], $imageData['format']);
return new ImageModel($imageData['image'], $imageData['format'], $imageData['imageOutputOptions']);
}
}
4 changes: 2 additions & 2 deletions src/Service/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ public function getCachePath($relativeName, $filter, $formatOrImage = null)
/**
* {@inheritDoc}
*/
public function createCache($relativeName, $filter, ImageInterface $image, $formatOrImage = null)
public function createCache($relativeName, $filter, ImageInterface $image, $formatOrImage = null, array $saveOptions = [])
{
$cachePath = $this->getCachePath($relativeName, $filter, $formatOrImage);
if (!is_dir(dirname($cachePath))) {
mkdir(dirname($cachePath), 0755, true);
}
$image->save($cachePath);
$image->save($cachePath, $saveOptions);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Service/CacheManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ public function getCachePath($relativeName, $filter, $formatOrImage = null);
* @param string $filter
* @param ImageInterface $image
* @param string|null $formatOrImage
* @param array $saveOptions
* @return void
*/
public function createCache($relativeName, $filter, ImageInterface $image, $formatOrImage = null);
public function createCache($relativeName, $filter, ImageInterface $image, $formatOrImage = null, array $saveOptions = []);

/**
* Deletes a new cache
Expand Down
26 changes: 15 additions & 11 deletions src/Service/ImageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ public function __construct(
ImagineInterface $imagine,
FilterManagerInterface $filterManager,
LoaderManagerInterface $loaderManager
)
{
) {
$this->cacheManager = $cacheManager;
$this->imagine = $imagine;
$this->filterManager = $filterManager;
Expand All @@ -59,36 +58,41 @@ public function getImage($relativePath, $filter)

$filterOptions = $this->filterManager->getFilterOptions($filter);

$binary = $this->loaderManager->loadBinary($relativePath, $filter);

if (isset($filterOptions['format'])) {
$format = $filterOptions['format'];
} else {
$binary = $this->loaderManager->loadBinary($relativePath, $filter);
$format = $binary->getFormat() ?: 'png';
}

$imageOutputOptions = [];
if (isset($filterOptions['quality'])) {
$imageOutputOptions['quality'] = $filterOptions['quality'];
}
if ($format === 'gif' && $filterOptions['animated']) {
$imageOutputOptions['animated'] = $filterOptions['animated'];
}

if ($this->cacheManager->isCachingEnabled($filter, $filterOptions) && $this->cacheManager->cacheExists($relativePath, $filter, $format)) {
$imagePath = $this->cacheManager->getCachePath($relativePath, $filter, $format);
$filteredImage = $this->imagine->open($imagePath);
} else {
if (!isset($binary)) {
$binary = $this->loaderManager->loadBinary($relativePath, $filter);
}

$image = $this->imagine->load($binary->getContent());
$filteredImage = $this->filterManager->getFilter($filter)->apply($image);

if ($this->cacheManager->isCachingEnabled($filter, $filterOptions)) {
$this->cacheManager->createCache($relativePath, $filter, $filteredImage, $format);
$this->cacheManager->createCache($relativePath, $filter, $filteredImage, $format, $imageOutputOptions);
}
}

$args = ['relativePath' => $relativePath, 'filter' => $filter, 'filteredImage' => $filteredImage, 'format' => $format];
$eventManager->trigger(__FUNCTION__ . '.post', $this, $args);
$eventManager->trigger(__FUNCTION__.'.post', $this, $args);

return [
'image' => $filteredImage,
'format' => $format
'format' => $format,
'imageOutputOptions' => $imageOutputOptions,
];

}
}
32 changes: 31 additions & 1 deletion src/View/Model/ImageModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,20 @@ class ImageModel extends ViewModel
*/
protected $format = 'png';

/**
* @var array
*/
protected $imageOutputOptions = [];

/**
* Constructor
*
* @param ImageInterface|string $imageOrPath
* @param string|null $format
* @param array $imageOutputOptions
* @throws Exception\InvalidArgumentException
*/
public function __construct($imageOrPath = null, $format = null)
public function __construct($imageOrPath = null, $format = null, array $imageOutputOptions = [])
{
if ($imageOrPath !== null) {
if (is_string($imageOrPath)) {
Expand All @@ -66,6 +72,7 @@ public function __construct($imageOrPath = null, $format = null)
if ($format !== null) {
$this->setFormat($format);
}
$this->setImageOutputOptions($imageOutputOptions);
}

/**
Expand Down Expand Up @@ -137,4 +144,27 @@ public function getFormat()
{
return $this->format;
}

/**
* Sets imageOutputOptions
*
* @param array $imageOutputOptions
* @return self
*/
public function setImageOutputOptions(array $imageOutputOptions)
{
$this->imageOutputOptions = $imageOutputOptions;

return $this;
}

/**
* Gets imageOutputOptions
*
* @return array
*/
public function getImageOutputOptions()
{
return $this->imageOutputOptions;
}
}
2 changes: 1 addition & 1 deletion src/View/Renderer/ImageRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function render($nameOrModel, $values = null)
);
}

return $image->get($format);
return $image->get($format, $imageModel->getImageOutputOptions());
}

throw new Exception\InvalidArgumentException(sprintf(
Expand Down
14 changes: 7 additions & 7 deletions tests/HtImgModuleTest/Controller/ImageControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function testGet404WhenImageIsNotFound()
$relativePath = 'relative/path/of/image';
$filter = 'awesome_filter';

$pluginManager = new PluginManager;
$pluginManager = new PluginManager();
$controller->setPluginManager($pluginManager);
$paramsPlugin = $this->getMock('Zend\Mvc\Controller\Plugin\Params');
$pluginManager->setService('params', $paramsPlugin);
Expand All @@ -36,7 +36,7 @@ public function testGet404WhenImageIsNotFound()
$imageService->expects($this->once())
->method('getImage')
->with($relativePath, $filter)
->will($this->throwException(new Exception\ImageNotFoundException));
->will($this->throwException(new Exception\ImageNotFoundException()));
$this->expect404($controller);
$controller->displayAction();
}
Expand All @@ -50,7 +50,7 @@ public function testGet404WhenFilterIsNotFound()
$relativePath = 'relative/path/of/image';
$filter = 'awesome_filter';

$pluginManager = new PluginManager;
$pluginManager = new PluginManager();
$controller->setPluginManager($pluginManager);
$paramsPlugin = $this->getMock('Zend\Mvc\Controller\Plugin\Params');
$pluginManager->setService('params', $paramsPlugin);
Expand All @@ -68,7 +68,7 @@ public function testGet404WhenFilterIsNotFound()
$imageService->expects($this->once())
->method('getImage')
->with($relativePath, $filter)
->will($this->throwException(new Exception\FilterNotFoundException));
->will($this->throwException(new Exception\FilterNotFoundException()));
$this->expect404($controller);
$controller->displayAction();
}
Expand All @@ -82,7 +82,7 @@ public function testDisplayImage()
$relativePath = 'relative/path/of/image';
$filter = 'awesome_filter';

$pluginManager = new PluginManager;
$pluginManager = new PluginManager();
$controller->setPluginManager($pluginManager);
$paramsPlugin = $this->getMock('Zend\Mvc\Controller\Plugin\Params');
$pluginManager->setService('params', $paramsPlugin);
Expand All @@ -99,7 +99,7 @@ public function testDisplayImage()

$image = $this->getMock('Imagine\Image\ImageInterface');
$format = 'gif';
$imageData = ['image' => $image, 'format' => $format];
$imageData = ['image' => $image, 'format' => $format, 'imageOutputOptions' => ['quality' => 57]];

$imageService->expects($this->once())
->method('getImage')
Expand All @@ -114,7 +114,7 @@ public function testDisplayImage()

protected function expect404(ImageController $controller)
{
$event = new MvcEvent;
$event = new MvcEvent();
$controller->setEvent($event);
$routeMatch = $this->getMockBuilder('Zend\Mvc\Router\RouteMatch')
->disableOriginalConstructor()
Expand Down
28 changes: 21 additions & 7 deletions tests/HtImgModuleTest/Service/ImageServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
namespace HtImgModuleTest\Service;

use HtImgModule\Service\ImageService;
use HtImgModule\Options\ModuleOptions;
use HtImgModule\Service\CacheManager;
use Imagine\Gd\Imagine;
use HtImgModule\Imagine\Filter\FilterManager;
Expand All @@ -17,7 +16,7 @@ public function testGetImageFromCache()
->will($this->returnValue(true));
$cacheManager->expects($this->once())
->method('getCachePath')
->will($this->returnValue(RESOURCES_DIR . '/flowers.jpg'));
->will($this->returnValue(RESOURCES_DIR.'/flowers.jpg'));
$imagine = $this->getMock('Imagine\Image\ImagineInterface');
$filterManager = $this->getMock('HtImgModule\Imagine\Filter\FilterManagerInterface');
$loaderManager = $this->getMock('HtImgModule\Imagine\Loader\LoaderManagerInterface');
Expand All @@ -28,7 +27,16 @@ public function testGetImageFromCache()
$loaderManager
);

$filterOptions = ['format' => 'jpg'];
$relativePath = 'path/to/image/flowers.jpg';
$filterName = 'foo_filter';

$binary = $this->getMock('HtImgModule\Binary\BinaryInterface');
$loaderManager->expects($this->once())
->method('loadBinary')
->with($relativePath, $filterName)
->will($this->returnValue($binary));

$filterOptions = ['format' => 'gif', 'quality' => 87, 'animated' => true];

$filterManager->expects($this->once())
->method('getFilterOptions')
Expand All @@ -43,12 +51,15 @@ public function testGetImageFromCache()
$image = $this->getMock('Imagine\Image\ImageInterface');
$imagine->expects($this->once())
->method('open')
->with(RESOURCES_DIR . '/flowers.jpg')
->with(RESOURCES_DIR.'/flowers.jpg')
->will($this->returnValue($image));

$imageData = $imageService->getImage('path/to/image/flowers.jpg', 'foo_filter');

$this->assertEquals($image, $imageData['image']);
$this->assertEquals('gif', $imageData['format']);
$this->assertEquals(87, $imageData['imageOutputOptions']['quality']);
$this->assertEquals(true, $imageData['imageOutputOptions']['animated']);
}

public function testGetImageFromRelativePathAndCreateCache()
Expand All @@ -67,10 +78,13 @@ public function testGetImageFromRelativePathAndCreateCache()
$binaryContent = '35345fascxzcasdfhj;alsdkf4asldfkja;sldf65854';
$relativePath = 'relative/path/to/image';
$filterName = 'foo-bar-filter';

$filterOptions = ['quality' => 87];

$filterManager->expects($this->once())
->method('getFilterOptions')
->with($filterName)
->will($this->returnValue([]));
->will($this->returnValue($filterOptions));

$binary = $this->getMock('HtImgModule\Binary\BinaryInterface');
$binary->expects($this->once())
Expand Down Expand Up @@ -102,12 +116,12 @@ public function testGetImageFromRelativePathAndCreateCache()

$cacheManager->expects($this->any())
->method('isCachingEnabled')
->with($filterName, [])
->with($filterName, $filterOptions)
->will($this->returnValue(true));

$cacheManager->expects($this->once())
->method('createCache')
->with($relativePath, $filterName, $filteredImage, 'png');
->with($relativePath, $filterName, $filteredImage, 'png', $filterOptions);

$imageData = $imageService->getImage($relativePath, $filterName);

Expand Down
9 changes: 5 additions & 4 deletions tests/HtImgModuleTest/View/Model/ImageModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ class ImageModelTest extends \PHPUnit_Framework_TestCase
{
public function testSettersAndGetters()
{
$model = new ImageModel('./');
$model->setFormat('jpeg');
$imageOutputOptions = ['quality' => 93];
$model = new ImageModel('./', 'jpg', $imageOutputOptions);
$this->assertEquals('./', $model->getImagePath());
$this->assertEquals('jpeg', $model->getFormat());
$this->assertEquals('jpg', $model->getFormat());
$this->assertEquals($imageOutputOptions, $model->getImageOutputOptions());
$imagine = new Imagine();
$archos = $imagine->open('resources/Archos.jpg');
$model = new ImageModel($archos, 'gif');
Expand All @@ -22,6 +23,6 @@ public function testSettersAndGetters()
public function testGetExceptionWithInvalidConstructorArgument()
{
$this->setExpectedException('HtImgModule\Exception\InvalidArgumentException');
$model = new ImageModel(new \ArrayObject);
$model = new ImageModel(new \ArrayObject());
}
}
16 changes: 9 additions & 7 deletions tests/HtImgModuleTest/View/Renderer/ImageRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,36 @@ class ImageRendererTest extends \PHPUnit_Framework_TestCase
{
public function testRender()
{
$imageOutputOptions = ['quality' => 93];

$image = $this->getMock('Imagine\Image\ImageInterface');
$image->expects($this->exactly(1))
->method('get')
->method('get', $imageOutputOptions)
->with('png')
->will($this->returnValue('image-binary-string'));

$model = new ImageModel($image, 'png');
$model = new ImageModel($image, 'png', $imageOutputOptions);

$renderer = new ImageRenderer;
$renderer = new ImageRenderer();
$this->assertEquals('image-binary-string', $renderer->render($model));
}

public function testGetExceptionWhenModelIsInvalid()
{
$renderer = new ImageRenderer;
$renderer = new ImageRenderer();
$this->setExpectedException('HtImgModule\Exception\InvalidArgumentException');
$renderer->render(new ViewModel);
$renderer->render(new ViewModel());
}

public function testGetEngine()
{
$renderer = new ImageRenderer;
$renderer = new ImageRenderer();
$this->assertEquals($renderer, $renderer->getEngine());
}

public function testSetResolver()
{
$renderer = new ImageRenderer;
$renderer = new ImageRenderer();
$this->assertEquals($renderer, $renderer->setResolver($this->getMock('Zend\View\Resolver\ResolverInterface')));
}
}

0 comments on commit 132548f

Please sign in to comment.