@@ -44,413 +44,413 @@ |
||
44 | 44 | |
45 | 45 | class Generator { |
46 | 46 | |
47 | - /** @var IPreview */ |
|
48 | - private $previewManager; |
|
49 | - /** @var IConfig */ |
|
50 | - private $config; |
|
51 | - /** @var IAppData */ |
|
52 | - private $appData; |
|
53 | - /** @var GeneratorHelper */ |
|
54 | - private $helper; |
|
55 | - /** @var EventDispatcherInterface */ |
|
56 | - private $eventDispatcher; |
|
57 | - |
|
58 | - /** |
|
59 | - * @param IConfig $config |
|
60 | - * @param IPreview $previewManager |
|
61 | - * @param IAppData $appData |
|
62 | - * @param GeneratorHelper $helper |
|
63 | - * @param EventDispatcherInterface $eventDispatcher |
|
64 | - */ |
|
65 | - public function __construct( |
|
66 | - IConfig $config, |
|
67 | - IPreview $previewManager, |
|
68 | - IAppData $appData, |
|
69 | - GeneratorHelper $helper, |
|
70 | - EventDispatcherInterface $eventDispatcher |
|
71 | - ) { |
|
72 | - $this->config = $config; |
|
73 | - $this->previewManager = $previewManager; |
|
74 | - $this->appData = $appData; |
|
75 | - $this->helper = $helper; |
|
76 | - $this->eventDispatcher = $eventDispatcher; |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * Returns a preview of a file |
|
81 | - * |
|
82 | - * The cache is searched first and if nothing usable was found then a preview is |
|
83 | - * generated by one of the providers |
|
84 | - * |
|
85 | - * @param File $file |
|
86 | - * @param int $width |
|
87 | - * @param int $height |
|
88 | - * @param bool $crop |
|
89 | - * @param string $mode |
|
90 | - * @param string $mimeType |
|
91 | - * @return ISimpleFile |
|
92 | - * @throws NotFoundException |
|
93 | - * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) |
|
94 | - */ |
|
95 | - public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) { |
|
96 | - //Make sure that we can read the file |
|
97 | - if (!$file->isReadable()) { |
|
98 | - throw new NotFoundException('Cannot read file'); |
|
99 | - } |
|
100 | - |
|
101 | - |
|
102 | - $this->eventDispatcher->dispatch( |
|
103 | - IPreview::EVENT, |
|
104 | - new GenericEvent($file, [ |
|
105 | - 'width' => $width, |
|
106 | - 'height' => $height, |
|
107 | - 'crop' => $crop, |
|
108 | - 'mode' => $mode |
|
109 | - ]) |
|
110 | - ); |
|
111 | - |
|
112 | - if ($mimeType === null) { |
|
113 | - $mimeType = $file->getMimeType(); |
|
114 | - } |
|
115 | - if (!$this->previewManager->isMimeSupported($mimeType)) { |
|
116 | - throw new NotFoundException(); |
|
117 | - } |
|
118 | - |
|
119 | - $previewFolder = $this->getPreviewFolder($file); |
|
120 | - |
|
121 | - $previewVersion = ''; |
|
122 | - if ($file instanceof IVersionedPreviewFile) { |
|
123 | - $previewVersion = $file->getPreviewVersion() . '-'; |
|
124 | - } |
|
125 | - |
|
126 | - // Get the max preview and infer the max preview sizes from that |
|
127 | - $maxPreview = $this->getMaxPreview($previewFolder, $file, $mimeType, $previewVersion); |
|
128 | - if ($maxPreview->getSize() === 0) { |
|
129 | - $maxPreview->delete(); |
|
130 | - throw new NotFoundException('Max preview size 0, invalid!'); |
|
131 | - } |
|
132 | - |
|
133 | - list($maxWidth, $maxHeight) = $this->getPreviewSize($maxPreview, $previewVersion); |
|
134 | - |
|
135 | - // If both width and heigth are -1 we just want the max preview |
|
136 | - if ($width === -1 && $height === -1) { |
|
137 | - $width = $maxWidth; |
|
138 | - $height = $maxHeight; |
|
139 | - } |
|
140 | - |
|
141 | - // Calculate the preview size |
|
142 | - list($width, $height) = $this->calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight); |
|
143 | - |
|
144 | - // No need to generate a preview that is just the max preview |
|
145 | - if ($width === $maxWidth && $height === $maxHeight) { |
|
146 | - return $maxPreview; |
|
147 | - } |
|
148 | - |
|
149 | - // Try to get a cached preview. Else generate (and store) one |
|
150 | - try { |
|
151 | - try { |
|
152 | - $preview = $this->getCachedPreview($previewFolder, $width, $height, $crop, $maxPreview->getMimeType(), $previewVersion); |
|
153 | - } catch (NotFoundException $e) { |
|
154 | - $preview = $this->generatePreview($previewFolder, $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight, $previewVersion); |
|
155 | - } |
|
156 | - } catch (\InvalidArgumentException $e) { |
|
157 | - throw new NotFoundException(); |
|
158 | - } |
|
159 | - |
|
160 | - if ($preview->getSize() === 0) { |
|
161 | - $preview->delete(); |
|
162 | - throw new NotFoundException('Cached preview size 0, invalid!'); |
|
163 | - } |
|
164 | - |
|
165 | - return $preview; |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * @param ISimpleFolder $previewFolder |
|
170 | - * @param File $file |
|
171 | - * @param string $mimeType |
|
172 | - * @param string $prefix |
|
173 | - * @return ISimpleFile |
|
174 | - * @throws NotFoundException |
|
175 | - */ |
|
176 | - private function getMaxPreview(ISimpleFolder $previewFolder, File $file, $mimeType, $prefix) { |
|
177 | - $nodes = $previewFolder->getDirectoryListing(); |
|
178 | - |
|
179 | - foreach ($nodes as $node) { |
|
180 | - $name = $node->getName(); |
|
181 | - if (($prefix === '' || strpos($name, $prefix) === 0) && strpos($name, 'max')) { |
|
182 | - return $node; |
|
183 | - } |
|
184 | - } |
|
185 | - |
|
186 | - $previewProviders = $this->previewManager->getProviders(); |
|
187 | - foreach ($previewProviders as $supportedMimeType => $providers) { |
|
188 | - if (!preg_match($supportedMimeType, $mimeType)) { |
|
189 | - continue; |
|
190 | - } |
|
191 | - |
|
192 | - foreach ($providers as $providerClosure) { |
|
193 | - $provider = $this->helper->getProvider($providerClosure); |
|
194 | - if (!($provider instanceof IProviderV2)) { |
|
195 | - continue; |
|
196 | - } |
|
197 | - |
|
198 | - if (!$provider->isAvailable($file)) { |
|
199 | - continue; |
|
200 | - } |
|
201 | - |
|
202 | - $maxWidth = (int)$this->config->getSystemValue('preview_max_x', 4096); |
|
203 | - $maxHeight = (int)$this->config->getSystemValue('preview_max_y', 4096); |
|
204 | - |
|
205 | - $preview = $this->helper->getThumbnail($provider, $file, $maxWidth, $maxHeight); |
|
206 | - |
|
207 | - if (!($preview instanceof IImage)) { |
|
208 | - continue; |
|
209 | - } |
|
210 | - |
|
211 | - // Try to get the extention. |
|
212 | - try { |
|
213 | - $ext = $this->getExtention($preview->dataMimeType()); |
|
214 | - } catch (\InvalidArgumentException $e) { |
|
215 | - // Just continue to the next iteration if this preview doesn't have a valid mimetype |
|
216 | - continue; |
|
217 | - } |
|
218 | - |
|
219 | - $path = $prefix . (string)$preview->width() . '-' . (string)$preview->height() . '-max.' . $ext; |
|
220 | - try { |
|
221 | - $file = $previewFolder->newFile($path); |
|
222 | - $file->putContent($preview->data()); |
|
223 | - } catch (NotPermittedException $e) { |
|
224 | - throw new NotFoundException(); |
|
225 | - } |
|
226 | - |
|
227 | - return $file; |
|
228 | - } |
|
229 | - } |
|
230 | - |
|
231 | - throw new NotFoundException(); |
|
232 | - } |
|
233 | - |
|
234 | - /** |
|
235 | - * @param ISimpleFile $file |
|
236 | - * @param string $prefix |
|
237 | - * @return int[] |
|
238 | - */ |
|
239 | - private function getPreviewSize(ISimpleFile $file, string $prefix = '') { |
|
240 | - $size = explode('-', substr($file->getName(), strlen($prefix))); |
|
241 | - return [(int)$size[0], (int)$size[1]]; |
|
242 | - } |
|
243 | - |
|
244 | - /** |
|
245 | - * @param int $width |
|
246 | - * @param int $height |
|
247 | - * @param bool $crop |
|
248 | - * @param string $mimeType |
|
249 | - * @param string $prefix |
|
250 | - * @return string |
|
251 | - */ |
|
252 | - private function generatePath($width, $height, $crop, $mimeType, $prefix) { |
|
253 | - $path = $prefix . (string)$width . '-' . (string)$height; |
|
254 | - if ($crop) { |
|
255 | - $path .= '-crop'; |
|
256 | - } |
|
257 | - |
|
258 | - $ext = $this->getExtention($mimeType); |
|
259 | - $path .= '.' . $ext; |
|
260 | - return $path; |
|
261 | - } |
|
262 | - |
|
263 | - |
|
264 | - /** |
|
265 | - * @param int $width |
|
266 | - * @param int $height |
|
267 | - * @param bool $crop |
|
268 | - * @param string $mode |
|
269 | - * @param int $maxWidth |
|
270 | - * @param int $maxHeight |
|
271 | - * @return int[] |
|
272 | - */ |
|
273 | - private function calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight) { |
|
274 | - |
|
275 | - /* |
|
47 | + /** @var IPreview */ |
|
48 | + private $previewManager; |
|
49 | + /** @var IConfig */ |
|
50 | + private $config; |
|
51 | + /** @var IAppData */ |
|
52 | + private $appData; |
|
53 | + /** @var GeneratorHelper */ |
|
54 | + private $helper; |
|
55 | + /** @var EventDispatcherInterface */ |
|
56 | + private $eventDispatcher; |
|
57 | + |
|
58 | + /** |
|
59 | + * @param IConfig $config |
|
60 | + * @param IPreview $previewManager |
|
61 | + * @param IAppData $appData |
|
62 | + * @param GeneratorHelper $helper |
|
63 | + * @param EventDispatcherInterface $eventDispatcher |
|
64 | + */ |
|
65 | + public function __construct( |
|
66 | + IConfig $config, |
|
67 | + IPreview $previewManager, |
|
68 | + IAppData $appData, |
|
69 | + GeneratorHelper $helper, |
|
70 | + EventDispatcherInterface $eventDispatcher |
|
71 | + ) { |
|
72 | + $this->config = $config; |
|
73 | + $this->previewManager = $previewManager; |
|
74 | + $this->appData = $appData; |
|
75 | + $this->helper = $helper; |
|
76 | + $this->eventDispatcher = $eventDispatcher; |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * Returns a preview of a file |
|
81 | + * |
|
82 | + * The cache is searched first and if nothing usable was found then a preview is |
|
83 | + * generated by one of the providers |
|
84 | + * |
|
85 | + * @param File $file |
|
86 | + * @param int $width |
|
87 | + * @param int $height |
|
88 | + * @param bool $crop |
|
89 | + * @param string $mode |
|
90 | + * @param string $mimeType |
|
91 | + * @return ISimpleFile |
|
92 | + * @throws NotFoundException |
|
93 | + * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) |
|
94 | + */ |
|
95 | + public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) { |
|
96 | + //Make sure that we can read the file |
|
97 | + if (!$file->isReadable()) { |
|
98 | + throw new NotFoundException('Cannot read file'); |
|
99 | + } |
|
100 | + |
|
101 | + |
|
102 | + $this->eventDispatcher->dispatch( |
|
103 | + IPreview::EVENT, |
|
104 | + new GenericEvent($file, [ |
|
105 | + 'width' => $width, |
|
106 | + 'height' => $height, |
|
107 | + 'crop' => $crop, |
|
108 | + 'mode' => $mode |
|
109 | + ]) |
|
110 | + ); |
|
111 | + |
|
112 | + if ($mimeType === null) { |
|
113 | + $mimeType = $file->getMimeType(); |
|
114 | + } |
|
115 | + if (!$this->previewManager->isMimeSupported($mimeType)) { |
|
116 | + throw new NotFoundException(); |
|
117 | + } |
|
118 | + |
|
119 | + $previewFolder = $this->getPreviewFolder($file); |
|
120 | + |
|
121 | + $previewVersion = ''; |
|
122 | + if ($file instanceof IVersionedPreviewFile) { |
|
123 | + $previewVersion = $file->getPreviewVersion() . '-'; |
|
124 | + } |
|
125 | + |
|
126 | + // Get the max preview and infer the max preview sizes from that |
|
127 | + $maxPreview = $this->getMaxPreview($previewFolder, $file, $mimeType, $previewVersion); |
|
128 | + if ($maxPreview->getSize() === 0) { |
|
129 | + $maxPreview->delete(); |
|
130 | + throw new NotFoundException('Max preview size 0, invalid!'); |
|
131 | + } |
|
132 | + |
|
133 | + list($maxWidth, $maxHeight) = $this->getPreviewSize($maxPreview, $previewVersion); |
|
134 | + |
|
135 | + // If both width and heigth are -1 we just want the max preview |
|
136 | + if ($width === -1 && $height === -1) { |
|
137 | + $width = $maxWidth; |
|
138 | + $height = $maxHeight; |
|
139 | + } |
|
140 | + |
|
141 | + // Calculate the preview size |
|
142 | + list($width, $height) = $this->calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight); |
|
143 | + |
|
144 | + // No need to generate a preview that is just the max preview |
|
145 | + if ($width === $maxWidth && $height === $maxHeight) { |
|
146 | + return $maxPreview; |
|
147 | + } |
|
148 | + |
|
149 | + // Try to get a cached preview. Else generate (and store) one |
|
150 | + try { |
|
151 | + try { |
|
152 | + $preview = $this->getCachedPreview($previewFolder, $width, $height, $crop, $maxPreview->getMimeType(), $previewVersion); |
|
153 | + } catch (NotFoundException $e) { |
|
154 | + $preview = $this->generatePreview($previewFolder, $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight, $previewVersion); |
|
155 | + } |
|
156 | + } catch (\InvalidArgumentException $e) { |
|
157 | + throw new NotFoundException(); |
|
158 | + } |
|
159 | + |
|
160 | + if ($preview->getSize() === 0) { |
|
161 | + $preview->delete(); |
|
162 | + throw new NotFoundException('Cached preview size 0, invalid!'); |
|
163 | + } |
|
164 | + |
|
165 | + return $preview; |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * @param ISimpleFolder $previewFolder |
|
170 | + * @param File $file |
|
171 | + * @param string $mimeType |
|
172 | + * @param string $prefix |
|
173 | + * @return ISimpleFile |
|
174 | + * @throws NotFoundException |
|
175 | + */ |
|
176 | + private function getMaxPreview(ISimpleFolder $previewFolder, File $file, $mimeType, $prefix) { |
|
177 | + $nodes = $previewFolder->getDirectoryListing(); |
|
178 | + |
|
179 | + foreach ($nodes as $node) { |
|
180 | + $name = $node->getName(); |
|
181 | + if (($prefix === '' || strpos($name, $prefix) === 0) && strpos($name, 'max')) { |
|
182 | + return $node; |
|
183 | + } |
|
184 | + } |
|
185 | + |
|
186 | + $previewProviders = $this->previewManager->getProviders(); |
|
187 | + foreach ($previewProviders as $supportedMimeType => $providers) { |
|
188 | + if (!preg_match($supportedMimeType, $mimeType)) { |
|
189 | + continue; |
|
190 | + } |
|
191 | + |
|
192 | + foreach ($providers as $providerClosure) { |
|
193 | + $provider = $this->helper->getProvider($providerClosure); |
|
194 | + if (!($provider instanceof IProviderV2)) { |
|
195 | + continue; |
|
196 | + } |
|
197 | + |
|
198 | + if (!$provider->isAvailable($file)) { |
|
199 | + continue; |
|
200 | + } |
|
201 | + |
|
202 | + $maxWidth = (int)$this->config->getSystemValue('preview_max_x', 4096); |
|
203 | + $maxHeight = (int)$this->config->getSystemValue('preview_max_y', 4096); |
|
204 | + |
|
205 | + $preview = $this->helper->getThumbnail($provider, $file, $maxWidth, $maxHeight); |
|
206 | + |
|
207 | + if (!($preview instanceof IImage)) { |
|
208 | + continue; |
|
209 | + } |
|
210 | + |
|
211 | + // Try to get the extention. |
|
212 | + try { |
|
213 | + $ext = $this->getExtention($preview->dataMimeType()); |
|
214 | + } catch (\InvalidArgumentException $e) { |
|
215 | + // Just continue to the next iteration if this preview doesn't have a valid mimetype |
|
216 | + continue; |
|
217 | + } |
|
218 | + |
|
219 | + $path = $prefix . (string)$preview->width() . '-' . (string)$preview->height() . '-max.' . $ext; |
|
220 | + try { |
|
221 | + $file = $previewFolder->newFile($path); |
|
222 | + $file->putContent($preview->data()); |
|
223 | + } catch (NotPermittedException $e) { |
|
224 | + throw new NotFoundException(); |
|
225 | + } |
|
226 | + |
|
227 | + return $file; |
|
228 | + } |
|
229 | + } |
|
230 | + |
|
231 | + throw new NotFoundException(); |
|
232 | + } |
|
233 | + |
|
234 | + /** |
|
235 | + * @param ISimpleFile $file |
|
236 | + * @param string $prefix |
|
237 | + * @return int[] |
|
238 | + */ |
|
239 | + private function getPreviewSize(ISimpleFile $file, string $prefix = '') { |
|
240 | + $size = explode('-', substr($file->getName(), strlen($prefix))); |
|
241 | + return [(int)$size[0], (int)$size[1]]; |
|
242 | + } |
|
243 | + |
|
244 | + /** |
|
245 | + * @param int $width |
|
246 | + * @param int $height |
|
247 | + * @param bool $crop |
|
248 | + * @param string $mimeType |
|
249 | + * @param string $prefix |
|
250 | + * @return string |
|
251 | + */ |
|
252 | + private function generatePath($width, $height, $crop, $mimeType, $prefix) { |
|
253 | + $path = $prefix . (string)$width . '-' . (string)$height; |
|
254 | + if ($crop) { |
|
255 | + $path .= '-crop'; |
|
256 | + } |
|
257 | + |
|
258 | + $ext = $this->getExtention($mimeType); |
|
259 | + $path .= '.' . $ext; |
|
260 | + return $path; |
|
261 | + } |
|
262 | + |
|
263 | + |
|
264 | + /** |
|
265 | + * @param int $width |
|
266 | + * @param int $height |
|
267 | + * @param bool $crop |
|
268 | + * @param string $mode |
|
269 | + * @param int $maxWidth |
|
270 | + * @param int $maxHeight |
|
271 | + * @return int[] |
|
272 | + */ |
|
273 | + private function calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight) { |
|
274 | + |
|
275 | + /* |
|
276 | 276 | * If we are not cropping we have to make sure the requested image |
277 | 277 | * respects the aspect ratio of the original. |
278 | 278 | */ |
279 | - if (!$crop) { |
|
280 | - $ratio = $maxHeight / $maxWidth; |
|
279 | + if (!$crop) { |
|
280 | + $ratio = $maxHeight / $maxWidth; |
|
281 | 281 | |
282 | - if ($width === -1) { |
|
283 | - $width = $height / $ratio; |
|
284 | - } |
|
285 | - if ($height === -1) { |
|
286 | - $height = $width * $ratio; |
|
287 | - } |
|
282 | + if ($width === -1) { |
|
283 | + $width = $height / $ratio; |
|
284 | + } |
|
285 | + if ($height === -1) { |
|
286 | + $height = $width * $ratio; |
|
287 | + } |
|
288 | 288 | |
289 | - $ratioH = $height / $maxHeight; |
|
290 | - $ratioW = $width / $maxWidth; |
|
289 | + $ratioH = $height / $maxHeight; |
|
290 | + $ratioW = $width / $maxWidth; |
|
291 | 291 | |
292 | - /* |
|
292 | + /* |
|
293 | 293 | * Fill means that the $height and $width are the max |
294 | 294 | * Cover means min. |
295 | 295 | */ |
296 | - if ($mode === IPreview::MODE_FILL) { |
|
297 | - if ($ratioH > $ratioW) { |
|
298 | - $height = $width * $ratio; |
|
299 | - } else { |
|
300 | - $width = $height / $ratio; |
|
301 | - } |
|
302 | - } else if ($mode === IPreview::MODE_COVER) { |
|
303 | - if ($ratioH > $ratioW) { |
|
304 | - $width = $height / $ratio; |
|
305 | - } else { |
|
306 | - $height = $width * $ratio; |
|
307 | - } |
|
308 | - } |
|
309 | - } |
|
310 | - |
|
311 | - if ($height !== $maxHeight && $width !== $maxWidth) { |
|
312 | - /* |
|
296 | + if ($mode === IPreview::MODE_FILL) { |
|
297 | + if ($ratioH > $ratioW) { |
|
298 | + $height = $width * $ratio; |
|
299 | + } else { |
|
300 | + $width = $height / $ratio; |
|
301 | + } |
|
302 | + } else if ($mode === IPreview::MODE_COVER) { |
|
303 | + if ($ratioH > $ratioW) { |
|
304 | + $width = $height / $ratio; |
|
305 | + } else { |
|
306 | + $height = $width * $ratio; |
|
307 | + } |
|
308 | + } |
|
309 | + } |
|
310 | + |
|
311 | + if ($height !== $maxHeight && $width !== $maxWidth) { |
|
312 | + /* |
|
313 | 313 | * Scale to the nearest power of four |
314 | 314 | */ |
315 | - $pow4height = 4 ** ceil(log($height) / log(4)); |
|
316 | - $pow4width = 4 ** ceil(log($width) / log(4)); |
|
317 | - |
|
318 | - // Minimum size is 64 |
|
319 | - $pow4height = max($pow4height, 64); |
|
320 | - $pow4width = max($pow4width, 64); |
|
321 | - |
|
322 | - $ratioH = $height / $pow4height; |
|
323 | - $ratioW = $width / $pow4width; |
|
324 | - |
|
325 | - if ($ratioH < $ratioW) { |
|
326 | - $width = $pow4width; |
|
327 | - $height /= $ratioW; |
|
328 | - } else { |
|
329 | - $height = $pow4height; |
|
330 | - $width /= $ratioH; |
|
331 | - } |
|
332 | - } |
|
333 | - |
|
334 | - /* |
|
315 | + $pow4height = 4 ** ceil(log($height) / log(4)); |
|
316 | + $pow4width = 4 ** ceil(log($width) / log(4)); |
|
317 | + |
|
318 | + // Minimum size is 64 |
|
319 | + $pow4height = max($pow4height, 64); |
|
320 | + $pow4width = max($pow4width, 64); |
|
321 | + |
|
322 | + $ratioH = $height / $pow4height; |
|
323 | + $ratioW = $width / $pow4width; |
|
324 | + |
|
325 | + if ($ratioH < $ratioW) { |
|
326 | + $width = $pow4width; |
|
327 | + $height /= $ratioW; |
|
328 | + } else { |
|
329 | + $height = $pow4height; |
|
330 | + $width /= $ratioH; |
|
331 | + } |
|
332 | + } |
|
333 | + |
|
334 | + /* |
|
335 | 335 | * Make sure the requested height and width fall within the max |
336 | 336 | * of the preview. |
337 | 337 | */ |
338 | - if ($height > $maxHeight) { |
|
339 | - $ratio = $height / $maxHeight; |
|
340 | - $height = $maxHeight; |
|
341 | - $width /= $ratio; |
|
342 | - } |
|
343 | - if ($width > $maxWidth) { |
|
344 | - $ratio = $width / $maxWidth; |
|
345 | - $width = $maxWidth; |
|
346 | - $height /= $ratio; |
|
347 | - } |
|
348 | - |
|
349 | - return [(int)round($width), (int)round($height)]; |
|
350 | - } |
|
351 | - |
|
352 | - /** |
|
353 | - * @param ISimpleFolder $previewFolder |
|
354 | - * @param ISimpleFile $maxPreview |
|
355 | - * @param int $width |
|
356 | - * @param int $height |
|
357 | - * @param bool $crop |
|
358 | - * @param int $maxWidth |
|
359 | - * @param int $maxHeight |
|
360 | - * @param string $prefix |
|
361 | - * @return ISimpleFile |
|
362 | - * @throws NotFoundException |
|
363 | - * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) |
|
364 | - */ |
|
365 | - private function generatePreview(ISimpleFolder $previewFolder, ISimpleFile $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight, $prefix) { |
|
366 | - $preview = $this->helper->getImage($maxPreview); |
|
367 | - |
|
368 | - if (!$preview->valid()) { |
|
369 | - throw new \InvalidArgumentException('Failed to generate preview, failed to load image'); |
|
370 | - } |
|
371 | - |
|
372 | - if ($crop) { |
|
373 | - if ($height !== $preview->height() && $width !== $preview->width()) { |
|
374 | - //Resize |
|
375 | - $widthR = $preview->width() / $width; |
|
376 | - $heightR = $preview->height() / $height; |
|
377 | - |
|
378 | - if ($widthR > $heightR) { |
|
379 | - $scaleH = $height; |
|
380 | - $scaleW = $maxWidth / $heightR; |
|
381 | - } else { |
|
382 | - $scaleH = $maxHeight / $widthR; |
|
383 | - $scaleW = $width; |
|
384 | - } |
|
385 | - $preview->preciseResize((int)round($scaleW), (int)round($scaleH)); |
|
386 | - } |
|
387 | - $cropX = (int)floor(abs($width - $preview->width()) * 0.5); |
|
388 | - $cropY = (int)floor(abs($height - $preview->height()) * 0.5); |
|
389 | - $preview->crop($cropX, $cropY, $width, $height); |
|
390 | - } else { |
|
391 | - $preview->resize(max($width, $height)); |
|
392 | - } |
|
393 | - |
|
394 | - |
|
395 | - $path = $this->generatePath($width, $height, $crop, $preview->dataMimeType(), $prefix); |
|
396 | - try { |
|
397 | - $file = $previewFolder->newFile($path); |
|
398 | - $file->putContent($preview->data()); |
|
399 | - } catch (NotPermittedException $e) { |
|
400 | - throw new NotFoundException(); |
|
401 | - } |
|
402 | - |
|
403 | - return $file; |
|
404 | - } |
|
405 | - |
|
406 | - /** |
|
407 | - * @param ISimpleFolder $previewFolder |
|
408 | - * @param int $width |
|
409 | - * @param int $height |
|
410 | - * @param bool $crop |
|
411 | - * @param string $mimeType |
|
412 | - * @param string $prefix |
|
413 | - * @return ISimpleFile |
|
414 | - * |
|
415 | - * @throws NotFoundException |
|
416 | - */ |
|
417 | - private function getCachedPreview(ISimpleFolder $previewFolder, $width, $height, $crop, $mimeType, $prefix) { |
|
418 | - $path = $this->generatePath($width, $height, $crop, $mimeType, $prefix); |
|
419 | - |
|
420 | - return $previewFolder->getFile($path); |
|
421 | - } |
|
422 | - |
|
423 | - /** |
|
424 | - * Get the specific preview folder for this file |
|
425 | - * |
|
426 | - * @param File $file |
|
427 | - * @return ISimpleFolder |
|
428 | - */ |
|
429 | - private function getPreviewFolder(File $file) { |
|
430 | - try { |
|
431 | - $folder = $this->appData->getFolder($file->getId()); |
|
432 | - } catch (NotFoundException $e) { |
|
433 | - $folder = $this->appData->newFolder($file->getId()); |
|
434 | - } |
|
435 | - |
|
436 | - return $folder; |
|
437 | - } |
|
438 | - |
|
439 | - /** |
|
440 | - * @param string $mimeType |
|
441 | - * @return null|string |
|
442 | - * @throws \InvalidArgumentException |
|
443 | - */ |
|
444 | - private function getExtention($mimeType) { |
|
445 | - switch ($mimeType) { |
|
446 | - case 'image/png': |
|
447 | - return 'png'; |
|
448 | - case 'image/jpeg': |
|
449 | - return 'jpg'; |
|
450 | - case 'image/gif': |
|
451 | - return 'gif'; |
|
452 | - default: |
|
453 | - throw new \InvalidArgumentException('Not a valid mimetype'); |
|
454 | - } |
|
455 | - } |
|
338 | + if ($height > $maxHeight) { |
|
339 | + $ratio = $height / $maxHeight; |
|
340 | + $height = $maxHeight; |
|
341 | + $width /= $ratio; |
|
342 | + } |
|
343 | + if ($width > $maxWidth) { |
|
344 | + $ratio = $width / $maxWidth; |
|
345 | + $width = $maxWidth; |
|
346 | + $height /= $ratio; |
|
347 | + } |
|
348 | + |
|
349 | + return [(int)round($width), (int)round($height)]; |
|
350 | + } |
|
351 | + |
|
352 | + /** |
|
353 | + * @param ISimpleFolder $previewFolder |
|
354 | + * @param ISimpleFile $maxPreview |
|
355 | + * @param int $width |
|
356 | + * @param int $height |
|
357 | + * @param bool $crop |
|
358 | + * @param int $maxWidth |
|
359 | + * @param int $maxHeight |
|
360 | + * @param string $prefix |
|
361 | + * @return ISimpleFile |
|
362 | + * @throws NotFoundException |
|
363 | + * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) |
|
364 | + */ |
|
365 | + private function generatePreview(ISimpleFolder $previewFolder, ISimpleFile $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight, $prefix) { |
|
366 | + $preview = $this->helper->getImage($maxPreview); |
|
367 | + |
|
368 | + if (!$preview->valid()) { |
|
369 | + throw new \InvalidArgumentException('Failed to generate preview, failed to load image'); |
|
370 | + } |
|
371 | + |
|
372 | + if ($crop) { |
|
373 | + if ($height !== $preview->height() && $width !== $preview->width()) { |
|
374 | + //Resize |
|
375 | + $widthR = $preview->width() / $width; |
|
376 | + $heightR = $preview->height() / $height; |
|
377 | + |
|
378 | + if ($widthR > $heightR) { |
|
379 | + $scaleH = $height; |
|
380 | + $scaleW = $maxWidth / $heightR; |
|
381 | + } else { |
|
382 | + $scaleH = $maxHeight / $widthR; |
|
383 | + $scaleW = $width; |
|
384 | + } |
|
385 | + $preview->preciseResize((int)round($scaleW), (int)round($scaleH)); |
|
386 | + } |
|
387 | + $cropX = (int)floor(abs($width - $preview->width()) * 0.5); |
|
388 | + $cropY = (int)floor(abs($height - $preview->height()) * 0.5); |
|
389 | + $preview->crop($cropX, $cropY, $width, $height); |
|
390 | + } else { |
|
391 | + $preview->resize(max($width, $height)); |
|
392 | + } |
|
393 | + |
|
394 | + |
|
395 | + $path = $this->generatePath($width, $height, $crop, $preview->dataMimeType(), $prefix); |
|
396 | + try { |
|
397 | + $file = $previewFolder->newFile($path); |
|
398 | + $file->putContent($preview->data()); |
|
399 | + } catch (NotPermittedException $e) { |
|
400 | + throw new NotFoundException(); |
|
401 | + } |
|
402 | + |
|
403 | + return $file; |
|
404 | + } |
|
405 | + |
|
406 | + /** |
|
407 | + * @param ISimpleFolder $previewFolder |
|
408 | + * @param int $width |
|
409 | + * @param int $height |
|
410 | + * @param bool $crop |
|
411 | + * @param string $mimeType |
|
412 | + * @param string $prefix |
|
413 | + * @return ISimpleFile |
|
414 | + * |
|
415 | + * @throws NotFoundException |
|
416 | + */ |
|
417 | + private function getCachedPreview(ISimpleFolder $previewFolder, $width, $height, $crop, $mimeType, $prefix) { |
|
418 | + $path = $this->generatePath($width, $height, $crop, $mimeType, $prefix); |
|
419 | + |
|
420 | + return $previewFolder->getFile($path); |
|
421 | + } |
|
422 | + |
|
423 | + /** |
|
424 | + * Get the specific preview folder for this file |
|
425 | + * |
|
426 | + * @param File $file |
|
427 | + * @return ISimpleFolder |
|
428 | + */ |
|
429 | + private function getPreviewFolder(File $file) { |
|
430 | + try { |
|
431 | + $folder = $this->appData->getFolder($file->getId()); |
|
432 | + } catch (NotFoundException $e) { |
|
433 | + $folder = $this->appData->newFolder($file->getId()); |
|
434 | + } |
|
435 | + |
|
436 | + return $folder; |
|
437 | + } |
|
438 | + |
|
439 | + /** |
|
440 | + * @param string $mimeType |
|
441 | + * @return null|string |
|
442 | + * @throws \InvalidArgumentException |
|
443 | + */ |
|
444 | + private function getExtention($mimeType) { |
|
445 | + switch ($mimeType) { |
|
446 | + case 'image/png': |
|
447 | + return 'png'; |
|
448 | + case 'image/jpeg': |
|
449 | + return 'jpg'; |
|
450 | + case 'image/gif': |
|
451 | + return 'gif'; |
|
452 | + default: |
|
453 | + throw new \InvalidArgumentException('Not a valid mimetype'); |
|
454 | + } |
|
455 | + } |
|
456 | 456 | } |
@@ -120,7 +120,7 @@ discard block |
||
120 | 120 | |
121 | 121 | $previewVersion = ''; |
122 | 122 | if ($file instanceof IVersionedPreviewFile) { |
123 | - $previewVersion = $file->getPreviewVersion() . '-'; |
|
123 | + $previewVersion = $file->getPreviewVersion().'-'; |
|
124 | 124 | } |
125 | 125 | |
126 | 126 | // Get the max preview and infer the max preview sizes from that |
@@ -199,8 +199,8 @@ discard block |
||
199 | 199 | continue; |
200 | 200 | } |
201 | 201 | |
202 | - $maxWidth = (int)$this->config->getSystemValue('preview_max_x', 4096); |
|
203 | - $maxHeight = (int)$this->config->getSystemValue('preview_max_y', 4096); |
|
202 | + $maxWidth = (int) $this->config->getSystemValue('preview_max_x', 4096); |
|
203 | + $maxHeight = (int) $this->config->getSystemValue('preview_max_y', 4096); |
|
204 | 204 | |
205 | 205 | $preview = $this->helper->getThumbnail($provider, $file, $maxWidth, $maxHeight); |
206 | 206 | |
@@ -216,7 +216,7 @@ discard block |
||
216 | 216 | continue; |
217 | 217 | } |
218 | 218 | |
219 | - $path = $prefix . (string)$preview->width() . '-' . (string)$preview->height() . '-max.' . $ext; |
|
219 | + $path = $prefix.(string) $preview->width().'-'.(string) $preview->height().'-max.'.$ext; |
|
220 | 220 | try { |
221 | 221 | $file = $previewFolder->newFile($path); |
222 | 222 | $file->putContent($preview->data()); |
@@ -238,7 +238,7 @@ discard block |
||
238 | 238 | */ |
239 | 239 | private function getPreviewSize(ISimpleFile $file, string $prefix = '') { |
240 | 240 | $size = explode('-', substr($file->getName(), strlen($prefix))); |
241 | - return [(int)$size[0], (int)$size[1]]; |
|
241 | + return [(int) $size[0], (int) $size[1]]; |
|
242 | 242 | } |
243 | 243 | |
244 | 244 | /** |
@@ -250,13 +250,13 @@ discard block |
||
250 | 250 | * @return string |
251 | 251 | */ |
252 | 252 | private function generatePath($width, $height, $crop, $mimeType, $prefix) { |
253 | - $path = $prefix . (string)$width . '-' . (string)$height; |
|
253 | + $path = $prefix.(string) $width.'-'.(string) $height; |
|
254 | 254 | if ($crop) { |
255 | 255 | $path .= '-crop'; |
256 | 256 | } |
257 | 257 | |
258 | 258 | $ext = $this->getExtention($mimeType); |
259 | - $path .= '.' . $ext; |
|
259 | + $path .= '.'.$ext; |
|
260 | 260 | return $path; |
261 | 261 | } |
262 | 262 | |
@@ -346,7 +346,7 @@ discard block |
||
346 | 346 | $height /= $ratio; |
347 | 347 | } |
348 | 348 | |
349 | - return [(int)round($width), (int)round($height)]; |
|
349 | + return [(int) round($width), (int) round($height)]; |
|
350 | 350 | } |
351 | 351 | |
352 | 352 | /** |
@@ -382,10 +382,10 @@ discard block |
||
382 | 382 | $scaleH = $maxHeight / $widthR; |
383 | 383 | $scaleW = $width; |
384 | 384 | } |
385 | - $preview->preciseResize((int)round($scaleW), (int)round($scaleH)); |
|
385 | + $preview->preciseResize((int) round($scaleW), (int) round($scaleH)); |
|
386 | 386 | } |
387 | - $cropX = (int)floor(abs($width - $preview->width()) * 0.5); |
|
388 | - $cropY = (int)floor(abs($height - $preview->height()) * 0.5); |
|
387 | + $cropX = (int) floor(abs($width - $preview->width()) * 0.5); |
|
388 | + $cropY = (int) floor(abs($height - $preview->height()) * 0.5); |
|
389 | 389 | $preview->crop($cropX, $cropY, $width, $height); |
390 | 390 | } else { |
391 | 391 | $preview->resize(max($width, $height)); |