Home

Awesome

middlewares/image-manipulation

Latest Version on Packagist Software License Testing Total Downloads

Middleware to transform images on demand, allowing resize, crop, rotate and transform to other formats. Uses imagecow library that can detect and use Gd and Imagick, and also has support for client hints and different automatic cropping methods.

The uri is generated encoding the image path and the manipulation options with lcobucci/jwt, to prevent alterations and image-resize attacks.

Note: To keep the SRP, this middleware does not provide the following functionalities, that should be delegated to other middleware:

It's possible to combine this library with middlewares/filesystem that allows to read and write to the filesystem. (See example below).

Requirements

Installation

This package is installable and autoloadable via Composer as middlewares/image-manipulation.

composer require middlewares/image-manipulation

Example

The following example uses also middlewares/filesystem to read/save the manipulated images.

use Middlewares\ImageManipulation;
use Middlewares\Reader;
use Middlewares\Writer;

//You need a signature key
$key = 'sdf6&-$<@#asf';

//Manipulated images directory
$cachePath = '/path/to/cache';

//Original images directory
$imagePath = '/path/to/images';

$dispatcher = new Dispatcher([
    //read and returns the manipulated image if it's currently cached
    Reader::createFromDirectory($cachePath)->continueOnError(),

    //saves the manipulated images returned by the next middleware
    Writer::createFromDirectory($cachePath),

    //transform the image
    new Middlewares\ImageManipulation($key),

    //read and return a response with original image if exists
    Reader::createFromDirectory($imagePath)->continueOnError(),

    //In your views
    function () {
        //Create a manipulated image uri
        $uri = Middlewares\ImageManipulation::getUri('image.jpg', 'resizeCrop,500,500,CROP_ENTROPY');

        echo "<img src='{$uri}' alt='Manipulated image' width=500 height=500>";
    }
]);

$response = $dispatcher->dispatch(new ServerRequest($uri));

Usage

You need a key to sign the uri. This prevent attacks and alterations to the path.

$key = 'super-secret-key';

$imageManipulation = new Middlewares\ImageManipulation($key);

Optionally, you can provide a Psr\Http\Message\StreamFactoryInterface as the second argument to create the new response stream with the image. If it's not defined, Middleware\Utils\Factory will be used to detect it automatically.

$key = 'super-secret-key';
$streamFactory = new MyOwnStreamFactory();

$imageManipulation = new Middlewares\ImageManipulation($key, $streamFactory);

clientHints

This option allows to use client hints, that is disabled by default. If this method is called with the default arguments, the allowed hints are ['Dpr', 'Viewport-Width', 'Width']. Note that client hints are supported only by Chrome and Opera browsers

library

The library to use. It can be Gd or Imagick. It's autodetected if it's not specified.

Helpers

getUri

To ease the uri creation this static method is provided, accepting three arguments:

use Middlewares\ImageManipulation;

$image = '/img/avatar.jpg';
$transform = 'resizeCrop,200,200';

$uri = ImageManipulation::getUri($image, $transform);

echo '<img src="'.$uri.'" alt="My image">';

Please see CHANGELOG for more information about recent changes and CONTRIBUTING for contributing details.

The MIT License (MIT). Please see LICENSE for more information.