Use the GD2 function to draw a geometric chart (typical PHP image tutorial 4)

Source: Internet
Author: User
This article mainly explains how to use the GD2 function to draw ry. First of all, we need to create an image. As we mentioned in the previous article, creating an image is the first step in all image operations. then, draw the outline of the image based on the coordinate point on the background, and finally output the image! Use the GD2 function to draw a geometric chart (typical PHP image tutorial 4)

This article mainly explains how to use the GD2 function to draw ry. First of all, we need to create an image. As we mentioned in the previous article, creating an image is the first step in all image operations. then, draw the outline of the image based on the coordinate point on the background, and finally output the image!

In the previous article "using image processing technology to generate a verification code (PHP graphic image typical application tutorial 3)", we introduced how to generate a verification code. I wonder if my friends have mastered this knowledge point, if you are not familiar with it, you can review it! Today we will explain how to draw a geometric chart!

Technical points of this article:

In this article, we mainly use the GD2 function to draw ry. some of the functions have been described in detail in the previous article. below we will introduce common functions:

(1) imagecreatetruecolor () function

This function is used to recommend a full-color image file resource. the syntax format of this function is as follows:

int imagecreatetruecolor(int x_size,int y_size);

The x_size and y_size parameters are respectively the width and height of the image, measured in pixels (px ).

(2) imagecolorallocate () function

This function is used to match the color of the specified image for other plotting functions. the syntax format is as follows:

int imagecolorallocate(int im, int red, int green, int blue);

The im parameter indicates the handle of the image. The red, green, and blue parameters are color RGB primary colors, and their values are integers ranging from 0 to 255 or 0x00 to hexadecimal values ~ 0xff. This function must be called to create each color used in the image.

(3) imagefilledarc () function

This function is used to draw and fill an ellipse. the syntax format of this function is as follows:

int imagefilledarc(im, int cx , int w, int h, int s, int e, int color, int style);

In the image represented by im, draw an elliptical arc with cx, cy (0, 0 in the upper left corner of the image) as the center. If the call succeeds, true is returned. if the call fails, false is returned. W and h respectively specify the width and height of the elliptic, s and e parameters specify the starting point and ending point with an angle, and color indicates the filled color value.

Style can be the values of the following values after bitwise OR (OR:

  • IMG_ARC_PIE

  • IMG_ARC_CHORD

  • IMG_ARC_NOFILL

  • IMG_ARC_EDGED

IMG_ARC_PIE and IMG_ARC_CHORD are mutually exclusive. IMG_ARC_CHORD only uses a straight line to connect the start and end points. IMG_ARC_PIE generates a circular boundary (if both are used, IMG_ARC_CHORD takes effect ). IMG_ARC_NOFILL indicates that the arc or string has only the contour and is not filled. IMG_ARC_EDGED indicates that a straight line is used to connect the start and end points to the center. it is a good way to draw the outline of a pie chart (instead of filling it) with IMG_ARC_NOFILL ).

(4) imagefilledrectangle () function

This function is used to draw a filled rectangle. the syntax format of this function is as follows:

bool imagefilledrectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int$color )

This function colors the closed rectangular area of the image. the parameters x1, y1, and x2 and y2 are the coordinates of the diagonal line of the rectangle. The col parameter indicates the color to be coated;

(5) imagedestroy () function

This function is used to delete images and release memory space. the syntax format is as follows:

int imagedestroy(int im);

The im parameter is the image created by the imagecreatetruecolor () function!

Now let's take a look at how to plot the RY:

The implementation code is as follows:

 100; $ I --) {imagefilledarc ($ img, 130, $ I, 150,100, 130, $ darkgreen, IMG_ARC_PIE); imagefilledarc ($ img, 150,100, $ I, 95,125, $ darkblue, IMG_ARC_PIE); imagefilledarc ($ img, 130, $ I, 150,100,120,360, $ bluered, IMG_ARC_PIE);} imagefilledarc ($ img, 130, $ I, 150,100, 130, $ green, IMG_ARC_PIE); imagefilledarc ($ img, 150,100, $ I, 95,125, 130, $ blue, IMG_ARC_PIE); imagefilledarc ($ img, 150,100,120,360, $ I, $ red, IMG_ARC_PIE); imagepng ( $ Img); // output the image imagedestroy ($ img) in png format; // release the resource?>

The output result is as follows:

The GD2 function is used to draw the ry. we have finished the introduction here. I believe all of you can master this function, next, we will continue to introduce how to add row and column labels to images in charts, for more information, see add row and column labels to charts using the GD2 function (PHP graphic image typical application tutorial 5).

The preceding section describes how to use the GD2 function to draw a geometric image (typical PHP image application tutorial 4). For more information, see other related articles in the first PHP community!

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.