diff --git a/code/Helper/ImageThumbnailHelper.php b/code/Helper/ImageThumbnailHelper.php index 0242d79d2..c1b4de3d6 100644 --- a/code/Helper/ImageThumbnailHelper.php +++ b/code/Helper/ImageThumbnailHelper.php @@ -3,28 +3,61 @@ use SilverStripe\AssetAdmin\Controller\AssetAdmin; use SilverStripe\Assets\File; -use SilverStripe\Assets\ImageBackendFactory; +use SilverStripe\Core\Convert; use SilverStripe\Core\Injector\Injectable; -use SilverStripe\Core\Injector\InjectionCreator; -use SilverStripe\Core\Injector\Injector; +use SilverStripe\ORM\DB; class ImageThumbnailHelper { use Injectable; + /** + * @var int + */ + private $maxImageFileSize; + + /** + * @param mixed $maxImageSize Maximum file size for which thumbnails will be generated. Set to `0` to disable the + * limit. + */ + public function __construct($maxImageFileSize = '9M') + { + $this->setMaxImageFileSize($maxImageFileSize); + } + + /** + * Get the maximum file size for which thumbnails will be generated. Set to `0` to disable the limit. + * @return int + */ + public function getMaxImageFileSize() + { + return $this->maxImageFileSize; + } + + /** + * Set the maximum file size for which thumbnails will be generated. Set to `0` to disable the limit. + * @param mixed $size + * @return $this + */ + public function setMaxImageFileSize($size) + { + $this->maxImageFileSize = Convert::memstring2bytes($size); + return $this; + } + public function run() { $assetAdmin = AssetAdmin::singleton(); - $creator = new InjectionCreator(); - Injector::inst()->registerService( - $creator, - ImageBackendFactory::class - ); + /** @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); + } } } }