Add ImageAdapter::checkMemoryLimit()
authorTim Düsterhus <duesterhus@woltlab.com>
Tue, 4 Aug 2020 08:26:34 +0000 (10:26 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Tue, 4 Aug 2020 08:36:06 +0000 (10:36 +0200)
Resolves #3229

wcfsetup/install/files/lib/system/image/adapter/IMemoryAwareImageAdapter.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/image/adapter/ImageAdapter.class.php

diff --git a/wcfsetup/install/files/lib/system/image/adapter/IMemoryAwareImageAdapter.class.php b/wcfsetup/install/files/lib/system/image/adapter/IMemoryAwareImageAdapter.class.php
new file mode 100644 (file)
index 0000000..f68388a
--- /dev/null
@@ -0,0 +1,25 @@
+<?php
+namespace wcf\system\image\adapter;
+
+/**
+ * A memory aware image adapter is able to determine whether it is
+ * likely able to process an image within the process' memory limit.
+ * 
+ * @author     Tim Duesterhus
+ * @copyright  2001-2020 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    WoltLabSuite\Core\System\Image\Adapter
+ * @since      5.3
+ */
+interface IMemoryAwareImageAdapter extends IImageAdapter {
+       /**
+        * Returns whether it is believed that sufficient memory
+        * is available to process an image with the given properties.
+        * 
+        * @param       integer         $width
+        * @param       integer         $height
+        * @param       string          $mimeType
+        * @return      boolean
+        */
+       public function checkMemoryLimit($width, $height, $mimeType);
+}
index 627062443f47bcd0d1dae7995d16666e31424dcf..96c3a475d635dcea5926ae5f86c5b700b65452db 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 namespace wcf\system\image\adapter;
 use wcf\system\exception\SystemException;
+use wcf\util\FileUtil;
 
 /**
  * Wrapper for image adapters.
@@ -10,7 +11,7 @@ use wcf\system\exception\SystemException;
  * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  * @package    WoltLabSuite\Core\System\Image\Adapter
  */
-class ImageAdapter implements IImageAdapter {
+class ImageAdapter implements IImageAdapter, IMemoryAwareImageAdapter {
        /**
         * IImageAdapter object
         * @var IImageAdapter
@@ -352,6 +353,19 @@ class ImageAdapter implements IImageAdapter {
                $this->overlayImage($file, $x, $y, $opacity);
        }
        
+       /**
+        * @inheritDoc
+        */
+       public function checkMemoryLimit($width, $height, $mimeType) {
+               if ($this->adapter instanceof IMemoryAwareImageAdapter) {
+                       return $this->adapter->checkMemoryLimit($width, $height, $mimeType);
+               }
+               
+               $channels = $mimeType == 'image/png' ? 4 : 3;
+               
+               return FileUtil::checkMemoryLimit($width * $height * $channels * 2.1);
+       }
+       
        /**
         * @inheritDoc
         */