HTTP Responses
Transform an image into a HTTP response
2.9M Downloads / Month
Open Source MIT License
2.9M Downloads / Month
Open Source MIT License
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.
// create a new image resource
$img = Image::canvas(800, 600, '#ff0000');
// send HTTP header and output image data
echo $img->response('jpg', 70);
// 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.
In Laravel applications it is almost the same thing, apart from that you can return the method's output directly from your route.
Route::get('/', function()
{
$img = Image::canvas(800, 600, '#ff0000');
return $img->response();
});
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