Skip to content
This repository has been archived by the owner on Jul 15, 2018. It is now read-only.

Latest commit

 

History

History
52 lines (34 loc) · 1.77 KB

imagine.md

File metadata and controls

52 lines (34 loc) · 1.77 KB

Imagine Service Provider for Silex

The ImagineServiceProvider provides integration with Imagine in Silex.

Parameters

  • imagine.factory (optional): Image factory to use (Gd, Gmagick, Imagick)

Services

  • imagine: Imagine service to load/create images (see the API)

Registering

If you are using composer to include the SilexServiceProvider in your project, you do not need to register anything. Composer automatically adds the appropriate namespaces to the autoloader.

In case you're not using composer, you will need to register the Imagine namespace in your autoloader yourself. Make sure you place a copy of Imagine in the vendor/imagine directory.

Register the service provider in your Silex application.

$app->register(new Grom\Silex\ImagineServiceProvider(), array(
    'imagine.factory' => 'Gd',
    'imagine.base_path' => __DIR__.'/vendor/imagine',
));

Usage

The Imagine service provider provide an imagine service.

$app->get('/thumb/{file}', function($file) use ($app) {
    $image = $app['imagine']->open('images/'.$file);

    $transformation = new Imagine\Filter\Transformation();
    $transformation->thumbnail(new Imagine\Image\Box(200, 200));
    $image = $transformation->apply($image);

    $format = pathinfo($file, PATHINFO_EXTENSION);

    $response = new Symfony\Component\HttpFoundation\Response();
    $response->headers->set('Content-type', 'image/'.$format);
    $response->setContent($image->get($format));

    return $response;
});

Put some images inside a directory name images. This will create a thumbnail of the image with a size of 200px.