1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the `liip/LiipImagineBundle` project. |
5
|
|
|
* |
6
|
|
|
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Liip\ImagineBundle\Imagine\Filter\Loader; |
13
|
|
|
|
14
|
|
|
use Imagine\Image\ImageInterface; |
15
|
|
|
use Imagine\Image\ImagineInterface; |
16
|
|
|
use Liip\ImagineBundle\Utility\OptionsResolver\OptionsResolver; |
17
|
|
|
use Liip\ImagineBundle\Exception\InvalidArgumentException; |
18
|
|
|
use Symfony\Component\OptionsResolver\Options; |
19
|
|
|
use Symfony\Component\OptionsResolver\Exception\ExceptionInterface; |
20
|
|
|
|
21
|
|
|
class ResolutionFilterLoader implements LoaderInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var ImagineInterface |
25
|
|
|
*/ |
26
|
|
|
private $imagine; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param ImagineInterface $imagine |
30
|
|
|
*/ |
31
|
|
|
public function __construct(ImagineInterface $imagine) |
32
|
|
|
{ |
33
|
|
|
$this->imagine = $imagine; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param ImageInterface $image |
38
|
|
|
* @param array $options |
39
|
|
|
* |
40
|
|
|
* @throws \Exception |
41
|
|
|
* |
42
|
|
|
* @return ImageInterface |
43
|
|
|
*/ |
44
|
|
|
public function load(ImageInterface $image, array $options = array()) |
45
|
|
|
{ |
46
|
|
|
$options = $this->sanitizeOptions($options); |
47
|
|
|
$temporaryFile = $this->getTemporaryFilePath(); |
48
|
|
|
|
49
|
|
|
try { |
50
|
|
|
$image->save($temporaryFile, array( |
51
|
|
|
'resolution-units' => $options['units-per-pixel'], |
52
|
|
|
'resolution-x' => $options['x'], |
53
|
|
|
'resolution-y' => $options['y'], |
54
|
|
|
)); |
55
|
|
|
|
56
|
|
|
$image = $this->imagine->open($temporaryFile); |
57
|
|
|
unlink($temporaryFile); |
58
|
|
|
|
59
|
|
|
return $image; |
60
|
|
|
} catch (\Exception $exception) { |
61
|
|
|
unlink($temporaryFile); |
62
|
|
|
throw $exception; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
private function getTemporaryFilePath() |
70
|
|
|
{ |
71
|
|
|
$path = sys_get_temp_dir(); |
72
|
|
|
$path = tempnam($path, 'liip-imagine-bundle'); |
73
|
|
|
|
74
|
|
|
if (false === $path) { |
75
|
|
|
throw new \RuntimeException(sprintf('Temporary file can not be created in "%s".', $path)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $path; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param array $options |
83
|
|
|
* |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
private function sanitizeOptions(array $options) |
87
|
|
|
{ |
88
|
|
|
$resolver = new OptionsResolver(); |
|
|
|
|
89
|
|
|
$resolver->setAllowedTypes('x', ['int']); |
90
|
|
|
$resolver->setAllowedTypes('y', ['int']); |
91
|
|
|
$resolver->setAllowedValues('units-per-pixel', ['inch', 'centimeter']); |
92
|
|
|
$resolver->setNormalizer('units-per-pixel', function (Options $options, $value) { |
93
|
|
|
return $value === 'inch' ? ImageInterface::RESOLUTION_PIXELSPERINCH : ImageInterface::RESOLUTION_PIXELSPERCENTIMETER; |
94
|
|
|
}); |
95
|
|
|
|
96
|
|
|
try { |
97
|
|
|
return $resolver->resolve($options); |
98
|
|
|
} catch (ExceptionInterface $e) { |
99
|
|
|
throw new InvalidArgumentException('The "units" option must be set to "inch" or "centimeter" and you must set a "x" and "y" value.'); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.