flobbos / laravel-image-cache
Replacement for the now abandoned imagecache package by Intervention
Installs: 8
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:laravel
Requires
- php: ^8.1
- illuminate/cache: ^10.0|^11.0|^12.0
- intervention/image: ^3.0
README
Laravel Image Cache is a package that provides an easy-to-use caching mechanism for image manipulation in Laravel. It serves as a replacement for the now-abandoned imagecache package by Intervention.
Features
- Cache manipulated images for improved performance.
- Support for multiple image drivers: GD, Imagick, and Libvips.
- Define custom image manipulation templates.
- Configurable cache lifetime and output formats.
- Dynamic routing for serving cached images.
Installation
-
Install the package via Composer:
composer require flobbos/laravel-image-cache
-
Publish the configuration file:
php artisan vendor:publish --tag=config --provider="Flobbos\LaravelImageCache\ImageCacheServiceProvider"
-
Configure the
config/imagecache.php
file as needed.
Usage
Basic Example
To use the package, you can define templates in the configuration file and use the ImageCache
facade to generate cached images.
use Flobbos\LaravelImageCache\Facades\ImageCache; // Example: Apply the "small" template to an image $cachedImage = ImageCache::template('path/to/image.jpg', 'small');
Dynamic Image Routes
The package provides a dynamic route for serving cached images. For example:
/images/{template}/{path}
template
: The name of the template defined in the configuration file.path
: The relative path to the image in thepublic/images
directory.
Example URL:
http://your-app.test/images/small/example.jpg
Custom Templates
You can define custom templates by creating a class that implements a build
method. For example:
namespace App\ImageTemplates; use Intervention\Image\Interfaces\ImageInterface; class CustomTemplate { public function build(ImageInterface $image) { $image->resize(800, 600); } }
Then, register the template in the config/imagecache.php
file:
'templates' => [ 'custom' => \App\ImageTemplates\CustomTemplate::class, ],
Configuration
The configuration file config/imagecache.php
allows you to customize:
- Cache lifetime
- Image processing driver
- Cache store
- Default output format
- Return object behavior
- Image manipulation templates
- Paths to search for images
Paths
You can specify one or more directories where the package should look for images.
Example from config/imagecache.php
:
'paths' => [ public_path('images'), storage_path('app/public/images'), ],
When a request is made, the package will search these directories for the requested image file in the order listed.
Requirements
- PHP 8.1 or higher
- Laravel 10 or higher
- Intervention Image 3.0 or higher
License
This package is open-sourced software licensed under the MIT license.
Credits
- Developed by Alexej Krzewitzki (alexej@helloo.it)
- Inspired by the original imagecache package by Intervention