Some functions of drawing images in PHP summary, PHP drawing image function _php Tutorial

Source: Internet
Author: User

Summary of some functions of drawing images in PHP, PHP drawing image function


The function of drawing images in PHP is very rich, including points, lines, various geometries and other conceivable plane graphics, can be done by the various drawing functions provided in PHP. We are here to introduce some of the commonly used image rendering, if you use a function we have not introduced, you can refer to the manual implementation. In addition, these drawing functions need to use the canvas resource, and the position in the canvas is determined by the coordinates (where the origin is at the beginning of the upper-left corner of the canvas, in pixels, along the positive direction of the x-axis, the y-axis is stretched downward), and the color of each graphic can also be set through the last parameter of the The coordinate system in the canvas.

First, the function graphics area fills Imagefill ()

It is not enough to draw only the edges of the geometry through PHP, and you can use the corresponding fill function to complete the fill of the graphics area. You can also use the Imagefill () function to implement area fills, in addition to each drawing having a corresponding fill function. The syntax format for this function is as follows:
Copy the Code code as follows:
BOOL Imagefill (resource $image, int $x, int $y, int $color)//area fill

The function performs an area fill from the coordinates ($x, $y) with the color specified by the parameter $color on the image represented by the parameter $image, relative to the upper-left corner of the image (0,0). The points with the same color as the coordinates ($x, $y) are populated with the adjacent dots. For example, in the following example, the background of the canvas is set to red. The code looks like this:
Copy the Code code as follows:
<?php
$im = Imagecreatetruecolor (100, 100); Create a 100*100-sized canvas
$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 '); Notification browser This is not text but a picture
Imagepng ($im); Generate image output in PNG format to browser

Imagedestroy ($im); Destroy the image resource and free up the memory space occupied by the canvas
?>

Second, draw points and lines imagesetpixel (), Imageline ()

Drawing points and lines is the most basic operation in drawing images, and if used flexibly, they can be drawn with ever-changing images. In PHP, use the Imagesetpixel () function to draw a single pixel point in the canvas and to set the color of the point. The prototype of its function is as follows:
Copy the Code code as follows:
BOOL Imagesetpixel (resource $image, int $x, int $y, int $color)//Draw a single pixel

The function draws a pixel with a color of $color on the canvas provided in the first parameter $image, the coordinates of the distance to the dots are $x and $y, respectively. You can use the draw point function in theory to draw all the graphics you need, or you can use other drawing functions. If you need to draw a segment, you can use the Imageline () function, whose syntax format is as follows:
Copy the 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 at coordinates ($x 1, $y 1) to ($x 2, $y 2) coordinates end to draw a line segment.

Third, Draw rectangle 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 the 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 behavior of these two functions is similar to that of drawing a rectangle in the $image image, except that it uses the $color parameter to specify the edge color of the rectangle, while the latter fills the rectangle with that color. Relative to the upper-left corner of the image (0,0), the upper-left coordinate of the rectangle is ($x 1, $y 1), and the lower-right coordinate is ($x 2, $y 2).

Four, Draw Polygon Imagepolygon (), Imagefilledpolygon ()

You can use the Imagepolygon () function to draw a polygon, or you can draw a polygon and fill it with the Imagefilledpolygon () function. The syntax format for these two functions is as follows:
Copy the 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 two functions behave similarly, drawing a polygon in a $image image, except that it uses the $color parameter to specify the edge color of the polygon, while 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 on. The third parameter, $num_points, is the total number of vertices and must be greater than 3.

Five, Draw ellipse imageellipse (), Imagefilledelipse ()

You can use the Imageellipse () function to draw an ellipse, or you can draw an ellipse and fill it with the Imagefilledellipse () function. The syntax format for these two functions is as follows:
Copy the 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 ellipse fill

These two functions behave similarly, by drawing an ellipse in the $image image, except that the edge color of the ellipse is specified using the $color parameter, while the latter is used to fill the color. Draws an ellipse centered on ($cx, $cy) coordinates relative to the upper-left corner of the canvas (0,0), and the parameters $w and $h Specify the width and height of the ellipse, respectively. Returns true if successful, and false if it fails.

Six, Draw 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 the 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 coordinate of the canvas (0,0), the function draws an elliptical arc in the image represented by $image, centered on ($CX, $cy) coordinates. Where the parameters $w and $h Specify the width and height of the ellipse respectively, the starting and ending points are specified with the $s and $e parameters as angles. The 0º is located at three o'clock ' clock and is painted in a clockwise direction. To draw a complete circle, first set the parameters $w and $h to equal values, then set the starting angle $s to 0 and the end angle $e to 360. If you need to draw a fill arc, you can query the Imagefilledarc () function for use.

http://www.bkjia.com/PHPjc/914046.html www.bkjia.com true http://www.bkjia.com/PHPjc/914046.html techarticle some of the functions of drawing images in PHP are summarized, PHP drawing function in PHP drawing image function is very rich, including points, lines, a variety of geometry and other conceivable plane graphics, ...

  • Related Article

    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.