Return image as response


#1

I need help with a very simple problem.
I want to return an image as my response.
I’ve tried the following:

        $response->withHeader("Content-type", 'image/jpg');
        $body = $response->getBody();
       $image = file_get_contents($fname)
        $body->write($image);

but the browser doesn’t recognize this as an image (it spits out the binary).
How do I return an image as a response?
I need this for an automatic image resizing. I’ve tried to use tuupola/slim-image-resize, but it is not compatible with v3.0. I am currently trying to upgrade it and I almost got it to work, except I have this annoying problem.

Help will be much appreciated.

Thank you!

Shay


Output Image from route
#2

Darn. I should have searched better.
This was already answered:

           $response->write($image);
           return $response->withHeader('Content-Type', FILEINFO_MIME_TYPE);