thapp/jmg

Just in time image manipulation.

Maintainers

Details

github.com/iwyg/jmg

Source

Issues

Installs: 100

Dependents: 5

Suggesters: 0

Security: 0

Stars: 1

Watchers: 3

Forks: 0

Open Issues: 0

Type:package

dev-processchain 2016-03-05 15:13 UTC

This package is not auto-updated.

Last update: 2024-11-07 00:48:12 UTC


README

Just In Time Image manipulation: Library for HTTP based image manipulation.

Author Source Code Software License

Build Status Code Coverage HHVM

Installation

> composer composer require thapp/jmg

Run tests

> composer install
> vendor/bin/phpunit -c phpunit.xml.dist

Quick start

Using the ImageResolver class, it is easy to resolve images from parameter strings.

<?php

use Thapp\Jmg\ParamGroup;
use Thapp\Jmg\Image\Processor;
use Thapp\Jmg\Resolver\PathResolver;
use Thapp\Jmg\Resolver\LoaderReslover;
use Thapp\Jmg\Resolver\ImageResolver;

$processor = new Thapp\Jmg\Image\Processor(
	new Thapp\Image\Driver\Gd\Source
);

$images = new ImageResolver($source, $pathResolver, $loaderResolver);

if ($resource = $res->resolve('images/source.jpg', ParamGroup::fromString('2/400/400/5'))) {
    header('Content-Type: image/jpeg');
    echo $resource->getContents();
}

Core concepts

Source loaders and resolvers

Jmg supports loading images from a variety of sources. In the example below, lets assume we have a local filesystem that hosts our images.

<?php

use Thapp\Jmg\Loader\FilesystemLoader;
use Thapp\Jmg\Resolver\LoaderReslover;
use Thapp\Jmg\Resolver\PathResolver;

$loaderResolver = new LoaderResolver;
$pathResolver = new PathResolver;

$pathResolver->add('local', __DIR__.'public/images');
$loaderResolver->add('local', new FilesystemLoader);

// tries to resolve a given prefix path;
if (!$loader === $loaderResolver->resolve('local')) // returns the FilesystemLoader {
    //then error
}

if (null === $path = $pathResolver->resolve('local')) {
    //then error
}

$src = $loader->load($path . '/image.jpg');

Custom loaders

You may create your own loaders, e.g. for loading images from a remote source like an Amazon s3 storage or an ftp server.

Your custom loader must implement the Thapp\Jmg\Loader\LoaderInterface or simply extend from Thapp\Jmg\Loader\AbstractLoader.

<?php

namespace Acme\Loaders;

use Thapp\Jmg\Loader\AbstractLoader

class AWSLoader extends AbstractLoader
{
	/**
	 * @throws Thapp\Jmg\Exception\SourceLoaderException
     * @return Thapp\Jmg\Resource\FileResourceInterface
	 */
    public function load($file)
    {
        //…
    }

	/**
	 * @return bool
	 */
    public function supports($path)
    {
        //…
    }
}