Return image as output again

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

Hello @aghezzi

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

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

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',

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

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

Selection_179