bagisto / image-cache
Caching extension for the Intervention Image Class
Requires
- php: ^8.2
- illuminate/cache: ^9|~10|~11
- illuminate/filesystem: ^9|~10|~11
- intervention/image: ~2.2
- nesbot/carbon: ^2.72.2
- opis/closure: ^3.5
Requires (Dev)
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2024-11-03 06:38:36 UTC
README
Note
This is a fork of the Intervention package. The original Intervention package is currently abandoned, so we are only using its basic functionalities.
Caution
Currently, this package is unstable because it also depends on the intervention/image
package, where the config path is registered. To make it work, we have turned off auto-discovery in Bagisto and overridden the necessary provider at this stage. In the future, we will either maintain this package or look for an alternative.
Intervention Image Cache extends the Intervention Image Class package to be capable of image caching functionality.
The library uses the Illuminate/Cache package and can be easily integrated into the Laravel Framework. Based on your Laravel cache configuration you are able to choose between Filesystem, Database, Memcached or Redis for the temporary buffer store.
The principle is simple. Every method call to the Intervention Image class is captured and checked by the caching interface. If this particular sequence of operations already have taken place, the data will be loaded directly from the cache instead of a resource-intensive image operation.
Installation
You can install this package quickly and easily with Composer.
Require the package via Composer:
$ composer require bagisto/image-cache:dev-master
Now you are able to require the vendor/autoload.php
file to PSR-4 autoload the library.
Laravel Integration
The Image Cache class supports Laravel integration. Best practice to use the library in Laravel is to add the ServiceProvider and Facade of the Intervention Image Class.
Open your Laravel config file config/app.php
and add the following lines.
In the $providers
array add the service providers for this package.
'providers' => array(
[...]
'Intervention\Image\ImageServiceProvider'
),
Usage
The Image Cache is best called by the static method Image::cache
from the Intervention Image class.
To create cached images just use the static method Image::cache
and pass the image manipulations via closure. The method will automatically detect if a cached file for your particular operations exists.
// run the operations on the image or read a file // for the particular operations from cache $img = Image::cache(function($image) { return $image->make('public/foo.jpg')->resize(300, 200)->greyscale(); });
Determine a lifetime in minutes for the cache file as an optional second parameter. Pass a boolean true as optional third parameter to return an Intervention Image object instead of a image stream.
// determine a lifetime and return as object instead of string $img = Image::cache(function($image) { return $image->make('public/foo.jpg')->resize(300, 200)->greyscale(); }, 10, true);
Server configuration
If you have Static Resources caching enabled on Nginx please add your cache directory ({route} in config) to static resources handler exclusion:
# where "cache" is {route}
location ~* ^\/(?!cache).*\.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|webp|woff|woff2)$ {
expires max;
access_log off;
add_header Cache-Control "public";
}
License
Intervention Imagecache Class is licensed under the MIT License.