PHP输出到img标签

时间:2016-12-21 10:17:00

标签: php apache rrdtool

我有这个脚本将图像输出到命令行,如果我将其重定向到file.png我可以正确地看到图形,但是如果我尝试从浏览器中做同样的事情我就看不到了飞。

我试图将脚本分成两部分,但是不起作用。

1 - >生成图表 2 - >从这一个调用第一个脚本并将所有脚本保存在变量中。

剧本:

func setChart(xValues: [String], yValuesLineChart: [Double], yValuesBarChart: [Double] , combinedChartView:CombinedChartView) {
    combinedChartView.noDataText = "Please provide data for the chart."

    var yVals1 : [ChartDataEntry] = [ChartDataEntry]()
    var yVals2 : [BarChartDataEntry] = [BarChartDataEntry]()

    for i in 0..<xValues.count {

        yVals1.append(ChartDataEntry(value: yValuesLineChart[i], xIndex: i))
        yVals2.append(BarChartDataEntry(value: yValuesBarChart[i] , xIndex: i))

    }


    let lineChartSet = LineChartDataSet(yVals: yVals1, label: "Line Data")
    lineChartSet.colors = [UIColor(red: 168.0/255.0, green: 164.0/255.0, blue: 164.0/255.0, alpha: 1.0)]

    // lineChartSet.mode = .CubicBezier
    lineChartSet.lineWidth = 5.0
    lineChartSet.drawCirclesEnabled = false
    //lineChartSet.drawFilledEnabled = true
    /*lineChartSet.colors = [UIColor(red: 0, green: 0, blue: 1, alpha: 0.0)]
     lineChartSet.fillColor = UIColor(red: 1, green: 0, blue: 0, alpha: 1.0)
     lineChartSet.fillAlpha = 0.8*/



    let barChartSet: BarChartDataSet = BarChartDataSet(yVals: yVals2, label: "Bar Data")
    // barChartSet.colors = ChartColorTemplates.colorful()
    barChartSet.barSpace = 0.65

    barChartSet.barShadowColor = UIColor(red: 1, green: 0, blue: 0, alpha: 0)// If alpha is > 0, then grey bars will appear.



    let data: CombinedChartData = CombinedChartData(xVals: xValues)
    data.barData = BarChartData(xVals: xValues, dataSets: [barChartSet])
    data.lineData = LineChartData(xVals: xValues, dataSets: [lineChartSet])
    //barChartView.leftAxis.startAtZeroEnabled = true
    combinedChartView.leftAxis.axisMinValue = 0

    combinedChartView.data = data

    combinedChartView.leftAxis.drawLabelsEnabled = false
    combinedChartView.rightAxis.drawLabelsEnabled = false
    //barChartView.leftAxis.startAtZeroEnabled = true
    combinedChartView.leftAxis.axisMinValue = 0
    combinedChartView.descriptionText = ""
    combinedChartView.legend.enabled = false
    combinedChartView.backgroundColor = UIColor.clearColor()
    //  combinedChartView.barSpace = 0.65


    combinedChartView.leftAxis.gridColor = UIColor(red: 179.0/255.0, green: 179.0/255.0, blue: 179.0/255.0, alpha: 0.2)
    combinedChartView.rightAxis.gridColor = UIColor(red: 179.0/255.0, green: 179.0/255.0, blue: 179.0/255.0, alpha: 0.2)

}

我试图输出到php://输出也没有运气。

正如我在日志中看到的那样,输出将转到apache服务器日志。

<?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"; } } ?>

谢谢!

1 个答案:

答案 0 :(得分:5)

你做错了。 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