From 89c020cc672080b22677e7718e002d39c02de327 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Thu, 1 Aug 2013 21:32:48 +0200 Subject: [PATCH] Added memory_limit check for thumbnail generation --- .../attachment/AttachmentAction.class.php | 10 +++- .../install/files/lib/util/FileUtil.class.php | 48 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/wcfsetup/install/files/lib/data/attachment/AttachmentAction.class.php b/wcfsetup/install/files/lib/data/attachment/AttachmentAction.class.php index fe8af684a0..991dd976f6 100644 --- a/wcfsetup/install/files/lib/data/attachment/AttachmentAction.class.php +++ b/wcfsetup/install/files/lib/data/attachment/AttachmentAction.class.php @@ -231,7 +231,7 @@ class AttachmentAction extends AbstractDatabaseObjectAction { * Generates thumbnails. */ public function generateThumbnails() { - if (!empty($this->objects)) { + if (empty($this->objects)) { $this->readObjects(); } @@ -254,7 +254,15 @@ class AttachmentAction extends AbstractDatabaseObjectAction { continue; // image smaller than thumbnail size; skip } + $adapter = ImageHandler::getInstance()->getAdapter(); + + // check memory limit + $neededMemory = $attachment->width * $attachment->height * ($attachment->imageType == 'image/png' ? 4 : 3) * 2.1; + if (FileUtil::getMemoryLimit() != -1 && FileUtil::getMemoryLimit() < (memory_get_usage() + $neededMemory)) { + continue; + } + $adapter->loadFile($attachment->getLocation()); $updateData = array(); diff --git a/wcfsetup/install/files/lib/util/FileUtil.class.php b/wcfsetup/install/files/lib/util/FileUtil.class.php index 0f8e2e9e0a..cff3221ce2 100644 --- a/wcfsetup/install/files/lib/util/FileUtil.class.php +++ b/wcfsetup/install/files/lib/util/FileUtil.class.php @@ -21,6 +21,12 @@ final class FileUtil { */ protected static $finfo = null; + /** + * memory limit in bytes + * @var integer + */ + protected static $memoryLimit = null; + /** * chmod mode * @var string @@ -561,5 +567,47 @@ final class FileUtil { } } + /** + * Returns memory limit in bytes. + * + * @return integer + */ + public static function getMemoryLimit() { + if (self::$memoryLimit === null) { + self::$memoryLimit = 0; + + $memoryLimit = ini_get('memory_limit'); + + // no limit + if ($memoryLimit == -1) { + self::$memoryLimit = -1; + } + + // completely numeric, PHP assumes byte + if (is_numeric($memoryLimit)) { + self::$memoryLimit = $memoryLimit; + } + + // PHP supports 'K', 'M' and 'G' shorthand notation + if (preg_match('~^(\d+)([KMG])$~', $memoryLimit, $matches)) { + switch ($matches[2]) { + case 'K': + self::$memoryLimit = $matches[1] * 1024; + break; + + case 'M': + self::$memoryLimit = $matches[1] * 1024 * 1024; + break; + + case 'G': + self::$memoryLimit = $matches[1] * 1024 * 1024 * 1024; + break; + } + } + } + + return self::$memoryLimit; + } + private function __construct() { } } -- 2.20.1