Output Image from route

So, I am having a similar issue as here and here.

But in fact, I am using PHP to generate dynamic images and I am capturing the Base64 code from OB as described in this stackoverflow

My route code is:

$app->get('/image/{name}', function ($request, $response, $args) {
  require __DIR__ . '/../platform/image.class.php';
  $name = $request->getAttribute('name');
  $ensino = [
    "foo" => "bar",
    "bar" => "foo",
  ];
  $carterinha = new Carterinha();
  $dados = base64_decode($carterinha->construir($name, $ensino, "15/09/2000", "123456"));
  $body = $response->getBody();
  $body->write($dados);
  return $response->withHeader('Content-Type', 'image/png');
});

But its outputing a empty blank square, none of the answers in the related questions worked. What am I doing wrong?

EDIT: Plain PHP is working tho

<?php
require __DIR__ . '/../platform/image.class.php';
$ensino = [
  "foo" => "bar",
  "bar" => "foo",
];
$nome = "teste";
$carterinha = new Carterinha();
header('Content-type: image/png');
$data = $carterinha->construir($nome, $ensino, "15/09/2000", "123456");
echo base64_decode($data);
?>

Looks like you got it sorted out over in Slack. That type of thing has tripped me up a few times before as well. I recall once a combination of settings resulted in my content being gzipped twice. Oops.