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\Exception\Imagine\Filter\LoadFilterException; |
17
|
|
|
use Liip\ImagineBundle\Utility\OptionsResolver\OptionsResolver; |
18
|
|
|
use Liip\ImagineBundle\Exception\InvalidArgumentException; |
19
|
|
|
use Symfony\Component\OptionsResolver\Options; |
20
|
|
|
use Symfony\Component\OptionsResolver\Exception\ExceptionInterface; |
21
|
|
|
|
22
|
|
|
class ResampleFilterLoader implements LoaderInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var ImagineInterface |
26
|
|
|
*/ |
27
|
|
|
private $imagine; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param ImagineInterface $imagine |
31
|
|
|
*/ |
32
|
|
|
public function __construct(ImagineInterface $imagine) |
33
|
|
|
{ |
34
|
|
|
$this->imagine = $imagine; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param ImageInterface $image |
39
|
|
|
* @param array $options |
40
|
|
|
* |
41
|
|
|
* @throws LoadFilterException |
42
|
|
|
* |
43
|
|
|
* @return ImageInterface |
44
|
|
|
*/ |
45
|
|
|
public function load(ImageInterface $image, array $options = array()) |
46
|
|
|
{ |
47
|
|
|
$options = $this->resolveOptions($options); |
48
|
|
|
$tmpFile = $this->getTemporaryFile($options['temp_dir']); |
49
|
|
|
|
50
|
|
|
try { |
51
|
|
|
$image->save($tmpFile, $this->getImagineSaveOptions($options)); |
52
|
|
|
$image = $this->imagine->open($tmpFile); |
53
|
|
|
$this->delTemporaryFile($tmpFile); |
54
|
|
|
} catch (\Exception $exception) { |
55
|
|
|
$this->delTemporaryFile($tmpFile); |
56
|
|
|
throw new LoadFilterException('Unable to save/open file in resample filter loader.', null, $exception); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $image; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $path |
64
|
|
|
* |
65
|
|
|
* @throws \RuntimeException |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
private function getTemporaryFile($path) |
70
|
|
|
{ |
71
|
|
|
if (!is_dir($path) || false === $file = tempnam($path, 'liip-imagine-bundle')) { |
72
|
|
|
throw new \RuntimeException(sprintf('Unable to create temporary file in "%s" base path.', $path)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $file; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param $file |
80
|
|
|
* |
81
|
|
|
* @throws \RuntimeException |
82
|
|
|
*/ |
83
|
|
|
private function delTemporaryFile($file) |
84
|
|
|
{ |
85
|
|
|
if (file_exists($file)) { |
86
|
|
|
unlink($file); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param array $options |
92
|
|
|
* |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
private function getImagineSaveOptions(array $options) |
96
|
|
|
{ |
97
|
|
|
$saveOptions = array( |
98
|
|
|
'resolution-units' => $options['unit'], |
99
|
|
|
'resolution-x' => $options['x'], |
100
|
|
|
'resolution-y' => $options['y'], |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
if (isset($options['filter'])) { |
104
|
|
|
$saveOptions['resampling-filter'] = $options['filter']; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $saveOptions; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param array $options |
112
|
|
|
* |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
|
|
private function resolveOptions(array $options) |
116
|
|
|
{ |
117
|
|
|
$resolver = new OptionsResolver(); |
|
|
|
|
118
|
|
|
$resolver->setDefined(array('filter')); |
119
|
|
|
$resolver->setRequired(array('x', 'y', 'unit', 'temp_dir')); |
120
|
|
|
|
121
|
|
|
$resolver->setDefault('temp_dir', sys_get_temp_dir()); |
122
|
|
|
$resolver->setAllowedTypes('temp_dir', array('string')); |
123
|
|
|
$resolver->setAllowedTypes('x', array('int')); |
124
|
|
|
$resolver->setAllowedTypes('y', array('int')); |
125
|
|
|
|
126
|
|
|
$resolver->setAllowedValues('unit', array('inch', 'centimeter')); |
127
|
|
|
$resolver->setNormalizer('unit', function (Options $options, $value) { |
128
|
|
|
return $this->normalizeOptionUnit($value); |
129
|
|
|
}); |
130
|
|
|
|
131
|
|
|
$resolver->setDefault('filter', 'UNDEFINED'); |
132
|
|
|
$resolver->setAllowedTypes('filter', array('string')); |
133
|
|
|
$resolver->setNormalizer('filter', function (Options $options, $value) { |
134
|
|
|
return $this->normalizeOptionFilter($value); |
135
|
|
|
}); |
136
|
|
|
|
137
|
|
|
try { |
138
|
|
|
return $resolver->resolve($options); |
139
|
|
|
} catch (ExceptionInterface $e) { |
140
|
|
|
throw new InvalidArgumentException('Invalid options provided. The "units" option must be set to "inch" '. |
141
|
|
|
'or "centimeter" and you must set a "x" and "y" value to the ppi/ppc desired.'); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string $value |
147
|
|
|
* |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
private function normalizeOptionUnit($value) |
151
|
|
|
{ |
152
|
|
|
return $value === 'inch' ? ImageInterface::RESOLUTION_PIXELSPERINCH : ImageInterface::RESOLUTION_PIXELSPERCENTIMETER; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param string $value |
157
|
|
|
* |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
|
|
private function normalizeOptionFilter($value) |
161
|
|
|
{ |
162
|
|
|
foreach (array('\Imagine\Image\ImageInterface::FILTER_%s', '\Imagine\Image\ImageInterface::%s', '\%s') as $format) { |
163
|
|
|
if (defined($constant = sprintf($format, strtoupper($value)))) { |
164
|
|
|
return constant($constant); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
throw new InvalidArgumentException('The "filter" option must resolve to a valid constant using one of the '. |
169
|
|
|
'following formats: "\Imagine\Image\ImageInterface::FILTER_%s" or "\Imagine\Image\ImageInterface::%s" '. |
170
|
|
|
'or "\%s"'); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
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.