Tam
Tam

Reputation: 780

Clear the cache of all manipulations of an image using Intervention/ImageCache with Laravel

I'm trying to work out a way to clear the cache of all manipulations of an image that have been created using Intervention/ImageCache's manipulation templates (and likely in the future the cache function as well).

I have a some manipulation templates that run a custom filters on the image:

'thumbnail' => function($image) {
    return $image->filter(new ImageFilters\Fit(150));
},

The filter gets the focal point from the database and then runs the appropriate functions, using the focal point for the position as needed.

public function applyFilter(Image $image)
{
    $fp = Media::where('file_name', $image->filename)->pluck('focal_point');
    $fp = $fp ?: 'center';
    return $image->fit($this->width, $this->height, function(){return true;}, $fp);
}

This all works fine, but when the focal point is changed by the user the image isn't updated because of the cache. I need a way of clearing the cache for that specific image across the board.

From what I can tell each manipulated image is given its own unique cache key made up of an md5 hash, but I can't find any way of targeting all manipulations of a single image.

Upvotes: 2

Views: 4729

Answers (2)

Richard Keil
Richard Keil

Reputation: 100

Even if the question lies quite in the past, I would like to present a solution:

// get an instance of the ImageCache class
$imageCache = new \Intervention\Image\ImageCache();

// get a cached image from it and apply all of your templates / methods
$image = $imageCache->make($pathToImage)->filter(new TemplateClass);

// remove the image from the cache by using its internal checksum
Cache::forget($image->checksum());

Execute this lines whenever you make a change to your image which affects the applied filters (e.g. a focal point). When the cached image is requested, it will be created again with new values.

Hope this helps, Richard

Upvotes: 3

Meinama
Meinama

Reputation: 109

Clearing the cache for one image is not possible. The string for the focal_point must be in the callback signature, to take effect on the caching.

You can do this, by adding it to the Filter, by GET parameter for example.

'thumbnail' => function($image) {
    $fp = Input::get('fp', 'center');
    return $image->filter(new ImageFilters\Fit(150, $fp));
},

Then request your image like this:

/imagecache/thumbnail/foo.jpg?fp=center

Upvotes: 2

Related Questions