-
Notifications
You must be signed in to change notification settings - Fork 380
/
FilterManager.php
211 lines (182 loc) · 6.42 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
<?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\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\ConfigurablePostProcessorInterface;
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 = array();
/**
* @var PostProcessorInterface[]
*/
protected $postProcessors = array();
/**
* @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($filter, LoaderInterface $loader)
{
$this->loaders[$filter] = $loader;
}
/**
* Adds a post-processor to handle binaries.
*
* @param string $name
* @param PostProcessorInterface $postProcessor
*/
public function addPostProcessor($name, PostProcessorInterface $postProcessor)
{
$this->postProcessors[$name] = $postProcessor;
}
/**
* @return FilterConfiguration
*/
public function getFilterConfiguration()
{
return $this->filterConfig;
}
/**
* @param BinaryInterface $binary
* @param array $config
*
* @throws \InvalidArgumentException
*
* @return BinaryInterface
*/
public function apply(BinaryInterface $binary, array $config)
{
$config = array_replace(
array(
'filters' => array(),
'quality' => 100,
'animated' => false,
),
$config
);
if ($binary instanceof FileBinaryInterface) {
$image = $this->imagine->open($binary->getPath());
} else {
$image = $this->imagine->load($binary->getContent());
}
foreach ($config['filters'] as $eachFilter => $eachOptions) {
if (!isset($this->loaders[$eachFilter])) {
throw new \InvalidArgumentException(sprintf(
'Could not find filter loader for "%s" filter type', $eachFilter
));
}
$prevImage = $image;
$image = $this->loaders[$eachFilter]->load($image, $eachOptions);
// If the filter returns a different image object destruct the old one because imagick keeps consuming memory if we don't
// See https://github.com/liip/LiipImagineBundle/pull/682
if ($prevImage !== $image && method_exists($prevImage, '__destruct')) {
$prevImage->__destruct();
}
}
$options = array(
'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 ($binary->getFormat() === 'gif' && $config['animated']) {
$options['animated'] = $config['animated'];
}
$filteredFormat = isset($config['format']) ? $config['format'] : $binary->getFormat();
$filteredContent = $image->get($filteredFormat, $options);
$filteredMimeType = $filteredFormat === $binary->getFormat() ? $binary->getMimeType() : $this->mimeTypeGuesser->guess($filteredContent);
// 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
if (method_exists($image, '__destruct')) {
$image->__destruct();
}
return $this->applyPostProcessors(new Binary($filteredContent, $filteredMimeType, $filteredFormat), $config);
}
/**
* @param BinaryInterface $binary
* @param array $config
*
* @throws \InvalidArgumentException
*
* @return BinaryInterface
*/
public function applyPostProcessors(BinaryInterface $binary, $config)
{
$config += array('post_processors' => array());
foreach ($config['post_processors'] as $postProcessorName => $postProcessorOptions) {
if (!isset($this->postProcessors[$postProcessorName])) {
throw new \InvalidArgumentException(sprintf('Post-processor "%s" could not be found', $postProcessorName));
}
$binary = $this->postProcessors[$postProcessorName]->process($binary, $postProcessorOptions);
}
return $binary;
}
/**
* 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 = array())
{
$config = array_replace_recursive(
$this->getFilterConfiguration()->get($filter),
$runtimeConfig
);
return $this->apply($binary, $config);
}
}