diff options
author | Richard Steinmetz <richard@steinmetz.cloud> | 2024-03-26 09:58:36 +0100 |
---|---|---|
committer | Richard Steinmetz <richard@steinmetz.cloud> | 2024-03-26 09:58:36 +0100 |
commit | e9c0d0e67fcda5fc12f24d7c415eec3c9cdcaaad (patch) | |
tree | 8f07b82e80b0bed799c1b65f20f0a1dc4fed18be /lib | |
parent | 47ac9071492b2f1f1055bbc4ddcfb65aa28d8e1e (diff) | |
download | nextcloud-server-e9c0d0e67fcda5fc12f24d7c415eec3c9cdcaaad.tar.gz nextcloud-server-e9c0d0e67fcda5fc12f24d7c415eec3c9cdcaaad.zip |
fix(preview): webp preview format
Signed-off-by: Richard Steinmetz <richard@steinmetz.cloud>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/legacy/OC_Image.php | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/lib/private/legacy/OC_Image.php b/lib/private/legacy/OC_Image.php index aac0c496734..56fb3e0d52f 100644 --- a/lib/private/legacy/OC_Image.php +++ b/lib/private/legacy/OC_Image.php @@ -27,6 +27,7 @@ declare(strict_types=1); * @author Samuel CHEMLA <chemla.samuel@gmail.com> * @author Thomas Müller <thomas.mueller@tmit.eu> * @author Thomas Tanghus <thomas@tanghus.net> + * @author Richard Steinmetz <richard@steinmetz.cloud> * * @license AGPL-3.0 * @@ -55,6 +56,9 @@ class OC_Image implements \OCP\IImage { // Default quality for jpeg images protected const DEFAULT_JPEG_QUALITY = 80; + // Default quality for webp images + protected const DEFAULT_WEBP_QUALITY = 80; + /** @var false|resource|\GdImage */ protected $resource = false; // tmp resource. /** @var int */ @@ -283,6 +287,9 @@ class OC_Image implements \OCP\IImage { case 'image/x-ms-bmp': $imageType = IMAGETYPE_BMP; break; + case 'image/webp': + $imageType = IMAGETYPE_WEBP; + break; default: throw new Exception('\OC_Image::_output(): "' . $mimeType . '" is not supported when forcing a specific output format'); } @@ -314,6 +321,9 @@ class OC_Image implements \OCP\IImage { case IMAGETYPE_BMP: $retVal = imagebmp($this->resource, $filePath); break; + case IMAGETYPE_WEBP: + $retVal = imagewebp($this->resource, null, $this->getWebpQuality()); + break; default: $retVal = imagepng($this->resource, $filePath); } @@ -364,6 +374,7 @@ class OC_Image implements \OCP\IImage { case 'image/png': case 'image/jpeg': case 'image/gif': + case 'image/webp': return $this->mimeType; default: return 'image/png'; @@ -391,6 +402,9 @@ class OC_Image implements \OCP\IImage { case "image/gif": $res = imagegif($this->resource); break; + case "image/webp": + $res = imagewebp($this->resource, null, $this->getWebpQuality()); + break; default: $res = imagepng($this->resource); $this->logger->info('OC_Image->data. Could not guess mime-type, defaulting to png', ['app' => 'core']); @@ -422,6 +436,18 @@ class OC_Image implements \OCP\IImage { } /** + * @return int + */ + protected function getWebpQuality(): int { + $quality = $this->config->getAppValue('preview', 'webp_quality', (string) self::DEFAULT_WEBP_QUALITY); + // TODO: remove when getAppValue is type safe + if ($quality === null) { + $quality = self::DEFAULT_WEBP_QUALITY; + } + return min(100, max(10, (int) $quality)); + } + + /** * (I'm open for suggestions on better method name ;) * Get the orientation based on EXIF data. * |