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
llvdl
April 28, 2016, 8:39pm
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);
});
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.
Dominic
September 18, 2016, 3:53pm
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);
});
llvdl
September 29, 2016, 9:06pm
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');
});
Dominic
September 30, 2016, 10:58pm
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:
Dominic
September 30, 2016, 10:59pm
8
in Advanced Rest Client ( https://advancedrestclient.com/ ):
1:
Dominic
September 30, 2016, 11:00pm
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.
llvdl
October 1, 2016, 9:18am
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:
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)
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
1 Like