Home > Backend Development > PHP Tutorial > Summary of some functions for drawing images in PHP, php image drawing functions_PHP tutorial

Summary of some functions for drawing images in PHP, php image drawing functions_PHP tutorial

WBOY
Release: 2016-07-13 10:13:49
Original
1260 people have browsed it

A summary of some functions for drawing images in PHP, PHP image drawing functions

There are many functions for drawing images in PHP, including points, lines, various geometric figures and other imaginable plane graphics, which can all be completed through various drawing functions provided in PHP. We introduce some commonly used image drawing here. If you use functions that we have not introduced, you can refer to the manual for implementation. In addition, these graphics drawing functions need to use canvas resources and pass coordinates in the position in the canvas (the origin is the starting position in the upper left corner of the canvas, in pixels, extending to the right along the positive direction of the X-axis, and the positive direction of the Y-axis direction extending downward), and the color of each graph can also be set through the last parameter of the function. The coordinate system in the canvas is as shown.

1. Function graphics area filling imageFill()

It is not enough to draw geometric figures with only edges through PHP. You can also use the corresponding filling function to complete the filling of the graphics area. In addition to each graphic having a corresponding fill function, the imageFill() function can also be used to implement area filling. The syntax format of this function is as follows:

Copy code The code is as follows:

bool imagefill(resource $image,int $x,int $y,int $color) //Area filling

This function fills the area from the coordinates ($x, $y) with the color specified by the parameter $color on the image represented by the parameter $image, relative to the (0,0) coordinate of the upper left corner of the image. Points with the same color and adjacent coordinates ($x, $y) will be filled. For example, in the example below, the background of the canvas is set to red. The code looks like this:
Copy code The code is as follows:

$im = imagecreatetruecolor(100, 100); //Create a canvas of size 100*100
$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'); //Notify the browser that this is not text but an image
imagepng($im); //Generate images in PNG format and output them to the browser

imagedestroy($im); //Destroy image resources and release the memory space occupied by the canvas
?>

2. Draw points and lines imageSetPixel(), imageline()

Drawing points and lines are the most basic operations in drawing images. If used flexibly, you can draw ever-changing images through them. In PHP, use the imageSetPixel() function to draw a single pixel point in the canvas, and you can set the color of the point. The prototype of its function is as follows:

Copy code The code is as follows:

bool imagesetpixel(resource $image,int $x,int $y,int $color) //Draw a single pixel

This function draws a pixel with the color $color on the canvas provided in the first parameter $image, at the coordinates of $x and $y from the dot. Theoretically, you can draw all the graphics you need by using the point drawing function, and you can also use other drawing functions. If you need to draw a line segment, you can use the imageline() function, whose syntax format is as follows:
Copy code 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 color to draw a line segment in the image $image, starting from the coordinates ($x1, $y1) and ending at the coordinates ($x2, $y2).

3. Draw rectangle imageRectangle(), imageFilledRectangle()

You can use the imageRectangle() function to draw a rectangle, or you can use the imageFilledRectangle() function to draw a rectangle and fill it. The syntax format of these two functions is as follows:

Copy code 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 a rectangle and fill it

The behavior of these two functions is similar. They both draw a rectangle in the $image 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 of the upper left corner of the image, the coordinates of the upper left corner of the rectangle are ($x1,$y1) and the coordinates of the lower right corner are ($x2,$y2).

4. Draw polygons imagePolygon(), 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. The syntax format of these two functions is as follows:

Copy code 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 both draw a polygon in the $image 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 containing the coordinates of each vertex of the 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 ellipse imageEllipse(), imageFilledElipse()

You can use the imageEllipse() function to draw an ellipse, or you can use the imageFilledEllipse() function to draw an ellipse and fill it. The syntax format of these two functions is as follows:

Copy code The code is as follows:

bool imageellipse(resource $image,int $cx,int $cy,int $w,int $h,int $color) //Draw an ellipse
bool imagefilledellipse(resource $image,int $cx,int $cy,int $w,int $h,int $color) //Draw an ellipse filled

These two functions behave similarly, both draw an ellipse in the $image image, except that the former uses the $color parameter to specify the edge color of the ellipse, while the latter uses it to fill the color. Relative to the coordinates (0,0) of the upper left corner of the canvas, draw an ellipse with the ($cx, $cy) coordinates as the center. The parameters $w and $h specify the width and height of the ellipse respectively. Returns TRUE if successful, FALSE if failed.

6. Draw arc imageArc()

The 3D fan chart example introduced earlier is implemented using the function of drawing filled arcs. You can use the imageArc() function to draw an arc, as well as circles and ellipses. The syntax format of this function is as follows:

Copy code The code is as follows:

bool imagearc(resource $image,int $cx,int $cy,int $w,int $h,int $s,int $e,int $color) //Draw elliptical arc

Relative to the coordinates (0,0) of the upper left corner of the canvas, this function takes the ($cx, $cy) coordinates as the center and draws an elliptical arc in the image represented by $image. The parameters $w and $h specify the width and height of the ellipse respectively, and the starting point and end point are specified in angles with the $s and $e parameters. 0º is at three o'clock, paint in a clockwise direction. If you want to draw a complete circle, first set the parameters $w and $h to equal values, then set the starting angle $s to 0, and specify the ending angle $e to 360. If you need to draw a filled arc, You can query the imageFilledArc() function to use.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/914046.htmlTechArticleA summary of some functions for drawing images in PHP, php drawing image functions There are many functions for drawing images in PHP, including Points, lines, various geometric figures and other imaginable plane figures,...
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template