我有这个脚本可以将图像输出到命令行,如果我将它重定向到 file.png,我可以正确地看到图形,但是如果我尝试从浏览器中执行相同的操作,我就无法在飞。
我也尝试将脚本分成两部分,但没有用。
1 -> 生成图表 2 -> 从这个脚本调用第一个脚本并将所有脚本保存在一个变量中。
脚本:
<?php
header("Content-Type: image/png");
header("Content-Transfer-Encoding: binary");
ob_flush();
require_once ('/opt/rMON/config.php');
//if(isset($_GET['id'])){
// $id = trim($_GET['id']);
//} else {
// die("El id?");
//}
//DEBUG ID
$id=1;
$result = ***MYSQL QUERY***
$ip = long2ip($result['ip']);
$interface = $result['interface'];
$counter = $result['counter'];
$unix_name = $result['unix_name'];
$community = $result['community'];
$version = $result['version'];
$port = $result['port'];
$rrd_file = __RRD_ROOT__.$unix_name.".rrd";
$graph_name = $result['name'];
$host_ip = long2ip($result['ip']);
$iface_name = $result['iface_name'];
$fecha = date("y-m-d h:i:s");
$start = "3600";
$tiempo = "1 Hora";
create_graph($start, $graph_name, $fecha, $rrd_file, $input, $output, $host_ip, $iface_name, $tiempo);
function create_graph($start, $graph_name, $fecha, $rrd_file, $input, $output, $host_ip, $iface_name, $tiempo){
$opts = array (
"--imgformat=PNG",
"--slope-mode",
"--title=$graph_name ($host_ip) - $iface_name - $tiempo",
"--rigid",
"--base=1000",
"--height=120",
"--width=500",
"--alt-autoscale-max",
"--lower-limit=0",
"--font=TITLE:10:",
"--font=AXIS:8:",
"--font=LEGEND:8:",
"--font=UNIT:8:30:",
"--watermark=$fecha - Radu Radu",
"--start=-$start",
"--end=now",
"DEF:a=$rrd_file:$input:AVERAGE",
"DEF:b=$rrd_file:$output:AVERAGE",
"CDEF:cdefa=a,8,*",
"CDEF:cdefe=b,8,*",
"AREA:cdefa#00CF00FF:Entrante\t",
"GPRINT:cdefa:LAST:Actual\:%8.2lf %s",
"GPRINT:cdefa:AVERAGE:Promedio\:%8.2lf %s",
"GPRINT:cdefa:MAX:Máximo\:%8.2lf %s",
"LINE1:cdefe#002A97FF:Saliente\t",
"GPRINT:cdefe:LAST:Actual\:%8.2lf %s",
"GPRINT:cdefe:AVERAGE:Promedio\:%8.2lf %s",
"GPRINT:cdefe:MAX:Máximo\:%8.2lf %s"
);
$ret = rrd_graph("-", $opts);
if(!$ret){
echo "ERROR en el objeto: $graph_name ".rrd_error()."\n";
}
}
?>
我也尝试输出到 php://output,但没有成功。
正如我在日志中看到的,输出将转到 apache 服务器日志。
"dic 21 10:58:00 xxx.xxx.com httpd[27941]: [305B blob 数据]"
谢谢!!
最佳答案
你做错了。 rrd_graph 不接受 - 用于 $filename 并且它返回一个包含生成图像信息的数组;它不输出或刷新任何图像数据。 - $filename 参数用于 RRDGraph 类。为了获得图像数据,您需要打开 rrd_graph 生成的文件,读取其数据并输出数据,或者使用 RRDGraph 返回的数组 [ 'image'] 获取二进制图像数据的键。
使用rrd_graph函数
function create_graph($start, $graph_name, $fecha, $rrd_file, $input, $output, $host_ip, $iface_name, $tiempo) {
$opts = array (
"--imgformat=PNG",
"--slope-mode",
"--title=$graph_name ($host_ip) - $iface_name - $tiempo",
"--rigid",
"--base=1000",
"--height=120",
"--width=500",
"--alt-autoscale-max",
"--lower-limit=0",
"--font=TITLE:10:",
"--font=AXIS:8:",
"--font=LEGEND:8:",
"--font=UNIT:8:30:",
"--watermark=$fecha - Radu Radu",
"--start=-$start",
"--end=now",
"DEF:a=$rrd_file:$input:AVERAGE",
"DEF:b=$rrd_file:$output:AVERAGE",
"CDEF:cdefa=a,8,*",
"CDEF:cdefe=b,8,*",
"AREA:cdefa#00CF00FF:Entrante\t",
"GPRINT:cdefa:LAST:Actual\:%8.2lf %s",
"GPRINT:cdefa:AVERAGE:Promedio\:%8.2lf %s",
"GPRINT:cdefa:MAX:Máximo\:%8.2lf %s",
"LINE1:cdefe#002A97FF:Saliente\t",
"GPRINT:cdefe:LAST:Actual\:%8.2lf %s",
"GPRINT:cdefe:AVERAGE:Promedio\:%8.2lf %s",
"GPRINT:cdefe:MAX:Máximo\:%8.2lf %s"
);
$fileName = "rrd.png";
$ret = rrd_graph($fileName, $opts);
if(!$ret){
echo "ERROR en el objeto: $graph_name ".rrd_error()."\n";
}
else {
header("Content-Type: image/png");
header("Content-Length: " . filesize($fileName));
$fp = fopen($fileName, 'rb');
if($fp) {
fpassthru($fp);
fclose($fp);
exit();
}
}
}
使用 RRDGraph 类
function create_graph($start, $graph_name, $fecha, $rrd_file, $input, $output, $host_ip, $iface_name, $tiempo){
$opts = array (
"--imgformat=PNG",
"--slope-mode",
"--title=$graph_name ($host_ip) - $iface_name - $tiempo",
"--rigid",
"--base=1000",
"--height=120",
"--width=500",
"--alt-autoscale-max",
"--lower-limit=0",
"--font=TITLE:10:",
"--font=AXIS:8:",
"--font=LEGEND:8:",
"--font=UNIT:8:30:",
"--watermark=$fecha - Radu Radu",
"--start=-$start",
"--end=now",
"DEF:a=$rrd_file:$input:AVERAGE",
"DEF:b=$rrd_file:$output:AVERAGE",
"CDEF:cdefa=a,8,*",
"CDEF:cdefe=b,8,*",
"AREA:cdefa#00CF00FF:Entrante\t",
"GPRINT:cdefa:LAST:Actual\:%8.2lf %s",
"GPRINT:cdefa:AVERAGE:Promedio\:%8.2lf %s",
"GPRINT:cdefa:MAX:Máximo\:%8.2lf %s",
"LINE1:cdefe#002A97FF:Saliente\t",
"GPRINT:cdefe:LAST:Actual\:%8.2lf %s",
"GPRINT:cdefe:AVERAGE:Promedio\:%8.2lf %s",
"GPRINT:cdefe:MAX:Máximo\:%8.2lf %s"
);
$graphObj = new RRDGraph('-');
$graphObj->setOptions($opts);
$ret = $graphObj->saveVerbose();
if(!$ret){
echo "ERROR en el objeto: $graph_name ".rrd_error()."\n";
}
else {
header("Content-type: image/png");
echo $res['image'];
exit();
}
}
您可以阅读问答 here对于与您类似的问题。
关于PHP 输出到 img 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41260351/
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我试图使用yard记录一些Ruby代码,尽管我所做的正是所描述的here或here#@param[Integer]thenumberoftrials(>=0)#@param[Float]successprobabilityineachtrialdefinitialize(n,p)#initialize...end虽然我仍然得到这个奇怪的错误@paramtaghasunknownparametername:the@paramtaghasunknownparametername:success然后生成的html看起来很奇怪。我称yard为:$yarddoc-mmarkdown我做错了什么?
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
这是一道面试题,我没有答对,但还是很好奇怎么解。你有N个人的大家庭,分别是1,2,3,...,N岁。你想给你的大家庭拍张照片。所有的家庭成员都排成一排。“我是家里的friend,建议家庭成员安排如下:”1岁的家庭成员坐在这一排的最左边。每两个坐在一起的家庭成员的年龄相差不得超过2岁。输入:整数N,1≤N≤55。输出:摄影师可以拍摄的照片数量。示例->输入:4,输出:4符合条件的数组:[1,2,3,4][1,2,4,3][1,3,2,4][1,3,4,2]另一个例子:输入:5输出:6符合条件的数组:[1,2,3,4,5][1,2,3,5,4][1,2,4,3,5][1,2,4,5,3][
我想使用spawn(针对多个并发子进程)在Ruby中执行一个外部进程,并将标准输出或标准错误收集到一个字符串中,其方式类似于使用Python的子进程Popen.communicate()可以完成的操作。我尝试将:out/:err重定向到一个新的StringIO对象,但这会生成一个ArgumentError,并且临时重新定义$stdxxx会混淆子进程的输出。 最佳答案 如果你不喜欢popen,这是我的方法:r,w=IO.pipepid=Process.spawn(command,:out=>w,:err=>[:child,:out])
我有一个div,它根据表单是否正确提交而改变。我想知道是否可以检查类的特定元素?开始元素看起来像这样。如果输入不正确,添加错误类。 最佳答案 试试这个:browser.div(:id=>"myerrortest").class_name更多信息:http://watir.github.com/watir-webdriver/doc/Watir/HTMLElement.html#class_name-instance_method另一种选择是只查看具有您期望的类的div是否存在browser.div((:id=>"myerrortes
我想知道Ruby用来在命令行打印这些东西的输出流:irb(main):001:0>a="test"=>"test"irb(main):002:0>putsatest=>nilirb(main):003:0>a=>"test"$stdout是否用于irb(main):002:0>和irb(main):003:0>?而且,在这两次调用之间,$stdout的值是否有任何变化?另外,有人能告诉我打印/写入这些内容的Ruby源代码吗? 最佳答案 是的。而且很容易向自己测试/证明。在命令行试试这个:ruby-e'puts"foo"'>test.
我在使用自定义RailsFormBuilder时遇到了问题,从昨天晚上开始我就发疯了。基本上我想对我的构建器方法之一有一个可选block,以便我可以在我的主要content_tag中显示其他内容。:defform_field(method,&block)content_tag(:div,class:'field')doconcatlabel(method,"Label#{method}")concattext_field(method)capture(&block)ifblock_given?endend当我在我的一个Slim模板中调用该方法时,如下所示:=f.form_field:e
考虑一下:现在这些情况:#output:http://domain.com/?foo=1&bar=2#output:http://domain.com/?foo=1&bar=2#output:http://domain.com/?foo=1&bar=2#output:http://domain.com/?foo=1&bar=2我需要用其他字符串输出URL。我如何保证&符号不会被转义?由于我无法控制的原因,我无法发送&。求助!把我的头发拉到这里:\编辑:为了澄清,我实际上有一个像这样的数组:@images=[{:id=>"fooid",:url=>"http://
我有一个像这样的ruby类:require'logger'classTdefdo_somethinglog=Logger.new(STDERR)log.info("Hereisaninfomessage")endend测试脚本行如下:#!/usr/bin/envrubygem"minitest"require'minitest/autorun'require_relative't'classTestMailProcessorClasses当我运行这个测试时,out和err都是空字符串。我看到消息打印在stderr上(在终端上)。有没有办法让Logger和capture_io一起玩得