1 | <?php |
||
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( |
|
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 |
|
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 |
|
159 | |||
160 | /** |
||
161 | * @return string |
||
162 | */ |
||
163 | 6 | protected function getTemporaryFile(): string |
|
178 | |||
179 | /** |
||
180 | * @return Server |
||
181 | */ |
||
182 | 1 | public function getServer(): Server |
|
186 | |||
187 | /** |
||
188 | * @return FilenameGeneratorInterface |
||
189 | */ |
||
190 | 1 | public function getFilenameGenerator(): FilenameGeneratorInterface |
|
194 | |||
195 | /** |
||
196 | * @return array |
||
197 | */ |
||
198 | 1 | public function getDefaultImageParameters(): array |
|
202 | |||
203 | /** |
||
204 | * @return string |
||
205 | */ |
||
206 | 1 | public function getTmpPath(): string |
|
210 | |||
211 | /** |
||
212 | * @return string |
||
213 | */ |
||
214 | 1 | public function getTmpPrefix(): string |
|
218 | |||
219 | /** |
||
220 | * @return string |
||
221 | */ |
||
222 | 1 | public function getFallbackImage(): string |
|
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.