aboutsummaryrefslogtreecommitdiffstats
path: root/apps/theming
diff options
context:
space:
mode:
authorFerdinand Thiessen <rpm@fthiessen.de>2023-02-20 13:10:49 +0100
committerFerdinand Thiessen <rpm@fthiessen.de>2023-02-20 14:57:50 +0100
commit2ca1153ff6667d7d7d98189aec95f246854ffcfa (patch)
treea9d1f3bb1d9f85a554a3d8452cf5841b5e447942 /apps/theming
parent61b9ba28e4a9cbaf22f34130dd14ad9cb41ab6e8 (diff)
downloadnextcloud-server-2ca1153ff6667d7d7d98189aec95f246854ffcfa.tar.gz
nextcloud-server-2ca1153ff6667d7d7d98189aec95f246854ffcfa.zip
fix(themeing): Add error handling to ImageManager
Signed-off-by: Ferdinand Thiessen <rpm@fthiessen.de>
Diffstat (limited to 'apps/theming')
-rw-r--r--apps/theming/lib/ImageManager.php64
1 files changed, 41 insertions, 23 deletions
diff --git a/apps/theming/lib/ImageManager.php b/apps/theming/lib/ImageManager.php
index fe9c3332f7e..d088699fd3c 100644
--- a/apps/theming/lib/ImageManager.php
+++ b/apps/theming/lib/ImageManager.php
@@ -244,32 +244,50 @@ class ImageManager {
}
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));
-
- // Preserve transparency
- imagesavealpha($newImage, true);
- imagealphablending($newImage, true);
-
- $tmpFile = $this->tempManager->getTemporaryFile();
- $newWidth = (int)(imagesx($newImage) < 4096 ? imagesx($newImage) : 4096);
- $newHeight = (int)(imagesy($newImage) / (imagesx($newImage) / $newWidth));
- $outputImage = imagescale($newImage, $newWidth, $newHeight);
-
- imageinterlace($outputImage, 1);
- if (strpos($detectedMimeType, 'image/jpeg') !== false) {
- imagejpeg($outputImage, $tmpFile, 90);
- } else {
- imagepng($outputImage, $tmpFile, 8);
- }
- imagedestroy($outputImage);
+ try {
+ // 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));
+ if ($newImage === false) {
+ throw new \Exception('Could not read background image, possibly corrupted.');
+ }
- $target->putContent(file_get_contents($tmpFile));
- } else {
- $target->putContent(file_get_contents($tmpFile));
+ // Preserve transparency
+ imagesavealpha($newImage, true);
+ imagealphablending($newImage, true);
+
+ $newWidth = (int)(imagesx($newImage) < 4096 ? imagesx($newImage) : 4096);
+ $newHeight = (int)(imagesy($newImage) / (imagesx($newImage) / $newWidth));
+ $outputImage = imagescale($newImage, $newWidth, $newHeight);
+ if ($outputImage === false) {
+ throw new \Exception('Could not scale uploaded background image.');
+ }
+
+ $newTmpFile = $this->tempManager->getTemporaryFile();
+ imageinterlace($outputImage, 1);
+ // Keep jpeg images encoded as jpeg
+ if (strpos($detectedMimeType, 'image/jpeg') !== false) {
+ if (!imagejpeg($outputImage, $newTmpFile, 90)) {
+ throw new \Exception('Could not recompress background image as JPEG');
+ }
+ } else {
+ if (!imagepng($outputImage, $newTmpFile, 8)) {
+ throw new \Exception('Could not recompress background image as PNG');
+ }
+ }
+ $tmpFile = $newTmpFile;
+ imagedestroy($outputImage);
+ } catch (\Exception $e) {
+ if (is_resource($outputImage) || $outputImage instanceof \GdImage) {
+ imagedestroy($outputImage);
+ }
+
+ $this->logger->debug($e->getMessage());
+ }
}
+ $target->putContent(file_get_contents($tmpFile));
+
return $detectedMimeType;
}