Talking about PHP drawing (i)

Source: Internet
Author: User
Tags define array continue manual functions header html page imagejpeg
Really dare not say is here "speak" GD library, because I use GD also only one or two times, most of the function has not
have access to. But the three bamboo small diao enthusiastically to me manuscripts, I had to bite the bullet to write a little own experience. Hope to
Enough to play a good effect.

In fact, we are in the Web page to achieve "diagram" effect is not necessarily the use of GD can not be more easily solved is columnar
Figure--It can be solved with HTML. Like what:

? $b = Array (150,110,125,180,160,175,230,220);?>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title></title>
<style>
<!--
td{font-size:9pt}
-->
</style>
<body>
<table border=0>
<tr valign= "Bottom" >/* (1) * *
<?for ($i =0 $i <8; $i + +) {><td align= "center" >
<table height= "<?echo $b [$i];? > "Border=0> * * (2) * *
<tr>
&LT;TD bgcolor= "#3F7F9F" width= "></td> * * (3) * *
</tr>
</table><br><font color= "#3F7F9F" ><?echo $b [$i]; ></font>/* (4) * *
</td><? }?>
</tr>
</table>
</body>

? $b = Array (150,110,125,180,160,175,230,220);?> is a set of data, where the data comes from, without
The big picture, depends on your needs, the code needs to say two places I have added a comment, now Yi Yilai description.

(1) Note here is the valign= "bottom", is to make the contents of the cell at the bottom of the alignment. Why add to <tr>
It? You can make the contents of this line in the table follow this alignment without having to specify it in each <td>, so you can
Save dozens of bytes in the original code of the HTML page that makes PHP execute the results! Save the valuable time of the viewer.

(2) Note that the most important thing is here! <table height= "xxx", we are using the table's Height property to
To achieve a "column" of different heights. I'm here to make it clear that the original data has not been scaled proportionally,
If your data is particularly large, or especially small, it is not appropriate to directly assign the table to the height property, but should be based on the situation
Conditions to scale the data to the appropriate proportions. For example, you estimate that every digit of your set of data will be between 3000~8000,
Can consider to reduce them 25 times times, namely height= " echo Floor (b[$i]/25);?> "

(3) mention the bgcolor= "#xxxxxx" in this line, which is the color of the cylinder (RGB). In fact, the real histogram should
Each column with a color, here for the code as simple as possible, I used this for loop, so there is no way to
Each column specifies a color. Well, there's a way, I just don't have to write it for this example.
A function to extract the color to get the beginner dizzy. So, that part is to be perfected by yourself.

(4) Display the true data in the same color as the cylinder. Of course, you can also choose to put this number on top of the cylinder
, maybe a little more professional. However, I am still used to putting it down.

With the help of HTML table, we can construct various columnar graphs, this example is about using bgcolor to display the color block,
In addition, you can also use the background= "(picture)", the picture is patterned, so the column shape of the cylinder has a pattern.
And you show the real data in a contrasting color to the <td> shown in the note (3) above, which is also a good result.

The front is an effective way to avoid GD, but to do complex graphics, it is not to use GD.

Sadly's PHP4 Chinese manual says that there are 44 functions in the GD function library, but I read the latest edition of the English PHP4 manual,
GD's function has more than 80! As the author of English is poor, read English handbook can only take a guess, so not sure
Does the new GD library support GIF again? Anyway, I think that since we're using completely free PHP, what's the need
"Adventure" to use a copyrighted gif? Why not free it to the end, with PNG? As long as you don't need animations, PNG can also make
A file as small as a GIF!

Here I will combine a program, a code to say the code of the commonly used GD functions.

Let's start with the beginning.

?
Header ("Content-type:image/png");
This is to send an HTTP header, tell the browser: "You listen, this is an image, but not as a word to show it!" ”
Because of my personal preference, PNG, 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 was not specified in the image format when it was created.
imagecreate function, two parameters, no doubt, this is created by the width and height of the image.
Its return value is an int value, this value is very important, you continue to draw this image,
Until you output this image, there is nowhere to go before this number, we call it the ID of the image.
Because the frequency of use is quite high, we assign it to a variable whose name is relatively short.

Now let's draw a line first. The function of drawing a line is this:
Imageline (int im, int x1, int y1, int x2, int y2, int col);
The first parameter IM, is the ID of the image, the x1,y1,x2,y2 behind it, needless to say,
is the coordinates of the starting point (x1,y1) (X2,Y2)! (the upper-left corner of the image coordinates (0,0))
What is the last parameter? It's a color! GD requires that colors be defined for the image to be plotted with these colors.
Why do you want to define colors for images? I guess, for GIF, PNG and other images used as a "palette."
This involves the knowledge of the image itself, not to be discussed here.
So before we draw the line, we have to define the color (really troublesome).

$col _red = imagecolorallocate ($im, 255,192,192);
This function has four parameters, the first $im ... What do I have to say every time? Don't say it next time!
The following three parameters are the components of the red (R), Green (G), Blue (B) of the color to be defined, between 0~255.
This involves the knowledge of physics and optics. The different light components of red, green, and blue primaries,
Has produced the ever-changing color. Above I define this color, red 255, Green 192, Blue 192.
If not mistaken, this is a lighter red. Wait a moment, let's draw a line to try.
Why wait for a while? Because a picture has only one color, it is nothing to see!
Let's get the background black first!
Although the manual does not make it clear, I find that the first color defined will default to the background.

$col _black = imagecolorallocate ($im, 0,0,0);
The definition of a color, red, green, blue light is not, natural pitch-dark-black.
Then define the color of the drawing line:
$col _red = imagecolorallocate ($im, 255,192,192);

Now it's time to start drawing the red line:
Imageline ($im, $col _red);
Don't worry, you will not see the image after this sentence is over.

Imagepng ($im);
This is the output of the image, Imagepng () output PNG image, imagejpeg output JPEG image,
Imagegif Output GIF image ...
Don't forget to have a parameter here, if the screen is displayed instead of saving it as a file,
This argument is omitted--the saved file name. If this is to save it as a file,
It should be written like this: Imagepng ($im, "test.png");
If you do not specify a path, the file is saved in your web's current directory.
If it is JPEG, then one more parameter, is the JPEG quality (0~100).
If you want to display on the screen, then imagejpeg ($im, "", 80);
If you want to save, then imagejpeg ($im, "test.jpg", 80);
Note that if you want to save this image as a file,
You cannot use Header ("Content-type:image/png"); Transmission means an image of the HTTP header,
Because once it does, it means you're going to output the image.

Imagedestroy ($im);
Destroy the image in memory to free up memory space.
This is good: a picture of the simplest GD is made.

The test found that the image file was generated in PNG format with only 131 bytes,
In JPEG format, even with the worst quality (0), 855 bytes are required, and the image quality is too bad to be seen.
and the highest JPEG quality, you need 2360 bytes, color is still not as bright as PNG.
This shows that PNG is much more cost-effective than JPEG for this image with a small number of colors.
?>

This time, I will try to continue writing as soon as possible.


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.