韌館-LearnHouse

[PHP]在HTML中顯示JpGraph

以下是Document文件說的以圖片的html tag <IMG>方式顯示

4.2 How to generate images with PHP

As a general rule each PHP script which generates an image must be specified in a separate file which is then called in an HTML <IMG> tag. For example, the following HTML excerpt includes the image generated by the PHP script in "fig1.php".

 <img src="fig1.php"  border=0 align=center width =300 height=200>

Strictly speaking the "align", "width" and "height" are not necessary but helps the browser position the image correctly before the image has been fully sent back to the browser.

The library will automatically generate the necessary headers to be sent back to the browser so that it correctly recognize the data stream received as an image of either PNG/GIF/JPEG format. The browser can then correctly decode the image

Observe that you can't return anything else than an image from the image script. By definition each HTML page (or more correctly each HTTP stream) can only consist of one mime type which is determined by the header for that particular stream.

A common mistake is to have a space in the beginning of the image script which the HTTP server will send back to the browser. The browser now assumes that the data coming back from this script is text since it hasn't received an explicit header. When then the image headers get sent back to the browser to forewarn the browser of the forthcoming image the browser will not like that as it has already assumed the data stream was a text stream. The browser will then give the infamous "Headers already sent error".

To include several images together with text on a page you need to have a parent page with several <IMG> tags which each refers to an image script (or the same image script with GET/POST data).

按照doc的作法,每要在html繪製一張圖表,就用一個<IMG>標籤指定該程式

但我試了幾個一樣也無法顯示,因此就修改了一下,改讓他先輸出png的圖片檔,再用<IMG>

首先先修改之前編寫的PHP script:

將原本的$graph->StrokeCSIM();  //從HTML中直接輸出圖片

改成
$graph_name = "路徑/檔名.png";
$graph->Stroke($graph_name);

再到要顯示的html頁面中加入<IMG>:

再加入<IMG>前要先把支前編寫的PHP script先include進來

<img src="images/test.png" width="600" height="850" /> 

寬度與高度要跟你在繪製圖表的PHP script指定的大小一樣 

Tips: 在開始html之前要先把寫好的繪圖的PHP script先include進來,而且最好用require_once("XXXX.php")的方式,避免重複include

2008年2 月 posted by admin in 程式&軟體 and have No Comments

Place your comment

Please fill your data and comment below.
名稱:
信箱:
網站:
您的評論: