1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MediaMonks\SonataMediaBundle\Generator; |
4
|
|
|
|
5
|
|
|
use League\Glide\Filesystem\FilesystemException; |
6
|
|
|
use League\Glide\Server; |
7
|
|
|
use MediaMonks\SonataMediaBundle\ErrorHandlerTrait; |
8
|
|
|
use MediaMonks\SonataMediaBundle\ParameterBag\ImageParameterBag; |
9
|
|
|
use MediaMonks\SonataMediaBundle\Model\MediaInterface; |
10
|
|
|
|
11
|
|
|
class ImageGenerator |
12
|
|
|
{ |
13
|
|
|
use ErrorHandlerTrait; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var Server |
17
|
|
|
*/ |
18
|
|
|
private $server; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var FilenameGeneratorInterface |
22
|
|
|
*/ |
23
|
|
|
private $filenameGenerator; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private $defaultImageParameters; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $tmpPath; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $tmpPrefix; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $fallbackImage; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param Server $server |
47
|
|
|
* @param FilenameGeneratorInterface $filenameGenerator |
48
|
|
|
* @param array $defaultImageParameters |
49
|
|
|
* @param null $fallbackImage |
50
|
|
|
* @param null $tmpPath |
51
|
|
|
* @param null $tmpPrefix |
52
|
|
|
*/ |
53
|
8 |
|
public function __construct( |
54
|
|
|
Server $server, |
55
|
|
|
FilenameGeneratorInterface $filenameGenerator, |
56
|
|
|
$defaultImageParameters = [], |
57
|
|
|
$fallbackImage = null, |
58
|
|
|
$tmpPath = null, |
59
|
|
|
$tmpPrefix = null |
60
|
|
|
) { |
61
|
8 |
|
$this->server = $server; |
62
|
8 |
|
$this->filenameGenerator = $filenameGenerator; |
63
|
8 |
|
$this->defaultImageParameters = $defaultImageParameters; |
64
|
8 |
|
$this->fallbackImage = $fallbackImage; |
65
|
8 |
|
$this->tmpPath = $tmpPath; |
66
|
8 |
|
$this->tmpPrefix = $tmpPrefix; |
67
|
8 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param MediaInterface $media |
71
|
|
|
* @param ImageParameterBag $parameterBag |
72
|
|
|
* @return string |
73
|
|
|
* @throws FilesystemException |
74
|
|
|
*/ |
75
|
7 |
|
public function generate(MediaInterface $media, ImageParameterBag $parameterBag): string |
76
|
|
|
{ |
77
|
7 |
|
$parameterBag->setDefaults($this->defaultImageParameters); |
78
|
7 |
|
if (!$parameterBag->hasExtra('fit')) { |
79
|
7 |
|
$parameterBag->addExtra('fit', 'crop-'.$media->getFocalPoint()); |
80
|
|
|
} |
81
|
7 |
|
if (!$parameterBag->hasExtra('fm') && isset($media->getProviderMetaData()['originalExtension'])) { |
82
|
4 |
|
$parameterBag->addExtra('fm', $media->getProviderMetaData()['originalExtension']); |
83
|
|
|
} |
84
|
|
|
|
85
|
7 |
|
$filename = $this->filenameGenerator->generate($media, $parameterBag); |
86
|
|
|
|
87
|
7 |
|
if (!$this->server->getCache()->has($filename)) { |
88
|
6 |
|
$this->generateImage($media, $parameterBag, $filename); |
89
|
|
|
} |
90
|
|
|
|
91
|
5 |
|
return $filename; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param MediaInterface $media |
96
|
|
|
* @param ImageParameterBag $parameterBag |
97
|
|
|
* @param $filename |
98
|
|
|
* |
99
|
|
|
* @throws FilesystemException |
100
|
|
|
* @throws \Exception |
101
|
|
|
*/ |
102
|
6 |
|
protected function generateImage(MediaInterface $media, ImageParameterBag $parameterBag, string $filename) |
103
|
|
|
{ |
104
|
6 |
|
$tmp = $this->getTemporaryFile(); |
105
|
6 |
|
$imageData = $this->getImageData($media); |
106
|
|
|
|
107
|
6 |
|
$this->disableErrorHandler(); |
108
|
6 |
|
if (file_put_contents($tmp, $imageData) === false) { |
109
|
|
|
$this->restoreErrorHandler(); |
110
|
|
|
throw new FilesystemException('Unable to write temporary file'); |
111
|
|
|
} |
112
|
6 |
|
$this->restoreErrorHandler(); |
113
|
|
|
|
114
|
|
|
try { |
115
|
6 |
|
$this->server->getCache()->put($filename, $this->doGenerateImage($media, $tmp, $parameterBag)); |
116
|
2 |
|
} catch (\Exception $e) { |
117
|
2 |
|
throw new FilesystemException('Could not generate image', 0, $e); |
118
|
4 |
|
} finally { |
119
|
6 |
|
if (file_exists($tmp)) { |
120
|
6 |
|
if (!@unlink($tmp)) { |
121
|
6 |
|
throw new FilesystemException('Unable to clean up temporary file'); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
4 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param MediaInterface $media |
129
|
|
|
* @return string |
130
|
|
|
* @throws FilesystemException |
131
|
|
|
*/ |
132
|
6 |
|
protected function getImageData(MediaInterface $media): string |
133
|
|
|
{ |
134
|
6 |
|
if ($this->server->getSource()->has($media->getImage())) { |
135
|
6 |
|
return $this->server->getSource()->read($media->getImage()); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if (!is_null($this->fallbackImage)) { |
139
|
|
|
return file_get_contents($this->fallbackImage); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
throw new FilesystemException('File not found'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param MediaInterface $media |
147
|
|
|
* @param string $tmp |
148
|
|
|
* @param ImageParameterBag $parameterBag |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
6 |
|
protected function doGenerateImage(MediaInterface $media, $tmp, ImageParameterBag $parameterBag): string |
|
|
|
|
152
|
|
|
{ |
153
|
6 |
|
$parameters = $parameterBag->getExtra(); |
154
|
6 |
|
$parameters['w'] = $parameterBag->getWidth(); |
155
|
6 |
|
$parameters['h'] = $parameterBag->getHeight(); |
156
|
|
|
|
157
|
6 |
|
return $this->server->getApi()->run($tmp, $parameters); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
6 |
|
protected function getTemporaryFile(): string |
164
|
|
|
{ |
165
|
6 |
|
if (empty($this->tmpPath)) { |
166
|
5 |
|
$this->tmpPath = sys_get_temp_dir(); |
167
|
|
|
} |
168
|
6 |
|
if (empty($this->tmpPrefix)) { |
169
|
6 |
|
$this->tmpPrefix = 'media'; |
170
|
|
|
} |
171
|
|
|
|
172
|
6 |
|
$this->disableErrorHandler(); |
173
|
6 |
|
$tempFile = tempnam($this->tmpPath, $this->tmpPrefix); |
174
|
6 |
|
$this->restoreErrorHandler(); |
175
|
|
|
|
176
|
6 |
|
return $tempFile; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return Server |
181
|
|
|
*/ |
182
|
1 |
|
public function getServer(): Server |
183
|
|
|
{ |
184
|
1 |
|
return $this->server; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @return FilenameGeneratorInterface |
189
|
|
|
*/ |
190
|
1 |
|
public function getFilenameGenerator(): FilenameGeneratorInterface |
191
|
|
|
{ |
192
|
1 |
|
return $this->filenameGenerator; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return array |
197
|
|
|
*/ |
198
|
1 |
|
public function getDefaultImageParameters(): array |
199
|
|
|
{ |
200
|
1 |
|
return $this->defaultImageParameters; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @return string |
205
|
|
|
*/ |
206
|
1 |
|
public function getTmpPath(): string |
207
|
|
|
{ |
208
|
1 |
|
return $this->tmpPath; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @return string |
213
|
|
|
*/ |
214
|
1 |
|
public function getTmpPrefix(): string |
215
|
|
|
{ |
216
|
1 |
|
return $this->tmpPrefix; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @return string |
221
|
|
|
*/ |
222
|
1 |
|
public function getFallbackImage(): string |
223
|
|
|
{ |
224
|
1 |
|
return $this->fallbackImage; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.