1 | <?php |
||
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 | 4 | public function __construct( |
|
65 | |||
66 | /** |
||
67 | * @param MediaInterface $media |
||
68 | * @param ParameterBag $parameterBag |
||
69 | * @return mixed |
||
70 | */ |
||
71 | 4 | public function generate(MediaInterface $media, ParameterBag $parameterBag) |
|
72 | { |
||
73 | 4 | $parameterBag->setDefaults($this->defaultImageParameters); |
|
74 | |||
75 | 4 | $filename = $this->filenameGenerator->generate($media, $parameterBag); |
|
76 | |||
77 | 4 | if (!$this->server->getSource()->has($filename)) { |
|
78 | 3 | $this->generateImage($media, $parameterBag, $filename); |
|
79 | 3 | } |
|
80 | |||
81 | 4 | return $filename; |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * @param MediaInterface $media |
||
86 | * @param ParameterBag $parameterBag |
||
87 | * @param $filename |
||
88 | * @throws FilesystemException |
||
89 | * @throws \Exception |
||
90 | */ |
||
91 | 3 | protected function generateImage(MediaInterface $media, ParameterBag $parameterBag, $filename) |
|
92 | { |
||
93 | 3 | $tmp = $this->getTemporaryFile(); |
|
94 | 3 | $imageData = $this->getImageData($media); |
|
95 | |||
96 | 3 | if (@file_put_contents($tmp, $imageData) === false) { |
|
97 | throw new FilesystemException('Unable to write temporary file'); |
||
98 | } |
||
99 | |||
100 | try { |
||
101 | 3 | $this->server->getCache()->put($filename, $this->doGenerateImage($media, $tmp, $parameterBag)); |
|
102 | 3 | } catch (\Exception $e) { |
|
103 | throw new \Exception('Could not generate image', 0, $e); |
||
104 | 3 | } finally { |
|
105 | 3 | if (file_exists($tmp)) { |
|
106 | 3 | if (!@unlink($tmp)) { |
|
107 | throw new FilesystemException('Unable to clean up temporary file'); |
||
108 | } |
||
109 | 3 | } |
|
110 | } |
||
111 | 3 | } |
|
112 | |||
113 | /** |
||
114 | * @param MediaInterface $media |
||
115 | * @return string |
||
116 | * @throws FilesystemException |
||
117 | */ |
||
118 | 3 | protected function getImageData(MediaInterface $media) |
|
119 | { |
||
120 | 3 | if ($this->server->getSource()->has($media->getImage())) { |
|
121 | 3 | 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 string $tmp |
||
134 | * @param ParameterBag $parameterBag |
||
135 | * @return string |
||
136 | */ |
||
137 | 3 | protected function doGenerateImage(MediaInterface $media, $tmp, ParameterBag $parameterBag) |
|
|
|||
138 | { |
||
139 | 3 | $parameters = $parameterBag->getExtra(); |
|
140 | 3 | $parameters['w'] = $parameterBag->getWidth(); |
|
141 | 3 | $parameters['h'] = $parameterBag->getHeight(); |
|
142 | |||
143 | 3 | return $this->server->getApi()->run($tmp, $parameters); |
|
144 | } |
||
145 | |||
146 | /** |
||
147 | * @return string |
||
148 | */ |
||
149 | 3 | protected function getTemporaryFile() |
|
150 | { |
||
151 | 3 | if (empty($this->tmpPath)) { |
|
152 | 3 | $this->tmpPath = sys_get_temp_dir(); |
|
153 | 3 | } |
|
154 | 3 | if (empty($this->tmpPrefix)) { |
|
155 | 3 | $this->tmpPrefix = 'media'; |
|
156 | 3 | } |
|
157 | |||
158 | 3 | return tempnam($this->tmpPath, $this->tmpPrefix); |
|
159 | } |
||
160 | |||
161 | /** |
||
162 | * @return Server |
||
163 | */ |
||
164 | public function getServer() |
||
168 | |||
169 | /** |
||
170 | * @return FilenameGeneratorInterface |
||
171 | */ |
||
172 | public function getFilenameGenerator() |
||
176 | |||
177 | /** |
||
178 | * @return string |
||
179 | */ |
||
180 | public function getTmpPath() |
||
184 | |||
185 | /** |
||
186 | * @return string |
||
187 | */ |
||
188 | public function getTmpPrefix() |
||
192 | |||
193 | /** |
||
194 | * @return string |
||
195 | */ |
||
196 | public function getFallbackImage() |
||
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.