Version: current

     1 <?php
     2 /**
     3  * Remove all cached/generated images that have been created as the result of a manipulation method being called on a
     4  * {@link Image} object
     5  *
     6  * @package sapphire
     7  * @subpackage filesystem
     8  */
     9 class FlushGeneratedImagesTask extends BuildTask {
    10
    11     protected $title = 'Flush Generated Images Task';
    12
    13     protected $description = 'Remove all cached/generated images created as the result of an image manipulation';
    14
    15     /**
    16      * Check that the user has appropriate permissions to execute this task
    17      */
    18     public function init() {
    19         if(!Director::is_cli() && !Director::isDev() && !Permission::check('ADMIN')) {
    20             return Security::permissionFailure();
    21         }
    22
    23         parent::init();
    24     }
    25
    26     /**
    27      * Actually clear out all the images
    28      */
    29     public function run($request) {
    30         $processedImages = 0;
    31         $removedItems    = 0;
    32
    33         if($images = DataObject::get('Image')) foreach($images as $image) {
    34             if($deleted = $image->deleteFormattedImages()) {
    35                 $removedItems += $deleted;
    36             }
    37
    38             $processedImages++;
    39         }
    40
    41         echo "Removed $removedItems generated images from $processedImages Image objects stored in the Database.";
    42     }
    43
    44 }

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -