# Return image as response

**URL:** <https://discourse.slimframework.com/t/return-image-as-response/295>\
**Category:** Questions\
**Created:** [May 22, 2016, 3:56am UTC](https://discourse.slimframework.com/t/return-image-as-response/295 "2016-05-22T03:56:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shayo](https://avatars.discourse-cdn.com/v4/letter/s/e99b99/32.png) [@shayo](https://discourse.slimframework.com/u/shayo)\
**Post date:** [May 22, 2016, 3:56am UTC](https://discourse.slimframework.com/t/return-image-as-response/295/1 "2016-05-22T03:56:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shayo](https://avatars.discourse-cdn.com/v4/letter/s/e99b99/32.png) [@shayo](https://discourse.slimframework.com/u/shayo)\
**Post date:** [May 22, 2016, 3:59am UTC](https://discourse.slimframework.com/t/return-image-as-response/295/2 "2016-05-22T03:59:04Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![paulosilva0306](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/paulosilva0306/32/640_2.png) [@paulosilva0306](https://discourse.slimframework.com/u/paulosilva0306)\
**Post date:** [March 8, 2018, 7:15pm UTC](https://discourse.slimframework.com/t/return-image-as-response/295/3 "2018-03-08T19:15:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![enriquerene](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/enriquerene/32/712_2.png) [@enriquerene](https://discourse.slimframework.com/u/enriquerene)\
**Post date:** [July 16, 2018, 1:10pm UTC](https://discourse.slimframework.com/t/return-image-as-response/295/4 "2018-07-16T13:10:52Z")

</div>

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