-
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 1d71c29
Showing
8 changed files
with
435 additions
and
12 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
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,172 @@ | ||
<?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->setDefined(array('filter')); | ||
$resolver->setRequired(array('x', 'y', 'unit', 'temp_dir')); | ||
|
||
$resolver->setDefault('temp_dir', sys_get_temp_dir()); | ||
$resolver->setAllowedTypes('temp_dir', array('string')); | ||
$resolver->setAllowedTypes('x', array('int')); | ||
$resolver->setAllowedTypes('y', array('int')); | ||
|
||
$resolver->setAllowedValues('unit', array('inch', 'centimeter')); | ||
$resolver->setNormalizer('unit', function (Options $options, $value) { | ||
return $this->normalizeOptionUnit($value); | ||
}); | ||
|
||
$resolver->setDefault('filter', 'UNDEFINED'); | ||
$resolver->setAllowedTypes('filter', array('string')); | ||
$resolver->setNormalizer('filter', function (Options $options, $value) { | ||
return $this->normalizeOptionFilter($value); | ||
}); | ||
|
||
try { | ||
return $resolver->resolve($options); | ||
} catch (ExceptionInterface $e) { | ||
throw new InvalidArgumentException('Invalid options provided. The "units" option must be set to "inch" '. | ||
'or "centimeter" and you must set a "x" and "y" value to the ppi/ppc desired.'); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $value | ||
* | ||
* @return string | ||
*/ | ||
private function normalizeOptionUnit($value) | ||
{ | ||
return $value === 'inch' ? ImageInterface::RESOLUTION_PIXELSPERINCH : ImageInterface::RESOLUTION_PIXELSPERCENTIMETER; | ||
} | ||
|
||
/** | ||
* @param string $value | ||
* | ||
* @return string | ||
*/ | ||
private function normalizeOptionFilter($value) | ||
{ | ||
foreach (array('\Imagine\Image\ImageInterface::FILTER_%s', '\Imagine\Image\ImageInterface::%s', '\%s') as $format) { | ||
if (defined($constant = sprintf($format, strtoupper($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"'); | ||
} | ||
} |
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.