Realtime image from rrd file in PHP

廉价感情. 提交于 2019-12-24 07:45:52

问题


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


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/20406429/realtime-image-from-rrd-file-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!