Slim 3 Content Type Image


#1

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


Output Image from route
#2

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

#3

Thanks @llvdl your code is work.


#4

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.


#5

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

});


#6

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

#7

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:


#8

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

1:


#9

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.


#10

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.


#11

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: