Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Imagecache Laravel Package

intervention/imagecache

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require intervention/imagecache
    

    Add to config/app.php:

    'providers' => [
        Intervention\ImageCache\ImageCacheServiceProvider::class,
    ],
    
  2. Publish Config (Optional):

    php artisan vendor:publish --provider="Intervention\ImageCache\ImageCacheServiceProvider"
    

    Config file: config/imagecache.php (default: filesystem driver).

  3. 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
    

Implementation Patterns

Core Workflow

  1. 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
    
  2. 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);
    
  3. Cache Invalidation:

    // Manually clear cache for a specific key
    ImageCache::clear('thumbs/800x600_rotated.jpg');
    
    // Clear all cached images (use sparingly!)
    ImageCache::clearAll();
    

Integration Tips

  • 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') }}">
    

Gotchas and Tips

Pitfalls

  1. Abandoned Package:

  2. Cache Key Collisions:

    • Default key generation may not account for all operations (e.g., ->sharpen()).
    • Fix: Extend the Intervention\ImageCache\CacheKeyGenerator class or override keys manually.
  3. Memory Leaks:

    • Caching large images in file driver can bloat storage.
    • Tip: Use redis or memcached for ephemeral caches and set short TTLs.
  4. Race Conditions:

    • Concurrent requests may regenerate cached images if not atomic.
    • Solution: Use Laravel’s cache()->lock() or Redis transactions.

Debugging

  • 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
    

Extension Points

  1. Custom Cache Drivers: Extend Intervention\ImageCache\CacheManager to support new drivers (e.g., S3).

  2. Event Listeners: Hook into imagecache.cached or imagecache.missed events (if supported in forks).

  3. 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)
    

Configuration Quirks

  • 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.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky