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);

#3

Boa tarde a todos. Estou com um problema na hora de enviar uma imagem do meu banco de dados da minha API para ser consumida, vejam como estou fazendo:
function GetAllCategoriasIMG(){
$pdo = Database::conexao();
$Select = $pdo->prepare(‘SELECT
CD_CATEGO,
NM_CATEGO,
DS_CATEGO,
IMG_CATEGO
FROM categorias’);
$Select->execute();
$data = $Select->fetchAll(PDO::FETCH_ASSOC);

if ($data > 0) {
	echo json_encode($data);
	#echo json_encode($data, JSON_PRESERVE_ZERO_FRACTION); 
} else {
	echo json_encode(array(["status" => "erro", "mensagem" => "Nenhum produto encontrado."]));
}

}

Obs: IMG_CATEGO é o meu campo de imagem.
Se alguém puder me ajudar, agradeço muito.


#4

I didn’t understand how to do that. Is there some way to route an image tag like <img src="http://image/route" />?