<img src="fig1.php" border=0
align=center width=300 height=200>
The library will automatically generate the necessary headers to be sent back to the browser to correctly recognize the data stream as an image of either PNG/GIF/JPEG format. The browser can then correctly decode the image
Observere that you can't return anything else than an image from the image script. By definition each HTML page can only consist of one mime type which is determined by the sent headers.
A common mistake is to have a space in the beginning of the image script file which the HTTP server will send back to the browser. The browser now assumes that the data comming back from this script is normal ASCII. When then the image headers get send back to the browser to forwarn the browser of the forthcomming image the browser will not like that. It has already determined that the script should only send ASCII data back and will then give you a "Headers already sent error".
To include several images together with text on a page you need to have a parent page with several <IMG> which each refers to an image script.
To get access to the library you will need to include at least two
files, the base library and one or more of the plot extensions. So for
example if you want to do line plots the top of your PHP file must have
the lines:
php
include (
'jpgraph.php');
include ('jpgraph_line.php');
...
// Code that uses the jpgraph
library
...
You shopuld remember that it is also possible to pass arguments to
the image script via the normal HTTP parameters, for example
<img src="showgraph.php?a=1&b=2">
This could for example be used to control the apperance of the image or perhaps send data to the image which will be displayed. Note that this is probably not the best way to send large amount of data to plot. Instead the only practical way, for large data sizes, is to get all the data in the image script, perhaps from a DB.
echo "<img
src='myimagescript.php?dummy=".now()."'>"
When it comes to the structure of your imaging script they will
generally have the structure
// ... Include necessary headers
$graph = new Graph($width,$height, ...);
// ... code to construct the graph details
$graph->Stroke();
JpGraph is completely Object oriented so all calls will be action on specific instances of classes. One of the fundamental classes is the Graph() class which represents the entire graph.
After the creation of the Graph() object you add all your lines of code to construct the details of the graph.
As the final call you will send the generated image back to the browser with a call to the Stroke() method.
Note: This is not always true, but to keep things simple for the moment we assume this.
In addition to this standard usage pattern you can also send the
graph directly to a file, get the GD image handler for the image and
also make use of the builtin cache system. The cache system, which
lessens the burden of the PHP server, works by avoiding o run all the
code that follows the initial Graph() call by checking if the image has
already been created and in that case directly send back the previously
created (and filed) image to the browser. When using the cache system
you must specify a filename which is used to store the image in the
cache system and possibly also a timeout value to indicate how long the
image in the cache directory should be valid. For this reason you might
in the following examples, for example, see the code
$graph = new Graph(300,200,"auto");
in the start of all the examples. The two first parameters specify the width and height of the graph and the third parameter the name of the image file in the cache directory. The special name 'auto' indicates that the image file will begiven the same name as the image script but with the extension changed to indicate the graphic format used, i.e '.jpg', '.png' and so on.
DEFINE("DEFAULT_GFORMAT","auto");
$graph->img->SetImgFormat("jpeg")
$graph->Stroke("/usr/home/peter/images/result2002.png");
Please note that the user running as Apache/PHP must have write access to the specified directory.
There are also two predefined filenames which have special meaning.
$handle = $graph->Stroke(_IMG_HANDLER);