|
27 | 27 | * @author Samuel CHEMLA <chemla.samuel@gmail.com> |
28 | 28 | * @author Thomas Müller <thomas.mueller@tmit.eu> |
29 | 29 | * @author Thomas Tanghus <thomas@tanghus.net> |
| 30 | + * @author Richard Steinmetz <richard@steinmetz.cloud> |
30 | 31 | * |
31 | 32 | * @license AGPL-3.0 |
32 | 33 | * |
@@ -55,6 +56,9 @@ class OC_Image implements \OCP\IImage { |
55 | 56 | // Default quality for jpeg images |
56 | 57 | protected const DEFAULT_JPEG_QUALITY = 80; |
57 | 58 |
|
| 59 | + // Default quality for webp images |
| 60 | + protected const DEFAULT_WEBP_QUALITY = 80; |
| 61 | + |
58 | 62 | /** @var false|resource|\GdImage */ |
59 | 63 | protected $resource = false; // tmp resource. |
60 | 64 | /** @var int */ |
@@ -283,6 +287,9 @@ private function _output(?string $filePath = null, ?string $mimeType = null): bo |
283 | 287 | case 'image/x-ms-bmp': |
284 | 288 | $imageType = IMAGETYPE_BMP; |
285 | 289 | break; |
| 290 | + case 'image/webp': |
| 291 | + $imageType = IMAGETYPE_WEBP; |
| 292 | + break; |
286 | 293 | default: |
287 | 294 | throw new Exception('\OC_Image::_output(): "' . $mimeType . '" is not supported when forcing a specific output format'); |
288 | 295 | } |
@@ -314,6 +321,9 @@ private function _output(?string $filePath = null, ?string $mimeType = null): bo |
314 | 321 | case IMAGETYPE_BMP: |
315 | 322 | $retVal = imagebmp($this->resource, $filePath); |
316 | 323 | break; |
| 324 | + case IMAGETYPE_WEBP: |
| 325 | + $retVal = imagewebp($this->resource, null, $this->getWebpQuality()); |
| 326 | + break; |
317 | 327 | default: |
318 | 328 | $retVal = imagepng($this->resource, $filePath); |
319 | 329 | } |
@@ -364,6 +374,7 @@ public function dataMimeType(): ?string { |
364 | 374 | case 'image/png': |
365 | 375 | case 'image/jpeg': |
366 | 376 | case 'image/gif': |
| 377 | + case 'image/webp': |
367 | 378 | return $this->mimeType; |
368 | 379 | default: |
369 | 380 | return 'image/png'; |
@@ -391,6 +402,9 @@ public function data(): ?string { |
391 | 402 | case "image/gif": |
392 | 403 | $res = imagegif($this->resource); |
393 | 404 | break; |
| 405 | + case "image/webp": |
| 406 | + $res = imagewebp($this->resource, null, $this->getWebpQuality()); |
| 407 | + break; |
394 | 408 | default: |
395 | 409 | $res = imagepng($this->resource); |
396 | 410 | $this->logger->info('OC_Image->data. Could not guess mime-type, defaulting to png', ['app' => 'core']); |
@@ -421,6 +435,18 @@ protected function getJpegQuality(): int { |
421 | 435 | return min(100, max(10, (int) $quality)); |
422 | 436 | } |
423 | 437 |
|
| 438 | + /** |
| 439 | + * @return int |
| 440 | + */ |
| 441 | + protected function getWebpQuality(): int { |
| 442 | + $quality = $this->config->getAppValue('preview', 'webp_quality', (string) self::DEFAULT_WEBP_QUALITY); |
| 443 | + // TODO: remove when getAppValue is type safe |
| 444 | + if ($quality === null) { |
| 445 | + $quality = self::DEFAULT_WEBP_QUALITY; |
| 446 | + } |
| 447 | + return min(100, max(10, (int) $quality)); |
| 448 | + } |
| 449 | + |
424 | 450 | /** |
425 | 451 | * (I'm open for suggestions on better method name ;) |
426 | 452 | * Get the orientation based on EXIF data. |
|
0 commit comments