Jmg
, (*1)
, (*2)
Just In Time Image manipulation: Library for HTTP based image manipulation
Installation
> composer composer require thapp/jmg
Run tests
> composer install
> vendor/bin/phpunit -c phpunit.xml.dist
Quick start
<?php
use Thapp\Jmg\Resolver\ImageResolver;
$processor = new Thapp\Jmg\Image\Processor(
new Thapp\Image\Driver\Gd\Source
);
$images = new Thapp\Jmg\Resolver\ImageResolver(
$processor,
$pathResolver,
$loaderResolver
// ...
);
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., (*3)
<?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., (*4)
Your custom loader must implement the Thapp\Jmg\Loader\LoaderInterface
or simply extend from Thapp\Jmg\Loader\AbstractLoader
., (*5)
<?php
namespace Acme\Loaders;
use Thapp\Jmg\Loader\AbstractLoader
class AWSLoader extends AbstractLoader
{
public function load($file)
{
//…
}
public function supports($path)
{
//…
}
}
Resolving images with parameters
Using the ImageResolver
class, it is easy to resolve images from parameter strings., (*6)
<?php
use Thapp\Jmg\Image\Processor;
use Thapp\Jmg\Resolver\PathResolver;
use Thapp\Jmg\Resolver\LoaderReslover;
use Thapp\Image\Driver\Imagick\Source;
$res = new ImageResolver(new Processor(new Source), $pathResolver, $loaderResolver);
$params = Parameters::fromString('2/400/400/5');
if ($resource = $res->resolve('images/source.jpg', $params)) {
header('Content-Type: image/jpeg');
echo $resource->getContents();
}
Framework integration
Jmg comes prebundled with support for Laravel 5.* and Silex., (*7)
Laravel 5.*
In config/app.php
, add:, (*8)
<?php
$providers => [
// …
'Thapp\Jmg\Framework\Laravel\JmgServiceProvider'
];
$aliases => [
// …
'Jmg' => 'Thapp\Jmg\Framework\Laravel\Facade\Jmg'
]
Then run, (*9)
$ php artisan vendor:publish
from the command line., (*10)
config/jmg.php
, (*11)
processor
The processor, default is image
. imagine
is experimental and likely to be removed from future releases., (*12)
driver:
The image driver. Available drivers are imagick
, im
(imagemagick binary), and gd
., (*13)
convert_path
If im
is set for the driver, specify the path to the convert binary here., (*14)
identify_path
If im
is set for the driver, specify the path to the identify binary here., (*15)
paths
Source paths aliases, e.g., (*16)
'images' => public_path().'/images', // will be available at `/images/<params>/image.jpg`
'remote' => 'http://images.example.com' // may be empty if you use absolute urls
loaders, (*17)
'loaders' => [
'images' => 'file',
'remote' => 'http',
]
disable_dynamic_processing
Disables image processing via dynamic urls., (*18)
mode_constraints
Set mode constraints on scaling values. This will only affect dynamic processing via URL., (*19)
recipes
Predefined image formats, e.g., (*20)
'thumbs' => [
'images', '1/0/400,filter:palette;p=rgb:clrz;c=#0ff' // will be available at `/thumbs/image.jpg`
],
default_cache
The default caching type. Shipped types are file
, (*21)
default_cache_path
Directory path for local caches., (*22)
Silex