从PHP RRD文件实时图像(Realtime image from rrd file in PHP

2019-10-19 01:58发布

我想在PHP脚本一个RRD文件,但没有成功生成一个“实时”的形象。 这是PHP脚本(/var/www/rrd_image.php),这应该产生图像:

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

所以我称它是这样的:

<img src="rrd_image.php" />

但是画面没有完成,在浏览器中我看到,它是0字节,并且在Apache日志中没有错误。 (当我运行从控制台rrd_image.php,那么它的工作原理,在“形象”进入到标准输出。)

Answer 1:

你这样做不对,因为rrd_graph()返回array没有图像。 你应该改变这即,查找这样的:

$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();

请总是先阅读文档: http://php.net/manual/en/function.rrd-graph.php

PS:除非你知道你需要它,从来没有使用?> -它可以节省您意外outputing东西回到IE浏览器(如空格或LF之后的S ?>



Answer 2:

“ - ”使用的可能性作为文件名中RRDGraph类存在:

<?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'];

来源: http://php.net/manual/en/rrdgraph.saveverbose.php



文章来源: Realtime image from rrd file in PHP
标签: php rrdtool