Slim 3 Content Type Image

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

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);
});
1 Like

Thanks @llvdl your code is work.

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.

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

});

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:

$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');
});

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:

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

1:

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

2:

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

Help is very much appreciated.

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.

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 :smile:

1 Like