From bfaccef53d03b61344de60ef7ab99c46e80ffbb7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Tue, 4 Aug 2020 10:26:34 +0200 Subject: [PATCH] Add ImageAdapter::checkMemoryLimit() Resolves #3229 --- .../IMemoryAwareImageAdapter.class.php | 25 +++++++++++++++++++ .../image/adapter/ImageAdapter.class.php | 16 +++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 wcfsetup/install/files/lib/system/image/adapter/IMemoryAwareImageAdapter.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 index 0000000000..f68388ad0b --- /dev/null +++ b/wcfsetup/install/files/lib/system/image/adapter/IMemoryAwareImageAdapter.class.php @@ -0,0 +1,25 @@ + + * @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); +} diff --git a/wcfsetup/install/files/lib/system/image/adapter/ImageAdapter.class.php b/wcfsetup/install/files/lib/system/image/adapter/ImageAdapter.class.php index 627062443f..96c3a475d6 100644 --- a/wcfsetup/install/files/lib/system/image/adapter/ImageAdapter.class.php +++ b/wcfsetup/install/files/lib/system/image/adapter/ImageAdapter.class.php @@ -1,6 +1,7 @@ * @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 */ -- 2.20.1