Gracefully ignore Thumbnail generation failures in DefaultUploadFileSaveStrategy
authorTim Düsterhus <duesterhus@woltlab.com>
Fri, 10 Jun 2022 10:14:45 +0000 (12:14 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Fri, 10 Jun 2022 10:16:51 +0000 (12:16 +0200)
`->generateThumbnails()` might already not do anything of the memory limit is
likely going to be exceeded, causing image upload to be thumbnail-less. Extend
this to all cases of thumbnail generation failure by catching exceptions thrown
when loading the image or when performing the resizing operation.

wcfsetup/install/files/lib/system/upload/DefaultUploadFileSaveStrategy.class.php

index 39d3e8c66f3d9fad35f4a52e1d087ee6e45d936c..9b2c9a2e039a5d1f396fc147e98e70efa281c10e 100644 (file)
@@ -310,7 +310,13 @@ class DefaultUploadFileSaveStrategy implements IUploadFileSaveStrategy
             return;
         }
 
-        $adapter->loadFile($file->getLocation());
+        try {
+            $adapter->loadFile($file->getLocation());
+        } catch (\Exception $e) {
+            \wcf\functions\exception\logThrowable($e);
+
+            return;
+        }
 
         $updateData = [];
         foreach ($this->options['thumbnailSizes'] as $type => $sizeData) {
@@ -331,11 +337,18 @@ class DefaultUploadFileSaveStrategy implements IUploadFileSaveStrategy
             }
 
             if ($file->width > $sizeData['width'] || $file->height > $sizeData['height']) {
-                $thumbnail = $adapter->createThumbnail(
-                    $sizeData['width'],
-                    $sizeData['height'],
-                    $sizeData['retainDimensions'] ?? true
-                );
+                try {
+                    $thumbnail = $adapter->createThumbnail(
+                        $sizeData['width'],
+                        $sizeData['height'],
+                        $sizeData['retainDimensions'] ?? true
+                    );
+                } catch (\Exception $e) {
+                    \wcf\functions\exception\logThrowable($e);
+
+                    continue;
+                }
+
                 $adapter->writeImage($thumbnail, $thumbnailLocation);
                 // Clear thumbnail as soon as possible to free up the memory for the next size.
                 $thumbnail = null;