HTTP Responses

Transform an image into a HTTP response

2.7M Downloads / Month

Open Source MIT License

You are viewing the documentation for Intervention Image 2, this version has reached EOL. Please consider upgrading to version 3.

The easiest way to return an image directly to the users browser, is to output the response() method. It will automatically send HTTP headers according to the currently image and output encoded image data.

Sending a HTTP response

// create a new image resource
$img = Image::canvas(800, 600, '#ff0000');

// send HTTP header and output image data
echo $img->response('jpg', 70);

Sending HTTP responses manually

// create a new image resource
$img = Image::canvas(800, 600, '#ff0000');

// send HTTP header and output image data
header('Content-Type: image/png');
echo $img->encode('png');

Read more about HTTP responses in the api documentation.

HTTP responses in Laravel Applications

In Laravel applications it is almost the same thing, apart from that you can return the method's output directly from your route.

Sending a HTTP response in Laravel

Route::get('/', function()
{
    $img = Image::canvas(800, 600, '#ff0000');

    return $img->response();
});

Attaching images to a HTTP response in Laravel Applications

Route::get('/', function()
{
    $img = Image::canvas(800, 600, '#ff0000');

    // create response and add encoded image data
    $response = Response::make($img->encode('png'));

    // set content-type
    $response->header('Content-Type', 'image/png');
    
    // output
    return $response;
});
Edit