iwyg / jmg by iwyg

Just in time image manipulation.
100
1
3
Package Data
Maintainer Username: iwyg
Maintainer Contact: mail@thomas-appel.com (Thomas Appel)
Package Create Date: 2015-11-02
Package Last Update: 2016-03-16
Home Page: https://jmg.thomas-appel.com
Language: PHP
License: MIT
Last Refreshed: 2024-04-26 03:00:47
Package Statistics
Total Downloads: 100
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 1
Total Watchers: 3
Total Forks: 0
Total Open Issues: 0

Jmg

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)
    {
        //…
    }
}