-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d2551d8
commit c697f12
Showing
8 changed files
with
491 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the `liip/LiipImagineBundle` project. | ||
* | ||
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors | ||
* | ||
* For the full copyright and license information, please view the LICENSE.md | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Liip\ImagineBundle\Exception\Imagine\Filter; | ||
|
||
use Liip\ImagineBundle\Exception\ExceptionInterface; | ||
|
||
class LoadFilterException extends \RuntimeException implements ExceptionInterface | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the `liip/LiipImagineBundle` project. | ||
* | ||
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors | ||
* | ||
* For the full copyright and license information, please view the LICENSE.md | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Liip\ImagineBundle\Imagine\Filter\Loader; | ||
|
||
use Imagine\Image\ImageInterface; | ||
use Imagine\Image\ImagineInterface; | ||
use Liip\ImagineBundle\Exception\Imagine\Filter\LoadFilterException; | ||
use Liip\ImagineBundle\Utility\OptionsResolver\OptionsResolver; | ||
use Liip\ImagineBundle\Exception\InvalidArgumentException; | ||
use Symfony\Component\OptionsResolver\Options; | ||
use Symfony\Component\OptionsResolver\Exception\ExceptionInterface; | ||
|
||
class ResampleFilterLoader implements LoaderInterface | ||
{ | ||
/** | ||
* @var ImagineInterface | ||
*/ | ||
private $imagine; | ||
|
||
/** | ||
* @param ImagineInterface $imagine | ||
*/ | ||
public function __construct(ImagineInterface $imagine) | ||
{ | ||
$this->imagine = $imagine; | ||
} | ||
|
||
/** | ||
* @param ImageInterface $image | ||
* @param array $options | ||
* | ||
* @throws LoadFilterException | ||
* | ||
* @return ImageInterface | ||
*/ | ||
public function load(ImageInterface $image, array $options = array()) | ||
{ | ||
$options = $this->resolveOptions($options); | ||
$tmpFile = $this->getTemporaryFile($options['temp_dir']); | ||
|
||
try { | ||
$image->save($tmpFile, $this->getImagineSaveOptions($options)); | ||
$image = $this->imagine->open($tmpFile); | ||
$this->delTemporaryFile($tmpFile); | ||
} catch (\Exception $exception) { | ||
$this->delTemporaryFile($tmpFile); | ||
throw new LoadFilterException('Unable to save/open file in resample filter loader.', null, $exception); | ||
} | ||
|
||
return $image; | ||
} | ||
|
||
/** | ||
* @param string $path | ||
* | ||
* @throws \RuntimeException | ||
* | ||
* @return string | ||
*/ | ||
private function getTemporaryFile($path) | ||
{ | ||
if (!is_dir($path) || false === $file = tempnam($path, 'liip-imagine-bundle')) { | ||
throw new \RuntimeException(sprintf('Unable to create temporary file in "%s" base path.', $path)); | ||
} | ||
|
||
return $file; | ||
} | ||
|
||
/** | ||
* @param $file | ||
* | ||
* @throws \RuntimeException | ||
*/ | ||
private function delTemporaryFile($file) | ||
{ | ||
if (file_exists($file)) { | ||
unlink($file); | ||
} | ||
} | ||
|
||
/** | ||
* @param array $options | ||
* | ||
* @return array | ||
*/ | ||
private function getImagineSaveOptions(array $options) | ||
{ | ||
$saveOptions = array( | ||
'resolution-units' => $options['unit'], | ||
'resolution-x' => $options['x'], | ||
'resolution-y' => $options['y'], | ||
); | ||
|
||
if (isset($options['filter'])) { | ||
$saveOptions['resampling-filter'] = $options['filter']; | ||
} | ||
|
||
return $saveOptions; | ||
} | ||
|
||
/** | ||
* @param array $options | ||
* | ||
* @return array | ||
*/ | ||
private function resolveOptions(array $options) | ||
{ | ||
$resolver = new OptionsResolver(); | ||
$resolver->setRequired(array('x', 'y', 'unit', 'temp_dir')); | ||
$resolver->setDefined(array('filter')); | ||
$resolver->setDefault('temp_dir', sys_get_temp_dir()); | ||
$resolver->setDefault('filter', 'UNDEFINED'); | ||
$resolver->setAllowedTypes('x', array('int')); | ||
$resolver->setAllowedTypes('y', array('int')); | ||
$resolver->setAllowedTypes('temp_dir', array('string')); | ||
$resolver->setAllowedTypes('filter', array('string')); | ||
$resolver->setAllowedValues('unit', array( | ||
ImageInterface::RESOLUTION_PIXELSPERINCH, | ||
ImageInterface::RESOLUTION_PIXELSPERCENTIMETER | ||
)); | ||
$resolver->setNormalizer('filter', function (Options $options, $value) { | ||
foreach (array('\Imagine\Image\ImageInterface::FILTER_%s', '\Imagine\Image\ImageInterface::%s', '\%s', '%s') as $format) { | ||
if (defined($constant = sprintf($format, strtoupper($value))) || defined($constant = sprintf($format, $value))) { | ||
return constant($constant); | ||
} | ||
} | ||
|
||
throw new InvalidArgumentException('The "filter" option must resolve to a valid constant using one of '. | ||
'the following formats: "\Imagine\Image\ImageInterface::FILTER_%s" or '. | ||
'"\Imagine\Image\ImageInterface::%s" or "\%s"'); | ||
}); | ||
|
||
try { | ||
return $resolver->resolve($options); | ||
} catch (ExceptionInterface $e) { | ||
throw new InvalidArgumentException('Invalid options provided. The "unit" option must be "ppi" (pixels per '. | ||
'inch) or "ppc" (pixels per centimeter) and "x" and "y" must be set to the desired ppi/ppc.', null, $e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
Tests/Functional/Imagine/Filter/Loader/ResampleFilterLoaderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the `liip/LiipImagineBundle` project. | ||
* | ||
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors | ||
* | ||
* For the full copyright and license information, please view the LICENSE.md | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Liip\ImagineBundle\Tests\Functional\Imagine\Filter\Loader; | ||
|
||
use Liip\ImagineBundle\Tests\Functional\AbstractWebTestCase; | ||
|
||
/** | ||
* @covers \Liip\ImagineBundle\Imagine\Filter\Loader\ResampleFilterLoader | ||
*/ | ||
class ResampleFilterLoaderTest extends AbstractWebTestCase | ||
{ | ||
public function testContainerHasService() | ||
{ | ||
$this->createClient(); | ||
|
||
$this->assertInstanceOf( | ||
'\Liip\ImagineBundle\Imagine\Filter\Loader\ResampleFilterLoader', | ||
self::$kernel->getContainer()->get('liip_imagine.filter.loader.resample') | ||
); | ||
} | ||
} |
Oops, something went wrong.