Added memory_limit check for thumbnail generation
authorAlexander Ebert <ebert@woltlab.com>
Thu, 1 Aug 2013 19:32:48 +0000 (21:32 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Thu, 1 Aug 2013 19:32:48 +0000 (21:32 +0200)
wcfsetup/install/files/lib/data/attachment/AttachmentAction.class.php
wcfsetup/install/files/lib/util/FileUtil.class.php

index fe8af684a039310ac26d9714a209366cbd13ab8f..991dd976f65d1bbe9ab7f4904d7913de2f86fb96 100644 (file)
@@ -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();
                        
index 0f8e2e9e0abc756e02093c05da4cae241885f9bc..cff3221ce27a59b96a0fdddac64af6ba4ad7d1b4 100644 (file)
@@ -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() { }
 }