首页 / PHP / PHP输出到img标签
PHP输出到img标签
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP输出到img标签,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4789字,纯文字阅读大概需要7分钟。
内容图文
![PHP输出到img标签](/upload/InfoBanner/zyjiaocheng/727/9b81516ebe8f4b52872d4d05393575e7.jpg)
我有这个脚本将图像输出到命令行,如果我将其重定向到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://输出也没有运气.
正如我在日志中看到的那样,输出将转到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标签全部内容,希望文章能够帮你解决PHP输出到img标签所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。