Sending Images via Response


#1

Hi. I am new to back end development.

I was wondering how Slim 3 (or native PHP 7) can respond to requests with physical images?

e.g.

   app.get("/foo/bar/img1", function($request, $response, $args) {
     // get img from local folder on server
     // send / stream that image to guzzle client
     return $response.**_what?_**
   }); 

I know this is probably a very broad question, but any tips or pointers to the right direction
would be great! Thanks :blush:


#2

I haven’t done this, but I’d assume all you would need to do would be to write the contents of the image file to the response and change the content type.

$image = file_get_contents('image.png');
$response = $response->write($image);
return $response->withHeader('Content-Type', 'img/png'); // set correct content-type