PHPackages                             quazardous/imagestack - 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. quazardous/imagestack

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

quazardous/imagestack
=====================

A PHP Image Serving Framework

1.7.0(1y ago)4181↓100%1MITPHPPHP ^7.1

Since Jan 26Pushed 1y ago2 watchersCompare

[ Source](https://github.com/quazardous/ImageStack)[ Packagist](https://packagist.org/packages/quazardous/imagestack)[ RSS](/packages/quazardous-imagestack/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (4)Versions (24)Used By (1)

ImageStack
==========

[](#imagestack)

A PHP image serving framework.

The main goal is to provide a robust framework to create an "on the fly" image thumbnailer generator similar to [imagecache](https://www.drupal.org/project/imagecache) / [image style](https://www.drupal.org/docs/8/core/modules/image/working-with-images) in [Drupal](https://www.drupal.org/).

This project is only the framework part. The framework is brick designed so that you can put together your own image serving project and add special bricks.

Framework integration:
----------------------

[](#framework-integration)

- [Symfony 4](https://symfony.com/4) bundle: [Imagestack Bundle](https://github.com/quazardous/imagestack-bundle)
- [Silex](https://silex.symfony.com/) provider: [SilexImageStack](https://github.com/quazardous/SilexImageStack)

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

[](#installation)

```
composer require quazardous/imagestack

```

Concept
-------

[](#concept)

### Typical flow

[](#typical-flow)

This describes the typical processing we want to achieve.

When the **front controller** handles an image HTTP request:

- the **front controller** will call **image stack** with the **image path**.
- the **image stack** will fetch the image using the **image backend**.
- the **image stack** will apply **image manipulator**.
- the **image stack** will store the image using the **storage backend**.
- the **image stack** will return the image to the **front controller**.
- the **front controller** will serve the image.

Ideally the image file will be stored so that the next HTTP request will be statically served.

### Lexicon / bricks

[](#lexicon--bricks)

#### Image path

[](#image-path)

The path of the image that could come from the front controller.

See `ImageStack\Api\ImagePathInterface`.

#### Image

[](#image)

The image content we are willing to serve.

See `ImageStack\Api\ImageInterface`.

#### Image backend

[](#image-backend)

Something that can provide image content.

See `ImageStack\Api\ImageBackendInterface`.

#### Image manipulator

[](#image-manipulator)

Service that can modify/transform/optimize the image content.

See `ImageStack\Api\ImageManipulatorInterface`.

#### Storage backend

[](#storage-backend)

Something where we can store the image content.
Typically we could store it onto the file system so that next HTTP query will be statically serve.

See `ImageStack\Api\StorageBackendInterface`.

#### Image stack

[](#image-stack)

Top of the iceberg class.

See `ImageStack\Api\ImageStackInterface`.

Usage
-----

[](#usage)

### Code example

[](#code-example)

This pseudo controller creates an image stack that will:

- fetch image from `https://images.example.com/backend/` + `$path`
- optimize it with `jpegtran`
- store it on `'/var/www/my/local/image/storage/'` + `$path`
- serve it

```
function myImageController($path)
{
    $stack = new \ImageStack\ImageStack(
        new \ImageStack\ImageBackend\HttpImageBackend('https://images.example.com/backend/'),
        new \ImageStack\StorageBackend\FileStorageBackend('/var/www/my/local/image/storage/'));
    $oim = new \ImageStack\ImageManipulator\OptimizerImageManipulator();
    $oim->registerImageOptimizer(new \ImageStack\ImageOptimizer\JpegtranImageOptimizer());
    $stack->addImageManipulator($oim);
    $image = $stack->stackImage(new \ImageStack\ImagePath($path));
    return $image->getBinaryContent();
}
```

### Implementation

[](#implementation)

#### Image stack

[](#image-stack-1)

A basic image stack.

See `ImageStack\ImageStack`

#### Image backends

[](#image-backends)

##### File image backend

[](#file-image-backend)

See `ImageStack\ImageBackend\FileImageBackend`

##### HTTP image backend

[](#http-image-backend)

See `ImageStack\ImageBackend\HttpImageBackend`

##### Cache image backend

[](#cache-image-backend)

Add a cache layer before an image backend.

See `ImageStack\ImageBackend\CacheImageBackend`

##### Sequential image backend

[](#sequential-image-backend)

Sequentially fetch image from a queue of image backends and return the first match.

See `ImageStack\ImageBackend\SequentialImageBackend`

#### Image manipulators

[](#image-manipulators)

##### Converter image manipulator

[](#converter-image-manipulator)

Convert image type.

See `ImageStack\ImageManipulator\ConverterImageManipulator`

##### Optimizer image manipulator

[](#optimizer-image-manipulator)

Optimize image.

See `ImageStack\ImageManipulator\OptimizerImageManipulator`

`jpegtran` wrapper, see `ImageStack\ImageOptimizer\JpegtranImageOptimizer`.

`pngcrush` wrapper, see `ImageStack\ImageOptimizer\PngcrushImageOptimizer`.

##### Thumbnailer image manipulator

[](#thumbnailer-image-manipulator)

Create an image thumbnail with (path) rules.

See `ImageStack\ImageManipulator\ThumbnailerImageManipulator`

Path pattern rule, see `ImageStack\ImageManipulator\ThumbnailRule\PatternThumbnailRule`

You can associate path patterns to thumbnail formats:

```
use ImageStack\ImageManipulator\ThumbnailerImageManipulator;
use Imagine\Gd\Imagine;
use ImageStack\ImagePath;
use ImageStack\ImageManipulator\ThumbnailRule\PatternThumbnailRule;
...

$rules = [
    '|^images/styles/big/|' => '
