# Return image as output again

**URL:** <https://discourse.slimframework.com/t/return-image-as-output-again/2436>\
**Category:** Questions\
**Created:** [May 25, 2018, 4:57pm UTC](https://discourse.slimframework.com/t/return-image-as-output-again/2436 "2018-05-25T16:57:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aghezzi](https://avatars.discourse-cdn.com/v4/letter/a/7993a0/32.png) [@aghezzi](https://discourse.slimframework.com/u/aghezzi)\
**Post date:** [May 25, 2018, 4:57pm UTC](https://discourse.slimframework.com/t/return-image-as-output-again/2436/1 "2018-05-25T16:57:13Z")

</div>

following examples found here I have this route  
`
$app->get(’/fileimg/’, function (Request $request, Response $response, array $args) {
$file=“C:\data\imgs-campione\350x150.png”;
if(!file_exists($file))
die(“file:$file”);
$image = file_get_contents($file);
if ($image === false)
die(“error getting image”);
$response->write($image);
return $response->withHeader(‘Content-Type’, ‘image/png’);
});
`  
I tried with many files of differents format

Chrome always display a blank screen with a small square in the center

firefox output a msg like : image can’t be displayed because contains errors

but if I run this code in a script outside Slim  
images are outputted correctly in both browser

`
<?php
$file="C:\\data\\imgs-campione\\uhd-1.png";
if(!file_exists($file))
```
die("file:$file");

```
header(‘Content-Type: image/png’);
readfile($file);?>
`

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [May 25, 2018, 10:01pm UTC](https://discourse.slimframework.com/t/return-image-as-output-again/2436/2 "2018-05-25T22:01:56Z")

</div>

Hello @aghezzi

I tried your code and it works fine (in Chrome and Firefox) 🙂

```php
$app->get('/fileimg', function ($request, $response){
    $file = __DIR__. "/aaa.png";
    if (!file_exists($file)) {
        die("file:$file");
    }
    $image = file_get_contents($file);
    if ($image === false) {
        die("error getting image");
    }
    $response->write($image);
    return $response->withHeader('Content-Type', 'image/png');
});

```

---

<div class="post-metadata">

**Author:** ![aghezzi](https://avatars.discourse-cdn.com/v4/letter/a/7993a0/32.png) [@aghezzi](https://discourse.slimframework.com/u/aghezzi)\
**Post date:** [May 26, 2018, 10:59am UTC](https://discourse.slimframework.com/t/return-image-as-output-again/2436/3 "2018-05-26T10:59:34Z")

</div>

Thanks for your test

These are my settings is there something to modify

`
Slim\Collection::__set_state(array(
'data' => 
array (
'httpVersion' => '1.1',
'responseChunkSize' => 4096,
'outputBuffering' => 'append',
'determineRouteBeforeAppMiddleware' => true,
'displayErrorDetails' => true,
'addContentLengthHeader' => false,
'routerCacheFile' => false,
'renderer' => 
array (
'template_path' => 'C:\\wamp\\www\\zencms\\src\\phpTemplates',
`

---

<div class="post-metadata">

**Author:** ![Programster](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/programster/32/1377_2.png) [@Programster](https://discourse.slimframework.com/u/Programster)\
**Post date:** [August 9, 2021, 2:05pm UTC](https://discourse.slimframework.com/t/return-image-as-output-again/2436/4 "2021-08-09T14:05:24Z")

</div>

I just spent an age with the exact same symptoms.

In the end it turned out I had a byte order mark in my index.php file. This is easy to spot in vim:

![-bin-bash_003](https://canada1.discourse-cdn.com/flex030/uploads/slimframework/original/2X/9/9fad9142de9e51a26612d01d0348f3c8fccb7ab6.png)

I should have noticed that my IDE was highlighting it slightly different (notice they grey)

![Selection_179](https://canada1.discourse-cdn.com/flex030/uploads/slimframework/original/2X/c/cb0dbf14311a27c5d537b6ef5befa560adb4b2cf.png)
