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