Installation:
composer require intervention/imagecache
Add to config/app.php:
'providers' => [
Intervention\ImageCache\ImageCacheServiceProvider::class,
],
Publish Config (Optional):
php artisan vendor:publish --provider="Intervention\ImageCache\ImageCacheServiceProvider"
Config file: config/imagecache.php (default: filesystem driver).
First Use Case:
use Intervention\ImageCache\Facades\ImageCache;
$img = ImageCache::make('path/to/image.jpg')
->resize(300, 200)
->cache('resized_300x200.jpg', 3600); // Cache for 1 hour
Cache-Aware Image Manipulation:
// Always use the facade for Laravel integration
$cachedImage = ImageCache::make('original.jpg')
->resize(800, 600)
->rotate(10)
->cache('thumbs/800x600_rotated.jpg', 86400); // 24h TTL
Cache Key Customization:
// Override default key generation (e.g., for dynamic paths)
$key = 'custom_key_' . md5($request->input('user_id'));
ImageCache::make('user_avatar.jpg')->cache($key, 3600);
Cache Invalidation:
// Manually clear cache for a specific key
ImageCache::clear('thumbs/800x600_rotated.jpg');
// Clear all cached images (use sparingly!)
ImageCache::clearAll();
Middleware for Dynamic Caching:
// Cache images based on user roles
if (auth()->check()) {
$key = 'user_' . auth()->id() . '_' . $request->path();
ImageCache::make('image.jpg')->cache($key, 3600);
}
Cache Storage Drivers: Leverage Laravel’s cache drivers (Redis recommended for high traffic):
// In config/imagecache.php
'driver' => env('CACHE_DRIVER', 'redis'),
Blade Directives:
// Create a Blade directive for cached images
Blade::directive('cachedImage', function ($expression) {
return "<?php echo ImageCache::make({$expression})->cache('blade_{$expression}', 3600); ?>";
});
Usage:
<img src="{{ cachedImage('path/to/image.jpg') }}">
Abandoned Package:
Cache Key Collisions:
->sharpen()).Intervention\ImageCache\CacheKeyGenerator class or override keys manually.Memory Leaks:
file driver can bloat storage.redis or memcached for ephemeral caches and set short TTLs.Race Conditions:
cache()->lock() or Redis transactions.Verify Cache Hits/Misses:
if (ImageCache::isCached('key')) {
logger("Cache hit for key: key");
}
Inspect Cache Contents:
// For filesystem driver
Storage::disk('cache')->files('intervention/imagecache');
Clear Stale Caches:
php artisan cache:clear
php artisan config:clear
Custom Cache Drivers:
Extend Intervention\ImageCache\CacheManager to support new drivers (e.g., S3).
Event Listeners:
Hook into imagecache.cached or imagecache.missed events (if supported in forks).
Queue Image Processing: For long-running operations, offload caching to a queue:
ImageCache::make('large_image.jpg')
->resize(2000, 2000)
->cache('queued_large.jpg', 86400)
->dispatch(); // Hypothetical method (requires custom implementation)
TTL Units:
Values in config/imagecache.php are in seconds (not minutes/hours).
'default_ttl' => 3600, // 1 hour
Cache Directory:
Default path: storage/framework/cache/intervention/imagecache.
Ensure storage/ is writable and backed up.
Fallback Behavior: If cache fails, the package falls back to generating the image (but logs the miss). Monitor for failures in production.
How can I help you explore Laravel packages today?