aboutsummaryrefslogtreecommitdiffstats
path: root/apps/theming/lib
diff options
context:
space:
mode:
authorFerdinand Thiessen <rpm@fthiessen.de>2023-02-01 13:23:35 +0100
committerFerdinand Thiessen <rpm@fthiessen.de>2023-02-20 13:11:19 +0100
commit7e1152603f1a083566241527ef2a615104c91395 (patch)
tree323e16faadc62280cbf8672d372ddc486acfa15e /apps/theming/lib
parent95eeba83b6570b3cf7cbb79aea42222f7d3e03f4 (diff)
downloadnextcloud-server-7e1152603f1a083566241527ef2a615104c91395.tar.gz
nextcloud-server-7e1152603f1a083566241527ef2a615104c91395.zip
feat(theming): Only convert a background image if it benefits from it
* WebP images are generally quite small, converting to pngs would increase the filesize a lot. * Small JPEG and PNG images do not benefit from any conversion, so skip it. * JPEG images will get quite bigger when converted to PNG so instead convert to progressive JPEG Signed-off-by: Ferdinand Thiessen <rpm@fthiessen.de>
Diffstat (limited to 'apps/theming/lib')
-rw-r--r--apps/theming/lib/ImageManager.php33
1 files changed, 31 insertions, 2 deletions
diff --git a/apps/theming/lib/ImageManager.php b/apps/theming/lib/ImageManager.php
index f7b0c12844a..fe9c3332f7e 100644
--- a/apps/theming/lib/ImageManager.php
+++ b/apps/theming/lib/ImageManager.php
@@ -243,7 +243,7 @@ class ImageManager {
throw new \Exception('Unsupported image type');
}
- if ($key === 'background' && strpos($detectedMimeType, 'image/svg') === false && strpos($detectedMimeType, 'image/gif') === false) {
+ if ($key === 'background' && $this->shouldOptimizeBackgroundImage($detectedMimeType, filesize($tmpFile))) {
// Optimize the image since some people may upload images that will be
// either to big or are not progressive rendering.
$newImage = @imagecreatefromstring(file_get_contents($tmpFile));
@@ -258,7 +258,11 @@ class ImageManager {
$outputImage = imagescale($newImage, $newWidth, $newHeight);
imageinterlace($outputImage, 1);
- imagepng($outputImage, $tmpFile, 8);
+ if (strpos($detectedMimeType, 'image/jpeg') !== false) {
+ imagejpeg($outputImage, $tmpFile, 90);
+ } else {
+ imagepng($outputImage, $tmpFile, 8);
+ }
imagedestroy($outputImage);
$target->putContent(file_get_contents($tmpFile));
@@ -270,6 +274,31 @@ class ImageManager {
}
/**
+ * Decide whether an image benefits from shrinking and reconverting
+ *
+ * @param string $mimeType the mime type of the image
+ * @param int $contentSize size of the image file
+ * @return bool
+ */
+ private function shouldOptimizeBackgroundImage(string $mimeType, int $contentSize): bool {
+ // Do not touch SVGs
+ if (strpos($mimeType, 'image/svg') !== false) {
+ return false;
+ }
+ // GIF does not benefit from converting
+ if (strpos($mimeType, 'image/gif') !== false) {
+ return false;
+ }
+ // WebP also does not benefit from converting
+ // We could possibly try to convert to progressive image, but normally webP images are quite small
+ if (strpos($mimeType, 'image/webp') !== false) {
+ return false;
+ }
+ // As a rule of thumb background images should be max. 150-300 KiB, small images do not benefit from converting
+ return $contentSize > 150000;
+ }
+
+ /**
* Returns a list of supported mime types for image uploads.
* "favicon" images are only allowed to be SVG when imagemagick with SVG support is available.
*