Laravel 4 Integration

Intervention Image has optional support for Laravel 4 and comes with a Service Provider and Facades for easy integration. After you have installed the Image class correctly, just follow the instructions.

Open your Laravel config file config/app.php and add the following lines.

In the $providers array add the service providers for this package.

‘Intervention\Image\ImageServiceProvider’

Add the facade of this package to the $aliases array.

‘Image’ => ‘Intervention\Image\Facades\Image’

Now the Image Class will be auto-loaded by Laravel.

Examples

Handling images from file uploads

It’s possible to pass an uploaded file directly to the make method.

  1. // resizing an uploaded file
  2. Image::make(Input::file('photo')->getRealPath())->resize(300, 200)->save('foo.jpg');

Attaching images to HTTP responses

To return an image directly, you can add it directly to a response and it will be returned in JPG format. Don’t forget to add a proper content-type.

  1. // create/resize image from file
  2. $image = Image::make('public/foo.png')->resize(300, 200);
  3. // return response
  4. return Response::make($image, 200, array('Content-Type' => 'image/jpeg'));

If you want to attach other formats than the default JPG, use the encode method.

  1. // create/resize image from file
  2. $image = Image::make('public/foo.jpg')->resize(300, 200);
  3. // create response and add formated image
  4. $response = Response::make($image->encode('png'));
  5. // set content-type
  6. $response->header('Content-Type', 'image/png');
  7. // et voila
  8. return $response;

It’s also possible to use the shortcut method response to create the HTTP response automatically.

  1. Route::get('/', function()
  2. {
  3. $img = Image::make('public/foo.jpg');
  4. return $img->response();
  5. });