silverstripe-framework/tasks/CleanImageManipulationCache.php

60 lines
1.8 KiB
PHP
Raw Normal View History

<?php
/**
* Wipe the cache of failed image manipulations. When {@link GDBackend} attempts to resample an image, it will write
* the attempted manipulation to the cache and remove it from the cache if the resample is successful. The objective
* of the cache is to prevent fatal errors (for example from exceeded memory limits) from occurring more than once.
*
* @package framework
* @subpackage filesystem
*/
class CleanImageManipulationCache extends BuildTask {
2014-08-15 08:53:05 +02:00
protected $title = 'Clean Image Manipulation Cache';
2014-08-15 08:53:05 +02:00
protected $description = 'Clean the failed image manipulation cache. Use this to allow SilverStripe to attempt
to resample images that have previously failed to resample (for example if memory limits were exceeded).';
2014-08-15 08:53:05 +02:00
/**
* Check that the user has appropriate permissions to execute this task
*/
public function init() {
if(!Director::is_cli() && !Director::isDev() && !Permission::check('ADMIN')) {
return Security::permissionFailure();
}
2014-08-15 08:53:05 +02:00
parent::init();
}
2014-08-15 08:53:05 +02:00
/**
* Clear out the image manipulation cache
* @param SS_HTTPRequest $request
*/
public function run($request) {
$failedManipulations = 0;
$processedImages = 0;
$images = DataObject::get('Image');
2014-08-15 08:53:05 +02:00
if($images && Image::get_backend() == "GDBackend") {
2018-06-01 01:05:03 +02:00
$cache = SS_Cache::factory('GDBackend_Manipulations', 'Output', array('disable-segmentation' => true));
foreach($images as $image) {
$path = $image->getFullPath();
if (file_exists($path)) {
$key = md5(implode('_', array($path, filemtime($path))));
if ($manipulations = unserialize($cache->load($key))) {
$failedManipulations += count($manipulations);
$processedImages++;
$cache->remove($key);
}
}
}
}
2014-08-15 08:53:05 +02:00
echo "Cleared $failedManipulations failed manipulations from
$processedImages Image objects stored in the Database.";
}
2014-08-15 08:53:05 +02:00
}