This article mainly introduces some function summaries for Drawing Images in PHP. This article describes functions such as drawing points and lines, drawing rectangles, drawing polygon, drawing an ellipse, and drawing an arc, for more information, see
This article mainly introduces some function summaries for Drawing Images in PHP. This article describes functions such as drawing points and lines, drawing rectangles, drawing polygon, drawing an ellipse, and drawing an arc, for more information, see
The functions for Drawing Images in PHP are rich, including points, lines, various geometric figures, and other plane graphics that can be imagined. They can all be completed through various drawing functions provided in PHP. Here we will introduce some common image rendering functions. If you use functions we have not introduced, refer to the Manual for Implementation. In addition, all these graphic painting functions use canvas resources and use coordinates in the canvas position (the origin is the starting position in the upper left corner of the canvas, in pixels, extend to the right along the positive side of the X axis, and extend downward along the positive side of the Y axis). You can also set the color of each graph through the last parameter of the function. Coordinate System in the canvas.
I. Fill imageFill in the function graphic area ()
It is not enough to use PHP to plot a geometric chart with only edges. You can also use the corresponding filling function to fill the graphic area. In addition to the corresponding filling function for each graph, you can also use the imageFill () function to implement area filling. The syntax format of this function is as follows:
The Code is as follows:
Bool imagefill (resource $ image, int $ x, int $ y, int $ color) // fill in the area
This function starts from the coordinates ($ x, $ y) in the upper-left corner of the image, relative to the coordinates () in the upper-left corner of the image) fill in the execution area with the color specified by $ color. The coordinates ($ x, $ y) and adjacent points are filled in. For example, in the following example, set the background of the canvas to red. The Code is as follows:
The Code is as follows:
<? Php
$ Im = imagecreatetruecolor (100,100); // create a canvas of 100*100 size
$ Red = imagecolorallocate ($ im, 255, 0, 0); // set a color variable to red.
Imagefill ($ im, 0, 0, $ red); // set the background to red
Header ('content-type: image/png '); // notifies the browser that this is not text but an image
Imagepng ($ im); // generate an image in PNG format and output it to the browser.
Imagedestroy ($ im); // destroy image resources and release the memory space occupied by the canvas
?>
2. Draw points and lines imageSetPixel () and imageline ()
Painting points and lines are the most basic operations for Drawing Images. If you use them flexibly, you can use them to draw ever-changing images. In PHP, use the imageSetPixel () function to draw a single pixel vertex in the canvas and set the color of the vertex. The function prototype is as follows:
The Code is as follows:
Bool imagesetpixel (resource $ image, int $ x, int $ y, int $ color) // draw a single pixel
On the canvas provided by the first parameter $ image, this function draws a pixel with a color of $ color from the coordinates of $ x and $ y respectively. Theoretically, you can use the point-of-painting function to draw all the images you need, or use other drawing functions. To draw a line segment, you can use the imageline () function. The syntax format is as follows:
The Code is as follows:
Bool imageline (resource $ image, int $ x1, int $ y1, int $ x2, int $ y2, int $ color) // draw a line segment
We all know that two points determine a line segment, so this function uses the $ color in the image $ image, starting from the coordinate ($ x1, $ y1) to ($ x2, $ y2) draw a line segment at the end of the coordinate.
3. Draw rectangular imageRectangle () and imageFilledRectangle ()
You can use the imageRectangle () function to draw a rectangle, or you can use the imageFilledRectangle () function to draw and fill a rectangle. The syntax format of these two functions is as follows:
The Code is as follows:
Bool imagerectangle (resource $ image, int $ x1, int $ y1, int $ x2, int $ y2, int $ color) // draw a rectangle
Bool imagefilledrectangle (resource image, int $ x1, int $ y1, int $ x2, int $ y2, int $ color) // draw and fill a rectangle
The behavior of these two functions is similar. They all draw a rectangle in the $ image, except that the former uses the $ color parameter to specify the edge color of the rectangle, while the latter uses this color to fill the rectangle. Relative to the (0, 0) position in the upper left corner of the image, the coordinates in the upper left corner of the rectangle are ($ x1, $ y1), and those in the lower right corner are ($ x2, $ y2 ).
4. Draw polygon imagePolygon () and imagefilledpolygon ()
You can use the imagePolygon () function to draw a polygon, or you can use the imageFilledPolygon () function to draw a polygon and fill it in. The syntax format of these two functions is as follows:
The Code is as follows:
Bool imagepolygon (resource $ image, array $ points, int $ num_points, int $ color) // draw a polygon
Bool imagefilledpolygon (resource $ image, array $ points, int $ num_points, int $ color) // draw a polygon and fill it
The behavior of these two functions is similar. They all draw a polygon in the $ image, except that the former uses the $ color parameter to specify the edge color of the polygon, while the latter uses this color to fill the polygon. The second parameter $ points is a PHP array that contains the coordinates of each vertex of a polygon. That is, points [0] = x0, points [1] = y0, points [2] = x1, points [3] = y1, and so on. The third parameter $ num_points is the total number of vertices, which must be greater than 3.
5. Draw the elliptical imageEllipse () and imageFilledElipse ()
You can use the imageEllipse () function to draw an elliptic, or you can use the imageFilledEllipse () function to draw an elliptic and fill it in. The syntax format of these two functions is as follows:
The Code is as follows:
Bool imageellipse (resource $ image, int $ cx, int $ cy, int $ w, int $ h, int $ color) // draw an elliptic
Bool imagefilledellipse (resource $ image, int $ cx, int $ cy, int $ w, int $ h, int $ color) // draw an elliptical fill
The two functions are similar in behavior. They all draw an ellipse in the $ image, except that the former uses the $ color parameter to specify the edge color of the elliptical shape, while the latter uses it to fill the color. Relative to the coordinates (0, 0) in the upper left corner of the canvas, an elliptic is drawn centered on the coordinates ($ cx, $ cy). The parameter $ w and $ h specify the width and height of the elliptic respectively. If the call succeeds, TRUE is returned. If the call fails, FALSE is returned.
6. Draw an arc imageArc ()
The preceding example shows how to use a function to draw a filled arc. You can use the imageArc () function to draw an arc, a circle, and an elliptical shape. The syntax format of this function is as follows:
The Code is as follows: