Skip to content

Commit 4177e8a

Browse files
authored
Merge pull request #44474 from nextcloud/fix/preview/webp-preview-format
fix(preview): webp preview format
2 parents 4b7b9c3 + e9c0d0e commit 4177e8a

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

lib/private/legacy/OC_Image.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* @author Samuel CHEMLA <chemla.samuel@gmail.com>
2828
* @author Thomas Müller <thomas.mueller@tmit.eu>
2929
* @author Thomas Tanghus <thomas@tanghus.net>
30+
* @author Richard Steinmetz <richard@steinmetz.cloud>
3031
*
3132
* @license AGPL-3.0
3233
*
@@ -55,6 +56,9 @@ class OC_Image implements \OCP\IImage {
5556
// Default quality for jpeg images
5657
protected const DEFAULT_JPEG_QUALITY = 80;
5758

59+
// Default quality for webp images
60+
protected const DEFAULT_WEBP_QUALITY = 80;
61+
5862
/** @var false|resource|\GdImage */
5963
protected $resource = false; // tmp resource.
6064
/** @var int */
@@ -283,6 +287,9 @@ private function _output(?string $filePath = null, ?string $mimeType = null): bo
283287
case 'image/x-ms-bmp':
284288
$imageType = IMAGETYPE_BMP;
285289
break;
290+
case 'image/webp':
291+
$imageType = IMAGETYPE_WEBP;
292+
break;
286293
default:
287294
throw new Exception('\OC_Image::_output(): "' . $mimeType . '" is not supported when forcing a specific output format');
288295
}
@@ -314,6 +321,9 @@ private function _output(?string $filePath = null, ?string $mimeType = null): bo
314321
case IMAGETYPE_BMP:
315322
$retVal = imagebmp($this->resource, $filePath);
316323
break;
324+
case IMAGETYPE_WEBP:
325+
$retVal = imagewebp($this->resource, null, $this->getWebpQuality());
326+
break;
317327
default:
318328
$retVal = imagepng($this->resource, $filePath);
319329
}
@@ -364,6 +374,7 @@ public function dataMimeType(): ?string {
364374
case 'image/png':
365375
case 'image/jpeg':
366376
case 'image/gif':
377+
case 'image/webp':
367378
return $this->mimeType;
368379
default:
369380
return 'image/png';
@@ -391,6 +402,9 @@ public function data(): ?string {
391402
case "image/gif":
392403
$res = imagegif($this->resource);
393404
break;
405+
case "image/webp":
406+
$res = imagewebp($this->resource, null, $this->getWebpQuality());
407+
break;
394408
default:
395409
$res = imagepng($this->resource);
396410
$this->logger->info('OC_Image->data. Could not guess mime-type, defaulting to png', ['app' => 'core']);
@@ -421,6 +435,18 @@ protected function getJpegQuality(): int {
421435
return min(100, max(10, (int) $quality));
422436
}
423437

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+
424450
/**
425451
* (I'm open for suggestions on better method name ;)
426452
* Get the orientation based on EXIF data.

0 commit comments

Comments
 (0)