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