Error responding with stream

Today I came across an error that was not happening. When requesting a URI that returns an image via stream, the file comes “broken”.

//get photo
$this->get('/foto/{id}/{width}', function ($request, $response, $arqs) {
    $id = $arqs["id"];
    $width = $arqs["width"];
    $Usuario = new UsuarioController;
    $img = $Usuario->foto($id, $width);
    if (file_exists(($img))) {
        if ($fh = fopen($img, 'r+')) {
            $stream = new \Slim\Http\Stream($fh);
            //$stream = fpassthru($fh);
            return $response->withBody($stream)
                ->withHeader("file", $img)
                ->withHeader("Content-Type", mime_content_type($img))
                ->withHeader("Content-Length", filesize($img));

is doing all the checks. and if the file exists then return. but


I’m sure that this response header does not work: ->withHeader("file", $img)
Try to remove this header “file”.

I guess that this code will not work: filesize($img)
Can you please show us the response header(s), such as Content-Length?