From 13b847501f117a6286cf079550f7c0031c4edf14 Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Wed, 23 Jan 2019 18:55:58 +1300 Subject: [PATCH] NEW add a memory limit to the ImageThumbnailHelper --- code/Helper/ImageThumbnailHelper.php | 40 +++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/code/Helper/ImageThumbnailHelper.php b/code/Helper/ImageThumbnailHelper.php index e9c22ff32..eda8f0018 100644 --- a/code/Helper/ImageThumbnailHelper.php +++ b/code/Helper/ImageThumbnailHelper.php @@ -3,20 +3,58 @@ use SilverStripe\AssetAdmin\Controller\AssetAdmin; use SilverStripe\Assets\File; +use SilverStripe\Core\Convert; use SilverStripe\Core\Injector\Injectable; +use SilverStripe\ORM\DB; class ImageThumbnailHelper { use Injectable; + /** + * @var int + */ + private $maxImageFileSize; + + /** + * @param mixed $maxImageSize Maximum file size for which thumbnails will be generated + */ + public function __construct($maxImageFileSize = '9M') + { + $this->setMaxImageFileSize($maxImageFileSize); + } + + /** + * @return int + */ + public function getMaxImageFileSize() + { + return $this->maxImageFileSize; + } + + /** + * @param mixed $size + * @return $this + */ + public function setMaxImageFileSize($size) + { + $this->maxImageFileSize = Convert::memstring2bytes($size); + return $this; + } + public function run() { $assetAdmin = AssetAdmin::singleton(); + /** @var File[] $files */ $files = File::get(); set_time_limit(0); + + $maxSize = $this->getMaxImageFileSize(); foreach ($files as $file) { - $assetAdmin->generateThumbnails($file, true); + if ($maxSize == 0 || $file->getAbsoluteSize() < $maxSize) { + $assetAdmin->generateThumbnails($file, true); + } } } }