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. |