szpal szpal - 9 months ago 40
PHP Question

Realtime image from rrd file in PHP

I would like to generate a "realtime" image from a rrd file in php script, but no success.
This is the php script (/var/www/rrd_image.php), which should generate the picture:

header("Content-type: image/png");

$options = array(
"--start", "-1d",
"LINE:tsnr#00FF00:US SNR",
"GPRINT:tsnr:MIN:Min\: %3.1lf dB",
"GPRINT:tsnr:MAX:Max\: %3.1lf dB",
"GPRINT:tsnr:LAST:Pill\: %3.1lf dB",

rrd_graph("-", $options);

So I'm calling it like this:

<img src="rrd_image.php" />

But the picture is not completed, in the browser i see, that it is 0 bytes, and there is no error in apache log.
(And when i run a rrd_image.php from console, then it works, the "image" goes to the standard output.)


You do this wrong, because rrd_graph() returns array not image. You should change this to look it i.e. like this:

$fileName = "rrd.png";
rrd_graph($fileName, $options);

header("Content-Type: image/png");
header("Content-Length: " . filesize($name));

$fp = fopen($name, 'rb');
if( $fp ) {


Please always read the docs first:

PS: Unless you know you need it, never use ?> - it saves you from accidentally outputing something back to i.e. browser (like whitespaces or LFs after the ?>)