Return image as output again


#1

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


#2

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

#3

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