1

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:

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

  $options = array(
    "--start", "-1d",
    "--title=xxx",
    "--lower-limit=0",
    "--width=450",
    "--height=120",
    "DEF:snr=/var/www/rrd/cm_100.rrd:snr:LAST",
    "CDEF:tsnr=snr,10,/",
    "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.)

PhearOfRayne
  • 4,990
  • 3
  • 31
  • 44
szpal
  • 647
  • 9
  • 27
  • Remove the `header()` and see whether you get an error message – Pekka Dec 05 '13 at 17:18
  • Does your webserver user (i. e. apache) have read access to the rrd file? – l-x Dec 05 '13 at 17:19
  • When i remove a header(), there is no error message.. Yes apache have read the rrd file, but strange: when i delete this rrd file, there is also no error in the apache error.log – szpal Dec 05 '13 at 17:46
  • Seems you have a php file in your tag, not sure if this is supposed to work... – user2196728 Dec 05 '13 at 17:59
  • http://php.net/manual/en/function.rrd-graph.php tells us, and you, that you're not using `rrd_graph` correctly. Always verify you're using functions correctly. – Mike 'Pomax' Kamermans Dec 05 '13 at 18:26

2 Answers2

4

The possibility of using '-' as the filename exists in RRDGraph class:

<?php
  $options = array(
    "--start", "-1d",
    "--title=xxx",
    "--lower-limit=0",
    "--width=450",
    "--height=120",
    "DEF:snr=/var/www/rrd/cm_100.rrd:snr:LAST",
    "CDEF:tsnr=snr,10,/",
    "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",
  );

  $graphObj = new RRDGraph('-');
  $graphObj->setOptions($options);
  $res = $graphObj->saveVerbose();

  header("Content-type: image/png");
  echo $res['image'];

Source: http://php.net/manual/en/rrdgraph.saveverbose.php

Chupaka
  • 190
  • 6
2

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 ) {
  fpassthru($fp);
  fclose($fp);
}

exit();

Please always read the docs first: http://php.net/manual/en/function.rrd-graph.php

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

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • thank you, but how can i do this task without "temporary file" (rrd.png)? Is it possible generate image _only_ to the memory, not into regular file? – szpal Dec 05 '13 at 19:15
  • No luck at the moment, as `rrd_graph()` output cannot be redirected. But you can reduce the impact by caching output file instead of creating in on each request (so if your data "resolution" is i.e. 5 minutes, you can easily reuse old image if it is not older than 5 minutes. – Marcin Orlowski Dec 05 '13 at 20:37