Script output – How to deal with it

There is basically two way to use the output stream of the pChart library.

Rendering to the web browser

The most common way to use this library. Your script are processing real time or archived data and then generate a picture that is sent directly to the user browser. Server side, you‘ll call the stroke() function, client side, you‘ll invoke the script from an <IMG> HTML tag. The whole stuff would be like :
Server Side (mypic.php)

1./* Render the picture (choose the best way) */
2.$myPicture->stroke;

Client side (mypage.html)

1.<IMG SRC=‘mypic.php‘>

Calling the Stroke() method in your script will automatically send the ‘Content-type: image/png‘ header to the user web browser and the raw picture in the HTTP GET data field.

Rendering to a flat file

This option is often used by scheduled tasks and automatic script that are run on a regular interval and generate a picture that will be access like a standard file from the web server. Those pictures may also be used inside complex documents (PDF, bigger pictures). To render a picture to a file, just invoke the render(“myfile.png”) function.
Server Side (mypic.php)

1./* Render the picture (choose the best way) */
2.$myPicture->render("mypic.png");

Client side (mypage.html)

1.<IMG SRC=‘mypic.png‘>

..with this way, the user browser will NOT trigger the picture generation. the mypic.php should be called by another source.

Automatic method choosing

You may have noticed that the examples shipped with the pChart library can either be run from a web browser or the command line. This behavior is possible because of the autoOutput(“myfile.png”) method. If this method is called from a CLI session then the picture will be rendered to a file. If this method is called from any other type of session, the picture will be sent to the user browser.
Server Side (mypic.php)

1./* Render the picture (choose the best way) */
2.$myPicture->autoOutput("mypic.png");

Client side (mypage.html)

1.<IMG SRC=‘mypic.php‘>

..if mypic.php is called from the IMG tag, the picture will be sent to the browser. If it is called from a CLI interface, a file named mypic.png will be created.

Comments are closed.

Post Navigation