PHP plot (1) _ PHP Tutorial

Source: Internet
Author: User
Tags imagejpeg
PHP plot (1 ). I dare not say "speak" about the GD library here, because I only use GD once or twice, and the vast majority of functions have not been used yet. But I had to submit a draft to me with enthusiasm.

I dare not say "speak" about the GD library here, because I only use GD once or twice, and most of the functions have not
There are contacts. However, I had to write my own experiences with enthusiasm. Hope
It is enough to serve as an example.

In fact, the effect of implementing "graphs" on web pages is not necessarily not necessary to use GD. what is easier to solve is columnar
Figure -- solve the problem with HTML. For example:













/* (1 )*/





/* (2 )*/ /* (3 )*/

/* (4 )*/


Is a group of data, where the data comes from, is none
It depends on your needs. I have added comments for the two sentences in the code. now I will explain them one by one.

(1) valign = "bottom" is used to align the bottom of the cell content. Why addLi
What about it? So that the contents of this row in the table follow this one-to-one method, without having.
In order to save dozens of bytes of the original code of the HTML page of PHP execution results! Saves valuable time for viewers.

(2) note that the most important thing is here!

















We use the table height attributeImplement "column" at different heights. I am here to make it clear that the raw data has not been scaled proportionally,If your data is large or small, it is not suitable to directly assign the height attribute to the table.Scale the data according to the appropriate proportion. For example, you estimate that every number in your group of data is between 3000 and ~ Between 8000,You can reduce them by 25 times, that is, height =" " (3) mention bgcolor = "# xxxxxx" in this line, which is the color of the bar (RGB ). In fact, the true bar chart shouldEach bar uses a color. here I use this for loop to make the code as simple as possible, so there is no wayEach bar specifies a color. -- In fact, there is a solution. I just don't have to write it for this example.A color extraction function is used to confuse beginners. Therefore, you should complete that part by yourself. (4) here, real data is displayed in the same color as the column. Of course, you can also choose to place this number on the top of the barOn, may be more professional. However, I am still used to putting it below. With the help of the HTML table, we can construct various bar charts. In this example, bgcolor is used to display color blocks,In addition, you can also use background = "(Image)", the picture is with a pattern, so the column of the bar chart has a pattern.And you will display the real data in a large contrast color in the one shown in the preceding comment (3 ).
Is also very good.

The above is an effective method to avoid GD, but to do complex graphics, it is not necessary to use GD.

In sadly's PHP4 Chinese manual, there are 44 functions in the GD function library, but I have read the latest PHP4 manual,
There are more than 80 GD functions! Because the author's English is relatively poor, I can only give guesses when reading the English manual, so I am not sure
Does the new GD library support GIF again? No matter what, I think, since we are using completely free PHP, why?
"Adventure" to use copyrighted GIF? Why not use PNG for free? PNG can also be used as long as you do not need to use animations
Files as small as GIF!

Next I will talk about the commonly used GD functions with a piece of code.

Start.

Header ("Content-type: image/png ");
// This is to send an HTTP header and tell the browser: "You listen, this is an image. do not display it as text !"
// Because of my personal preferences, PNG is used. of course, you can also use Header ("Content-type: image/gif ");
// Or Header ("Content-type: image/jpeg ");
$ Im = ImageCreate (50,100 );
// Create an image. Note that the image format is not specified when the image is created.
// The ImageCreate function. two parameters are not in question. this is the width and height of the created image.
// Its return value is an int value, which is very important. you can continue to draw this image,
// Until you output this image, there is no need to use this value. We call it the ID of the image for the moment.
// Because the usage frequency is quite high, we assign it to a variable with a short name.

// Draw a line first. The linear function is as follows:
// Imageline (int im, int x1, int y1, int x2, int y2, int col );
// The first parameter im is the image ID, followed by x1, y1, x2, y2, needless to say,
// It is the coordinate of the start point (x1, y1) and the end point (x2, y2! (The coordinates in the upper left corner of the image are (0, 0 ))
// What is the last parameter? Yes color! GD requires that colors be defined for the image, and these colors are used for plotting.
// Why do we need to define colors for images? I guess it is used as a "palette" for GIF, PNG, and other images.
// This involves the knowledge of the image itself. I will not repeat it here.
// Therefore, before draw a line, we need to define the color first (really troublesome ).

// $ Col_red = ImageColorAllocate ($ im, 255,192,192 );
// The four parameters of this function, the first $ im ...... What else do I need to say? Don't talk about it next time!
// The following three parameters are the red (R), Green (G), and Blue (B) components of the color to be defined, 0 ~ In the range of 255.
// This involves the knowledge of physics and optics. Different colors of red, green, and blue light,
// It produces ever-changing colors. The color I defined above, red 255, Green 192, and blue 192.
// If there is no error, it is a bright red color. Let's draw a line later.
// Why wait for a while? Because a picture only has one color, nothing can be seen!
// Let's turn the background into a black first!
// Although not explicitly stated in the manual, I found that the color first defined will be used as the background by default.

$ Col_black = ImageColorAllocate ($ im, 0, 0 );
// Defines a color. there is no red light, green light, or blue light. it is black.
// Then define the color of the draw line:
$ Col_red = ImageColorAllocate ($ im, 255,192,192 );

// Now you can draw the red line:
Imageline ($ im, 10, 20, 45, 85, $ col_red );
// Don't worry. you can't see the image after this sentence is over.

ImagePNG ($ im );
// This sentence outputs the image, ImagePNG () outputs the png image, and ImageJPEG outputs the jpeg image,
// ImageGIF output gif image ......
// Do not forget that there is a parameter here. if it is displayed on the screen rather than saved as a file,
// This parameter is omitted-the saved file name. If you want to save it as a file,
// Write it like this: ImagePNG ($ im, "test.png ");
// If no path is specified, the file is saved in your current web directory.
// If JPEG is used, an additional parameter, which is the JPEG quality (0 ~ 100 ).
// To display on the screen, ImageJPEG ($ im, "", 80 );
// If you want to save the image, ImageJPEG ($ im, "test.jpg", 80 );
// Note: If you want to save this image as a file,
// You cannot use the Header ("Content-type: image/png"); transfer means the HTTP Header of the image,
// Once this is done, it indicates that you will output the image.

ImageDestroy ($ im );
// Destroy the images in the memory to release the memory space.
// The result is as follows: the simplest GD image is made.

// The test shows that the image file is generated in PNG format only 131 bytes,
// In JPEG format, even the worst quality (0) requires 855 bytes. The image quality is too bad to be viewed.
// The highest JPEG quality requires 2360 bytes, but the color is not as bright as PNG.
// It can be seen that PNG is much more cost-effective than JPEG for images with a small number of colors.
?>

I will try to continue writing this article as soon as possible.

Pipeline has been exposed. But I had to submit a draft to me with enthusiasm...

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.