To send 'Content-Length' header (like in static pictures case) i use "Output handler" like this...
<?php
// Output handler
function output_handler($img) {
header('Content-type: image/png');
header('Content-Length: ' . strlen($img));
return $img;
}
// Image output
ob_start("output_handler");
imagepng($im);
ob_end_flush();
?>