Skip to content

Commit

Permalink
Merge pull request #905 from open-sausages/pulls/1/optimise-image-bac…
Browse files Browse the repository at this point in the history
…kend-to-reduce-memory-usage

NEW Remove ImageBackendFactory Injector override from ImageThumbnailHelper
  • Loading branch information
bergice authored Jan 29, 2019
2 parents 64feb50 + ca5e80b commit f29848b
Showing 1 changed file with 42 additions and 9 deletions.
51 changes: 42 additions & 9 deletions code/Helper/ImageThumbnailHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}

0 comments on commit f29848b

Please sign in to comment.