# Slim 3 Content Type Image

**URL:** <https://discourse.slimframework.com/t/slim-3-content-type-image/161>\
**Category:** Questions\
**Created:** [April 21, 2016, 8:25am UTC](https://discourse.slimframework.com/t/slim-3-content-type-image/161 "2016-04-21T08:25:23Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![nanaksr](https://avatars.discourse-cdn.com/v4/letter/n/94ad74/32.png) [@nanaksr](https://discourse.slimframework.com/u/nanaksr)\
**Post date:** [April 21, 2016, 8:25am UTC](https://discourse.slimframework.com/t/slim-3-content-type-image/161/1 "2016-04-21T08:25:23Z")

</div>

hi,

I previously used slim framework 2 to display images with the following code:

$app-\>get(’/image/p/:data’, function ($data) use ($app) {  
$image = @file\_get\_contents(“[http://localhost/main/media/image/p/$data](http://localhost/main/media/image/p/%24data)”);  
$app-\>response-\>header(‘Content-Type’, 'content-type: '.FILEINFO\_MIME\_TYPE);  
if ($image === FALSE){  
$app-\>render(‘error404.page’);  
}else{  
echo $image;  
}  
});

and now, how to display with slim framework 3 ?

Thanks

---

<div class="post-metadata">

**Author:** ![llvdl](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/llvdl/32/78_2.png) [@llvdl](https://discourse.slimframework.com/u/llvdl)\
**Post date:** [April 28, 2016, 8:39pm UTC](https://discourse.slimframework.com/t/slim-3-content-type-image/161/2 "2016-04-28T20:39:39Z")

</div>

You could try the following:

```
$app->get('/image/p/{data:\w+}', function($request, $response, $args) {
    $data = $args['data'];
    $image = @file_get_contents("http://localhost/main/media/image/p/$data");
    if($image === FALSE) {
        $handler = $this->notFoundHandler;
        return $handler($request, $response);    
    }
    
    $response->write($image);
    return $response->withHeader('Content-Type', FILEINFO_MIME_TYPE);
});
```

---

<div class="post-metadata">

**Author:** ![nanaksr](https://avatars.discourse-cdn.com/v4/letter/n/94ad74/32.png) [@nanaksr](https://discourse.slimframework.com/u/nanaksr)\
**Post date:** [April 29, 2016, 10:47am UTC](https://discourse.slimframework.com/t/slim-3-content-type-image/161/3 "2016-04-29T10:47:17Z")

</div>

Thanks @llvdl your code is work.

---

<div class="post-metadata">

**Author:** ![Dominic](https://avatars.discourse-cdn.com/v4/letter/d/13edae/32.png) [@Dominic](https://discourse.slimframework.com/u/Dominic)\
**Post date:** [August 22, 2016, 11:24am UTC](https://discourse.slimframework.com/t/slim-3-content-type-image/161/4 "2016-08-22T11:24:36Z")

</div>

hi,

I tried this but it does not work for me.  
I use Advanced REST Client in Chrome to test but it only shows a broken Image Icon.

---

<div class="post-metadata">

**Author:** ![Dominic](https://avatars.discourse-cdn.com/v4/letter/d/13edae/32.png) [@Dominic](https://discourse.slimframework.com/u/Dominic)\
**Post date:** [September 18, 2016, 3:53pm UTC](https://discourse.slimframework.com/t/slim-3-content-type-image/161/5 "2016-09-18T15:53:07Z")

</div>

Can anybody help? I still haven’t fixed this.

This is my code:

$app-\>get(’/articleImage’, function ($request, $response) {  
$image = file\_get\_contents(‘test.jpg’);  
$finfo = @finfo\_open(FILEINFO\_MIME\_TYPE);  
$type = finfo\_file($finfo, ‘test.jpg’);  
if($image === FALSE) {  
return $response-\>write(“test”);  
}

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

```

});

---

<div class="post-metadata">

**Author:** ![llvdl](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/llvdl/32/78_2.png) [@llvdl](https://discourse.slimframework.com/u/llvdl)\
**Post date:** [September 29, 2016, 9:06pm UTC](https://discourse.slimframework.com/t/slim-3-content-type-image/161/6 "2016-09-29T21:06:17Z")

</div>

Hello Dominic,

Can you elaborate on what is not working for you?

Perhaps simplifying the code will help you get back on track. For example change the code to leave out the call to `finfo_open`:

```php
$app->get('/articleImage', function ($request, $response) {
    $image = @file_get_contents('test.jpg');
    if ($image === false) {
        $response->write('Could not find test.jpg.');
        return $response->withStatus(404);
    }

    $response->write($image);
    return $response->withHeader('Content-Type', 'image/jpeg');
});

```

---

<div class="post-metadata">

**Author:** ![Dominic](https://avatars.discourse-cdn.com/v4/letter/d/13edae/32.png) [@Dominic](https://discourse.slimframework.com/u/Dominic)\
**Post date:** [September 30, 2016, 10:58pm UTC](https://discourse.slimframework.com/t/slim-3-content-type-image/161/7 "2016-09-30T22:58:54Z")

</div>

Hey llvdl,

thx for the reply. Basically I just want to get the Image but the data seams to be invalid.  
I tested your code this is what I get:

In Chrome:

 ![](https://canada1.discourse-cdn.com/flex030/uploads/slimframework/original/1X/01eac63fb1b6be8d1d5e438abc5c139bc6de7ed2.PNG)

---

<div class="post-metadata">

**Author:** ![Dominic](https://avatars.discourse-cdn.com/v4/letter/d/13edae/32.png) [@Dominic](https://discourse.slimframework.com/u/Dominic)\
**Post date:** [September 30, 2016, 10:59pm UTC](https://discourse.slimframework.com/t/slim-3-content-type-image/161/8 "2016-09-30T22:59:36Z")

</div>

in Advanced Rest Client ( [https://advancedrestclient.com/](https://advancedrestclient.com/) ):

1:

 ![](https://canada1.discourse-cdn.com/flex030/uploads/slimframework/original/1X/bd2a648f6ff27aae253c814bd78102edec1dec2b.PNG)

---

<div class="post-metadata">

**Author:** ![Dominic](https://avatars.discourse-cdn.com/v4/letter/d/13edae/32.png) [@Dominic](https://discourse.slimframework.com/u/Dominic)\
**Post date:** [September 30, 2016, 11:00pm UTC](https://discourse.slimframework.com/t/slim-3-content-type-image/161/9 "2016-09-30T23:00:19Z")

</div>

in Advanced Rest Client ( [https://advancedrestclient.com/](https://advancedrestclient.com/) ):

2:

 ![](https://canada1.discourse-cdn.com/flex030/uploads/slimframework/original/1X/df6252ca0cc698082662370dabe42c5b77d22232.PNG)

I am sry for the multiple posts but I can only supply one Image per post.

Help is very much appreciated.

---

<div class="post-metadata">

**Author:** ![llvdl](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/llvdl/32/78_2.png) [@llvdl](https://discourse.slimframework.com/u/llvdl)\
**Post date:** [October 1, 2016, 9:18am UTC](https://discourse.slimframework.com/t/slim-3-content-type-image/161/10 "2016-10-01T09:18:00Z")

</div>

Hello Dominic,

I just tested the code I provided with Chromium (Chrome for Linux) and the picture seems valid. Also, the result from the Advanced Rest Client looks in order. You could check the following:

1. Is the source image actually valid? Does it display if you open it from disk in Chrome? (probably, but it won’t hurt to check)
2. If you save the request result, is it different from the source file (same size, same content)?

You may have inadvertently added whitespace to the ouput by having spaces or enters before the `<?php` opening tag.

 ![](https://canada1.discourse-cdn.com/flex030/uploads/slimframework/original/1X/a6002e267941bb6fbf45ba81090489abaa1998d1.png)

---

<div class="post-metadata">

**Author:** ![Dominic](https://avatars.discourse-cdn.com/v4/letter/d/13edae/32.png) [@Dominic](https://discourse.slimframework.com/u/Dominic)\
**Post date:** [October 2, 2016, 7:50pm UTC](https://discourse.slimframework.com/t/slim-3-content-type-image/161/11 "2016-10-02T19:50:21Z")

</div>

Hey llvdl,

I actually had a Newline before a \<?php opening tag in an included File.  
Removing it fixed the Problem.

Thank you for your help 😄
