gpt4 book ai didi

PHP 输出到 img 标签

转载 作者:可可西里 更新时间:2023-11-01 00:40:32 24 4
gpt4 key购买 nike

我有这个脚本可以将图像输出到命令行,如果我将它重定向到 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com