The ImagineServiceProvider provides integration with Imagine in Silex.
- imagine.factory (optional): Image factory to use (Gd, Gmagick, Imagick)
- imagine: Imagine service to load/create images (see the API)
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',
));
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.