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 mixed |
||
73 | */ |
||
74 | 7 | public function generate(MediaInterface $media, ImageParameterBag $parameterBag) |
|
75 | { |
||
76 | 7 | $parameterBag->setDefaults($this->defaultImageParameters); |
|
77 | 7 | if (!$parameterBag->hasExtra('fit')) { |
|
78 | 7 | $parameterBag->addExtra('fit', 'crop-'.$media->getFocalPoint()); |
|
79 | } |
||
80 | |||
81 | 7 | $filename = $this->filenameGenerator->generate($media, $parameterBag); |
|
82 | |||
83 | 7 | if (!$this->server->getCache()->has($filename)) { |
|
84 | 6 | $this->generateImage($media, $parameterBag, $filename); |
|
85 | } |
||
86 | |||
87 | 5 | return $filename; |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * @param MediaInterface $media |
||
92 | * @param ImageParameterBag $parameterBag |
||
93 | * @param $filename |
||
94 | * @throws FilesystemException |
||
95 | * @throws \Exception |
||
96 | */ |
||
97 | 6 | protected function generateImage(MediaInterface $media, ImageParameterBag $parameterBag, $filename) |
|
121 | |||
122 | /** |
||
123 | * @param MediaInterface $media |
||
124 | * @return string |
||
125 | * @throws FilesystemException |
||
126 | */ |
||
127 | 6 | protected function getImageData(MediaInterface $media) |
|
128 | { |
||
129 | 6 | if ($this->server->getSource()->has($media->getImage())) { |
|
130 | 6 | return $this->server->getSource()->read($media->getImage()); |
|
131 | } |
||
132 | |||
133 | if (!is_null($this->fallbackImage)) { |
||
134 | return file_get_contents($this->fallbackImage); |
||
135 | } |
||
136 | |||
137 | throw new FilesystemException('File not found'); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @param MediaInterface $media |
||
142 | * @param string $tmp |
||
143 | * @param ImageParameterBag $parameterBag |
||
144 | * @return string |
||
145 | */ |
||
146 | 6 | protected function doGenerateImage(MediaInterface $media, $tmp, ImageParameterBag $parameterBag) |
|
154 | |||
155 | /** |
||
156 | * @return string |
||
157 | */ |
||
158 | 6 | protected function getTemporaryFile() |
|
159 | { |
||
160 | 6 | if (empty($this->tmpPath)) { |
|
161 | 5 | $this->tmpPath = sys_get_temp_dir(); |
|
162 | } |
||
163 | 6 | if (empty($this->tmpPrefix)) { |
|
164 | 6 | $this->tmpPrefix = 'media'; |
|
165 | } |
||
166 | |||
167 | 6 | $this->disableErrorHandler(); |
|
168 | 6 | $tempFile = tempnam($this->tmpPath, $this->tmpPrefix); |
|
169 | 6 | $this->restoreErrorHandler(); |
|
170 | |||
171 | 6 | return $tempFile; |
|
172 | } |
||
173 | |||
174 | /** |
||
175 | * @return Server |
||
176 | */ |
||
177 | 1 | public function getServer() |
|
181 | |||
182 | /** |
||
183 | * @return FilenameGeneratorInterface |
||
184 | */ |
||
185 | 1 | public function getFilenameGenerator() |
|
189 | |||
190 | /** |
||
191 | * @return array |
||
192 | */ |
||
193 | 1 | public function getDefaultImageParameters() |
|
197 | |||
198 | /** |
||
199 | * @return string |
||
200 | */ |
||
201 | 1 | public function getTmpPath() |
|
205 | |||
206 | /** |
||
207 | * @return string |
||
208 | */ |
||
209 | 1 | public function getTmpPrefix() |
|
213 | |||
214 | /** |
||
215 | * @return string |
||
216 | */ |
||
217 | 1 | public function getFallbackImage() |
|
221 | } |
||
222 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.