Skip to content

Commit

Permalink
NEW add a memory limit to the ImageThumbnailHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime Rainville committed Jan 29, 2019
1 parent 99ff45a commit 13b8475
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion code/Helper/ImageThumbnailHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}

0 comments on commit 13b8475

Please sign in to comment.