Some functions of drawing images in PHP _php tips

Source: Internet
Author: User
Tags set background

The functions of drawing images in PHP are very rich, including points, lines, all kinds of geometric figures can be imagined graphic graphics, can be provided through PHP in a variety of drawing functions to complete. We are here to introduce some common image rendering, if you use the function we have not introduced, you can refer to the manual implementation. In addition, these graphical drawing functions require the use of canvas resources, and the position in the canvas is determined by the coordinates (the origin is the starting position in the upper-left corner of the canvas, in pixels, along the x-axis positive direction to the right, and the y-axis is extending downward), and the color of each shape can be set by the last argument of the function. The coordinate system in the canvas is shown in the figure.

function Graphics area fill Imagefill ()

It is not enough to draw only the geometry of the edges through PHP, but you can also use the corresponding fill function to complete the fill of the graphics area. You can also use the Imagefill () function to implement a zone fill, except for each graphic that has a corresponding fill function. The syntax format for this function is as follows:

Copy Code code as follows:

BOOL Imagefill (resource $image, int $x, int $y, int $color)//region fill

On the image represented by the parameter $image, the function performs a region fill from the coordinates ($x, $y) with the specified color, relative to the upper-left corner (0,0) coordinates of the image. Points with the same color as the coordinates ($x, $y) are populated. For example, in the following example, set the canvas's background to red. The code looks like this:
Copy Code code as follows:

<?php
$im = Imagecreatetruecolor (100, 100); Create a 100*100 size canvas
$red = Imagecolorallocate ($im, 255, 0, 0); Set a color variable to red

Imagefill ($im, 0, 0, $red); Set Background to Red

Header (' content-type:image/png '); Notifies the browser that this is not text but a picture
Imagepng ($im); Generate the PNG format picture output to the browser

Imagedestroy ($im); Destroying image resources, freeing the memory space occupied by the canvas
?>

Second, draw point and Line Imagesetpixel (), Imageline ()

Drawing dots and lines is the most basic operation in the drawing of images, and if used flexibly, they can be plotted with ever-changing images. In PHP, you 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 code as follows:

BOOL Imagesetpixel (resource $image, int $x, int $y, int $color)//Draw a single pixel

On the canvas provided in the first parameter $image, the function draws a pixel with a color of $color from the coordinate position of the $x and $y respectively. In theory, you can use a point function to draw all the graphics you need, or you can 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 code as follows:

BOOL Imageline (resource $image, int $x 1,int $y 1,int $x 2,int $y 2,int $color)//Draw a line segment

We all know two points to determine a segment, so the function uses $color color in the image $image, starting from coordinates ($x 1, $y 1) to ($x 2, $y 2) coordinates to draw a line.

Third, draw rectangular imagerectangle (), Imagefilledrectangle ()

You can draw a rectangle using the Imagerectangle () function, or you can draw a rectangle and fill it with the Imagefilledrectangle () function. The syntax format for these two functions is as follows:

Copy Code code as follows:

BOOL Imagerectangle (resource $image, int $x 1, int $y 1,int $x 2,int $y 2,int $color)//Draw a rectangle
BOOL Imagefilledrectangle (Resource image,int $x 1, int $y 1, int $x 2, int $y 2,int $color)//Draw a rectangle and fill

The two functions behave similarly by drawing a rectangle in the $image image, except that it uses the $color parameter to specify the edge color of the rectangle, and the latter fills the rectangle with that color. Relative to the (0,0) position in the upper-left corner of the image, the upper-left corner coordinates of the rectangle are ($x 1, $y 1) and the lower-right corner coordinates ($x 2, $y 2).

Draw Polygon Imagepolygon (), Imagefilledpolygon ()

You can draw a polygon using the Imagepolygon () function, or you can draw a polygon and fill it with the Imagefilledpolygon () function. The syntax for these two functions is as follows:

Copy Code code 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

The behavior of these two functions is similar to drawing a polygon in a $image image, except that it uses the $color parameter to specify the edge color of the polygon, and the latter fills the polygon with that color. The second parameter, $points, is a PHP array that contains the coordinates of each vertex of the polygon. That is, points[0]=x0,points[1]=y0,points[2]=x1,points[3]=y1, and so forth. The third parameter, $num_points, is the total number of vertices, which must be greater than 3.

V. Drawing of elliptical imageellipse (), Imagefilledelipse ()

You can draw an ellipse using the Imageellipse () function, or you can draw an ellipse and fill it with the Imagefilledellipse () function. The syntax for these two functions is as follows:

Copy Code code 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 oval fill

These two functions behave similarly by drawing an ellipse in the $image image, except that it uses the $color parameter to specify the edge color of the ellipse, while the latter uses it to fill the color. Relative to the upper-left corner of the canvas (0,0), an ellipse is drawn at the center ($CX, $cy) coordinates, and the parameters $w and $h Specify the width and height of the ellipse respectively. Returns true if successful, and returns False if it fails.

Six, draw the Arc Imagearc ()

The 3D pie chart example described earlier is implemented using a function that draws a filled arc. You can use the Imagearc () function to draw an arc, as well as a circle and an ellipse. The syntax format for this function is as follows:

Copy Code code 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 upper-left corner coordinate (0,0) of the canvas, the function centers on ($CX, $cy) coordinates, drawing an elliptical arc in the image represented by $image. Where the parameters $w and $h Specify the width and height of the ellipse, and the starting and ending points are specified in terms of the $s and $e parameters. The 0º is at three o'clock, drawing in clockwise direction. If you want to draw a complete circle, first set the parameters $w and $h to equal values, and then set the starting angle $s to 0, and the end angle $e to 360. If you need 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.