PHPackages                             marceli-to/intervention-image-cache - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Image &amp; Media](/categories/media)
4. /
5. marceli-to/intervention-image-cache

ActiveLibrary[Image &amp; Media](/categories/media)

marceli-to/intervention-image-cache
===================================

A simple image caching package for Intervention Image v3

v2.0.1(1y ago)020MITPHPPHP ^8.1

Since Mar 13Pushed 1y ago1 watchersCompare

[ Source](https://github.com/marceli-to/intervention-image-cache)[ Packagist](https://packagist.org/packages/marceli-to/intervention-image-cache)[ RSS](/packages/marceli-to-intervention-image-cache/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (5)Versions (11)Used By (0)

Intervention Image Cache
========================

[](#intervention-image-cache)

A simple image caching package for Laravel using Intervention Image v3.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require marceli-to/intervention-image-cache
```

Intervention Image v3 Compatibility
-----------------------------------

[](#intervention-image-v3-compatibility)

This package is built for Intervention Image v3, which has a significantly different API compared to v2. If you're upgrading from a package that used Intervention Image v2, please note these key differences:

- The v3 API uses interfaces like `ImageInterface` and `ModifierInterface`
- Image manipulation methods return a new image instance rather than modifying the original
- The driver system has changed (GD is used by default in this package)

For more details on Intervention Image v3, please refer to the [official documentation](https://image.intervention.io/v3).

Configuration
-------------

[](#configuration)

Publish the configuration file:

```
php artisan vendor:publish --tag=image-cache-config
```

This will create a `config/image-cache.php` file where you can configure:

- Cache path
- Cache lifetime
- Image search paths
- Available templates
- Route configuration

Usage
-----

[](#usage)

### Basic Usage

[](#basic-usage)

```
use MarceliTo\InterventionImageCache\Facades\ImageCache;

// Get a cached image
$path = ImageCache::getCachedImage('large', 'image.jpg');

// Display the image in a view

```

### In a Controller

[](#in-a-controller)

```
use MarceliTo\InterventionImageCache\Facades\ImageCache;

class ImageController extends Controller
{
    public function show($template, $filename)
    {
        $path = ImageCache::getCachedImage($template, $filename);

        if (!$path) {
            abort(404);
        }

        return response()->file($path);
    }
}
```

### In Views

[](#in-views)

The package automatically registers the necessary routes, so you can use it directly in your views:

```

```

The URL format is:

```
/img/{template}/{filename}/{maxWidth?}/{maxHeight?}/{coords?}

```

Where:

- `template`: One of the templates defined in your config (e.g., 'large', 'small', 'thumbnail', 'crop')
- `filename`: The name of the image file to process
- `maxWidth`: (Optional) Maximum width for the output image
- `maxHeight`: (Optional) Maximum height for the output image
- `coords`: (Optional) Comma-separated string in the format `x,y,width,height` for cropping

**Important notes about cropping coordinates:**

- All four values must be numeric
- x and y coordinates must be non-negative (values less than 0 will be set to 0)
- Width and height must be positive (values less than or equal to 0 will skip cropping)
- Coordinates that exceed image dimensions will be adjusted automatically
- If all coordinates are 0 (0,0,0,0), cropping will be skipped

### Custom Controller

[](#custom-controller)

If you prefer to use your own controller, you can disable the automatic route registration in the config file:

```
// config/image-cache.php
'register_routes' => false,
```

Then create your own route and controller:

```
// routes/web.php
Route::get('/img/{template}/{filename}/{maxW?}/{maxH?}/{coords?}', [ImageController::class, 'getResponse']);

// App\Http\Controllers\ImageController.php
public function getResponse($template, $filename, $maxW = null, $maxH = null, $coords = null)
{
    $params = [];

    if ($maxW) {
        $params['maxWidth'] = (int) $maxW;
    }

    if ($maxH) {
        $params['maxHeight'] = (int) $maxH;
    }

    if ($coords) {
        $params['coords'] = $coords;
    }

    $path = ImageCache::getCachedImage($template, $filename, $params);

    if (!$path || !file_exists($path)) {
        abort(404);
    }

    $mime = mime_content_type($path);
    $content = file_get_contents($path);

    return response($content)
        ->header('Content-Type', $mime)
        ->header('Cache-Control', 'public, max-age=31536000');
}
```

### Programmatic Usage

[](#programmatic-usage)

You can also use the package programmatically:

```
use MarceliTo\InterventionImageCache\Facades\ImageCache;

// Get a cached image
$path = ImageCache::getCachedImage('large', 'image.jpg', [
    'maxWidth' => 1200,
    'maxHeight' => 800
]);

// Get a cached image with cropping
$path = ImageCache::getCachedImage('crop', 'image.jpg', [
    'maxWidth' => 800,
    'maxHeight' => 600,
    'coords' => '100,150,500,300'  // Format: x,y,width,height
]);
```

### Clearing the Cache

[](#clearing-the-cache)

You can clear the cache using the provided Artisan command:

```
# Clear all cached images
php artisan image:clear-cache

# Clear cached images for a specific template
php artisan image:clear-cache large
```

Or programmatically:

```
use MarceliTo\InterventionImageCache\Facades\ImageCache;

// Clear all cached images
ImageCache::clearCache();

// Clear cached images for a specific template
ImageCache::clearTemplateCache('large');
```

Custom Templates
----------------

[](#custom-templates)

You can create your own templates by:

1. Creating a class that implements `Intervention\Image\Interfaces\ModifierInterface`
2. Registering it in the `templates` array in the config file

Example:

```
