2016-12-21 98 views
3

我有這個腳本輸出圖像到命令行,如果我將它重定向到file.png我可以正確看到圖形,但如果我嘗試從瀏覽器做同樣的事情,不要在飛行中看到它。PHP輸出到img標籤

我試圖將腳本分成兩部分,但不起作用。

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 data]"

謝謝!

+0

目前尚不清楚問題出在哪裏。檢查服務器在客戶端的響應:標題,內容。 – AlexM

+0

你好,客戶端控制檯(谷歌瀏覽器)返回'資源解釋爲文檔,但與MIME類型圖像/ png傳遞:「https://test.xxx.com/graph.php」.' –

+0

你能提供代碼,你已經用來顯示圖像(html) –

回答

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對於類似您這樣的問題。

+0

很棒!第一個工作不起作用,第二個工作像魅力,非常感謝你,我正在尋找tihs解決方案近一個星期。 PD:我必須等20個小時才能完成50+,我會這麼做 –

+0

也許第一個不能工作,因爲它不能在腳本執行的當前目錄下讀寫,但第二個使用內部保存你不必直接處理文件系統。很高興我的回答很有幫助。我可以等待賞金,沒關係:) –