/
FilterManager.php
265 lines (230 loc) · 7.69 KB
/
FilterManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?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;
use Imagine\Image\ImageInterface;
use Imagine\Image\ImagineInterface;
use Liip\ImagineBundle\Binary\BinaryInterface;
use Liip\ImagineBundle\Binary\FileBinaryInterface;
use Liip\ImagineBundle\Binary\MimeTypeGuesserInterface;
use Liip\ImagineBundle\Imagine\Filter\Loader\LoaderInterface;
use Liip\ImagineBundle\Imagine\Filter\PostProcessor\PostProcessorInterface;
use Liip\ImagineBundle\Model\Binary;
class FilterManager
{
/**
* @var FilterConfiguration
*/
protected $filterConfig;
/**
* @var ImagineInterface
*/
protected $imagine;
/**
* @var MimeTypeGuesserInterface
*/
protected $mimeTypeGuesser;
/**
* @var LoaderInterface[]
*/
protected $loaders = [];
/**
* @var PostProcessorInterface[]
*/
protected $postProcessors = [];
/**
* @param FilterConfiguration $filterConfig
* @param ImagineInterface $imagine
* @param MimeTypeGuesserInterface $mimeTypeGuesser
*/
public function __construct(FilterConfiguration $filterConfig, ImagineInterface $imagine, MimeTypeGuesserInterface $mimeTypeGuesser)
{
$this->filterConfig = $filterConfig;
$this->imagine = $imagine;
$this->mimeTypeGuesser = $mimeTypeGuesser;
}
/**
* Adds a loader to handle the given filter.
*
* @param string $filter
* @param LoaderInterface $loader
*/
public function addLoader(string $filter, LoaderInterface $loader): void
{
$this->loaders[$filter] = $loader;
}
/**
* Adds a post-processor to handle binaries.
*
* @param string $name
* @param PostProcessorInterface $postProcessor
*/
public function addPostProcessor(string $name, PostProcessorInterface $postProcessor): void
{
$this->postProcessors[$name] = $postProcessor;
}
/**
* @return FilterConfiguration
*/
public function getFilterConfiguration(): FilterConfiguration
{
return $this->filterConfig;
}
/**
* @param BinaryInterface $binary
* @param array $config
*
* @throws \InvalidArgumentException
*
* @return BinaryInterface
*/
public function apply(BinaryInterface $binary, array $config): BinaryInterface
{
$config += [
'quality' => 100,
'animated' => false,
];
return $this->applyPostProcessors($this->applyFilters($binary, $config), $config);
}
/**
* @param BinaryInterface $binary
* @param array $config
*
* @return BinaryInterface
*/
public function applyFilters(BinaryInterface $binary, array $config): BinaryInterface
{
if ($binary instanceof FileBinaryInterface) {
$image = $this->imagine->open($binary->getPath());
} else {
$image = $this->imagine->load($binary->getContent());
}
foreach ($this->sanitizeFilters($config['filters'] ?? []) as $name => $options) {
$prior = $image;
$image = $this->loaders[$name]->load($image, $options);
if ($prior !== $image) {
$this->destroyImage($prior);
}
}
return $this->exportConfiguredImageBinary($binary, $image, $config);
}
/**
* Apply the provided filter set on the given binary.
*
* @param BinaryInterface $binary
* @param string $filter
* @param array $runtimeConfig
*
* @throws \InvalidArgumentException
*
* @return BinaryInterface
*/
public function applyFilter(BinaryInterface $binary, $filter, array $runtimeConfig = [])
{
$config = array_replace_recursive(
$this->getFilterConfiguration()->get($filter),
$runtimeConfig
);
return $this->apply($binary, $config);
}
/**
* @param BinaryInterface $binary
* @param array $config
*
* @throws \InvalidArgumentException
*
* @return BinaryInterface
*/
public function applyPostProcessors(BinaryInterface $binary, array $config): BinaryInterface
{
foreach ($this->sanitizePostProcessors($config['post_processors'] ?? []) as $name => $options) {
$binary = $this->postProcessors[$name]->process($binary, $options);
}
return $binary;
}
/**
* @param BinaryInterface $binary
* @param ImageInterface $image
* @param array $config
*
* @return BinaryInterface
*/
private function exportConfiguredImageBinary(BinaryInterface $binary, ImageInterface $image, array $config): BinaryInterface
{
$options = [
'quality' => $config['quality'],
];
if (isset($config['jpeg_quality'])) {
$options['jpeg_quality'] = $config['jpeg_quality'];
}
if (isset($config['png_compression_level'])) {
$options['png_compression_level'] = $config['png_compression_level'];
}
if (isset($config['png_compression_filter'])) {
$options['png_compression_filter'] = $config['png_compression_filter'];
}
if ('gif' === $binary->getFormat() && $config['animated']) {
$options['animated'] = $config['animated'];
}
$filteredFormat = $config['format'] ?? $binary->getFormat();
$filteredString = $image->get($filteredFormat, $options);
$this->destroyImage($image);
return new Binary(
$filteredString,
$filteredFormat === $binary->getFormat() ? $binary->getMimeType() : $this->mimeTypeGuesser->guess($filteredString),
$filteredFormat
);
}
/**
* @param array $filters
*
* @return array
*/
private function sanitizeFilters(array $filters): array
{
$sanitized = array_filter($filters, function (string $name): bool {
return isset($this->loaders[$name]);
}, ARRAY_FILTER_USE_KEY);
if (\count($filters) !== \count($sanitized)) {
throw new \InvalidArgumentException(sprintf('Could not find filter(s): %s', implode(', ', array_map(function (string $name): string {
return sprintf('"%s"', $name);
}, array_diff(array_keys($filters), array_keys($sanitized))))));
}
return $sanitized;
}
/**
* @param array $processors
*
* @return array
*/
private function sanitizePostProcessors(array $processors): array
{
$sanitized = array_filter($processors, function (string $name): bool {
return isset($this->postProcessors[$name]);
}, ARRAY_FILTER_USE_KEY);
if (\count($processors) !== \count($sanitized)) {
throw new \InvalidArgumentException(sprintf('Could not find post processor(s): %s', implode(', ', array_map(function (string $name): string {
return sprintf('"%s"', $name);
}, array_diff(array_keys($processors), array_keys($sanitized))))));
}
return $sanitized;
}
/**
* We are done with the image object so we can destruct the this because imagick keeps consuming memory if we don't.
* See https://github.com/liip/LiipImagineBundle/pull/682
*
* @param ImageInterface $image
*/
private function destroyImage(ImageInterface $image): void
{
if (method_exists($image, '__destruct')) {
$image->__destruct();
}
}
}