Summary of some functions for drawing images in PHP

Source: Internet
Author: User
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 about how to draw images in PHP, you can use a wide range of functions, including point, line, and various geometric figures, you can use various graph 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, draw an ellipse centered on the coordinates ($ cx, $ cy). the parameters $ w and $ h specify the width and height of the ellipse 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:


Bool imagearc (resource $ image, int $ cx, int $ cy, int $ w, int $ h, int $ s, int $ e, int $ color) // draw an elliptical arc


Relative to the coordinate (0, 0) in the upper left corner of the canvas, this function is centered on the coordinates ($ cx, $ cy) and draws an elliptical arc in the image represented by $ image. The $ w and $ h parameters specify the width and height of the ellipse respectively. the starting point and ending point are specified with the angle of $ s and $ e parameters. 0 ° is located at three o'clock and painted clockwise. To draw a complete circle, set the $ w and $ h parameters to equal values, and then set the starting angle $ s to 0, end angle $ e is 360. to draw a filled arc, you can query the imageFilledArc () function.

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.