Jpgraph Chinese User Manual: Introduction to the concept of using jpgraph to generate images

Source: Internet
Author: User
Tags what header
After introducing the phpjpgraph installation and configuration tutorial and the first jpgraph instance tutorial, we need to sort out our ideas to better use jpgraph. What steps do we usually need to use phpjpgraph to compile and generate image code? Can an image be generated using the command line? How does an HTML tag call an image generation script? After introducing the php jpgraph installation and configuration tutorial and the first jpgraph instance tutorial, we need to organize our ideas to better use jpgraph, what are the common steps to use php jpgraph to compile the image generation code? Can an image be generated using the command line? How does an HTML tag call an image generation script? How to Write images to files? These questions need to be answered in this tutorial. Even if you are familiar with PHP, it is strongly recommended that you visit this tutorial quickly to learn about related concepts.

  Use command line to generate dynamic images

The jpgraph class library can generate images directly using the PHP command line. It works in a similar way as the browser generates images. The generated image data is binary.

This method is an effective way to make the website efficiently and automatically create images at a fixed interval. This prevents visitors from generating images each time they access the website to increase the server load.

Make sure that your PHP command line version is cli. PHP using CGI/ISAPI cannot work because it generates HTTP headers.

  Jpgraph usage tips: If the CGI version is used, you can add the-q option to suppress the generation of HTTP headers. Recommended: PHP command line parameter usage and instructions tutorial

You can use

1
Php -- version

Command to check whether your PHP command line version is cli. Then

1
Php myimage. php> image.png

Command to generate an image. the file extension must match the format of the generated image.

  How to use the jpgraph class library to generate images

Common steps for creating and using images on a WEB page are as follows:

1. create a script that includes the following content: get data and specify how the image should be built, how much the image size, what color and font to use, and expand other things on the image.

2. add one or more img labels on the HTML page where the image should be displayed to connect to the PHP image program. Of course, you can also directly call these image programs in the browser to display them. Using the img label, you can include any number of images on a Web page.

It should be noted that parameters can be passed to the image program through the normal http get/POST method. For example

1

This code may be used to control the image appearance or transmit data to a place where the image should be displayed. Note that this may not be the best way to send data for drawing. For images with a large amount of data requirements, you can obtain all the data required by the image script from the database.

  Detailed steps for using jpgraph to create a graph

When you are going to use the php jpgraph class library to create your PHP graphics script, there are generally the following steps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Contains necessary class library files
Require_once 'jpgraph. php ';
Require_once '....';
 
// Create a Graph instance
$ Graph = new Graph ($ width, $ height ,...);
 
// Set the scale type
$ Graph-> SetScale ('...');
 
// Create image details and graphic model objects
 
// Add one or more image model objects to the image
$ Graph-> Add (..);
 
// Send the image to the browser for display
$ Graph-> Stroke ();
?>

Jpgraph is fully object-oriented, so all calls are for specific instances of the class. The basic class is Graph, which represents the entire chart.

After the Graph object is created, all code is added to create the details of the Graph. The final method Graph: Stroke () called in the script is used to send the constructed Graph to the browser. If the chart supports CSIM, you need to change this method to Graph: StrokeCSIM ()

In addition to these standard usage, you can also choose

1. directly send the chart to the file. This is passed to the called Stroke () method by specifying a file name as a parameter.

2. access the GD image processing program

3. Use the built-in cache system to send the previously generated images. Note: the cache system is disabled by default and you need to make the appropriate settings in the jpg-config.inc to enable the cache system.

4. use the MGraph () class to combine several images in the same image.

  Jpgraph image compression format selection

By default, the jpgraph class library automatically selects the image format in the order of PNG, JPEG, and GIF. The availability depends on the system where the jpgraph class library is installed.

There are two ways to change the image format

1. change the default image format defined in the jpgraph profile jpg-config.inc.php, that is

1
DEFINE ('default _ gformat', 'auto ')

If you want all your images to be produced in JPG format, you need to change them

1
DEFINE ('default _ gformat', 'jpg ')

2. dynamically select the format to be compressed in the script, mainly by calling the Image: SetImgFormat () method. for example, you want to use JPEG format to send images to the browser.

1
2
3
$ Graph-> img-> SetImgFormat ('jpeg ')
?>

The last row of almost all Chart programs is $ graph-> Stroke ();

This line begins the actual chart creation. When all methods are called to this stage, you have set the class library scenario and specified all necessary parameters (similar to the photoshop scenario ). When you call the Stroke () method, you start to build an image. If no error is found during image generation, follow these steps in principle:

1. start to create images in the memory. This creates a variety of specified graphics modes by analyzing specified parameters and providing data.

2. check what header is to be sent, which image compression format is used for the ratio, and then send the header to the client.

3. send the actual image data to the client

  Description of writing images directly to files

In addition to sending the image file to the browser, you can also directly write the image to the named file. The file name must be passed to the Graph: Stroke () method as a parameter, that is

1
2
3
$ Graph-> Stroke ('/tmp/myimage.png ');
?>

There are three points to note

1. the PHP program must have the write permission for the directory where you are trying to write files. If you run PHP through the browser. This means that the HTTP server must have the write permission in this directory.

2. the file extension must match the specified image compression format.

3. if an image is directly transmitted to a file but not to the browser, the script can return normal text.

  Description of an instance that writes an image to a file and returns it to the browser

In this jpgraph instance, we should normally use the Graph: StrokeStore ($ aFileName) method in jpgraph 2.5. If you are using a previous version and cannot upgrade for various reasons, you can use the following trick to implement it: Use the _ IMG_HANDLER option to force Graph: Stroke () method to return the image handle (handler) and stop it. Then, manually send the image to the selected file, and then send the file stream to the browser using the internal method in the class library. That is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Contains necessary class library files
 
$ Graph = new Graph (400,300 );
 
// Compile the image generation code
 
// Use the _ IMG_HANDLER parameter to prevent images from being sent to the browser.
$ GdImgHandler = $ graph-> Stroke (_ IMG_HANDLER );
 
// The default image file format is PNG, and the image is transmitted to the specified file
$ FileName = "/tmp/imagefile.png ";
$ Graph-> img-> Stream ($ fileName );
 
// Send the image to the browser
$ Graph-> img-> Headers ();
$ Graph-> img-> Stream ();
?>

  Special description of the Stroke function

In addition to file name parameters, there are two predefined parameters that can be used in this function.

  _ IMG_AUTO: A file with the same name as the script will be created under the script Directory. the extension is the configured Image extension.

  _ IMG_HANDLER: Indicates that the file name is not created or returned to the browser. Instead, it only returns the GD handle from the Stroke () method. It is useful if you want to operate images in areas that do not support the jpgraph class library later. For example, include an image in a dynamically generated PDF file.

  How to force the browser to update your image

Some browsers may not return a request to the HTTP server unless the user refreshes. Because the time stamp of the script file does not change, the user may always see the old data.

1

This can also happen because of browser cache problems. you can also use this method to force the browser to update images.

  How to print the generated image

Some browsers, the most notable IE browsers, have a dynamic image printing problem. This is because the IE designer assumes that all images are images of traditional static image files and will not be dynamically generated, which may affect printing.

1. when IE is preparing to print, the page is often Retrieved again. This means that a new image will be generated. if the data changes rapidly, users may think that the image is different.

2. some earlier versions of Internet Explorer do not print dynamic images at all. the only way is to ensure static images are used.

Note the last question. Generally, most browsers support right-clicking and downloading images to your local device. However, when using dynamic images, some older versions of IE may be very messy. for example, if you try to download a PNG image and save it as a BMP file.

Of course, the new version of Internet Explorer seems to be able to process dynamic images.

OK, the following describes how to use jpgraph to generate an image and how to use command line to generate an image, how to use jpgraph to write the generated image to a file, and how to use HTML tags to call the jpgraph script.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.