bst27/laravel-image-proxy

A Laravel image proxy to cache, minify and modify images very easily.

v0.4.0 2025-06-15 19:12 UTC

This package is auto-updated.

Last update: 2025-06-15 19:14:33 UTC


README

Latest Version on Packagist Tests Total Downloads

A Laravel image proxy to cache, minify and manipulate images very easily.

Features

  • Automatic image compression / minification
  • Automatic caching of images on host and client side
  • Flexible storage options (local, S3, SFTP etc.)
  • Optional: image manipulation (resizing etc.)
  • Optional: custom image filename in URL
  • Easily extendable with custom strategies for image manipulation

Installation

composer require bst27/laravel-image-proxy

Per default spatie/image-optimizer is used to compress images. So make sure you install the required dependencies as described in their docs.

Usage

Use the global helper function proxy_image() to generate a secure image URL:

<img src="{{ proxy_image('images/example.jpg') }}" alt="Example">

This will automatically minify and cache the image and generate something like this:

<img src="http://localhost/img/1AXe...S11lg.jpg" alt="Example">

You can also define a strategy and filename for image manipulation:

<img src="{{ proxy_image('images/example.jpg', 'default', 'cat.jpg') }}" alt="A black cat">

This will use the default strategy for image manipulation, keep the given filename and generate something like this:

<img src="http://localhost/img/1AX5...3c1KAw/cat.jpg" alt="A black cat">

Manipulation Strategies

To manipulate images, you can configure different strategies in config/image-proxy.php. The DefaultManipulator strategy uses spatie/image-optimizer to compress images.

'manipulation_strategy' => [
    'default' => [
        'class'  => \Bst27\ImageProxy\Services\ImageManipulator\DefaultManipulator::class,
        'params' => [],
    ],
],

Each strategy class must implement the ImageManipulator contract. You can add your own image manipulation strategy easily:

  1. Implement the contract interface
  2. Add your manipulator to the manipulation_strategy array of the image-proxy.php with a unique strategy key.
  3. Start using it by calling proxy_image() with your strategy key.

Endpoints

Two routes are registered automatically:

  • Short URL:
    /img/{token}.{ext}

  • Named URL:
    /img/{token}/{filename}

URLs to these routes are generated via proxy_image() and require a valid encrypted payload token. You can customize them via the config.

Storage

Flysystem is used to offer flexible storage options. Per default the original images are read from the local storage disk. The cached images are stored on the local storage disk, too. You can customize the used storage disks using the plugin config or environment settings.

Configuration

Check image-proxy.php for default config. You can customize config via environment variables or publish the config file:

php artisan vendor:publish --provider="Bst27\ImageProxy\ImageProxyServiceProvider"

This will create config/image-proxy.php.

Tests

To run tests, you can execute the following command:

docker run --rm -it \
  -u "$(id -u):$(id -g)" \
  -v "$PWD":/var/www/html \
  -w /var/www/html \
  laravelsail/php84-composer:latest \
  php vendor/bin/phpunit