Generate Image program code in PHP

Source: Internet
Author: User
Tags 0xc0 relative 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.

1. Function Graph area fills 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:

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:

The code is as follows Copy Code
<?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
?>

2. 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:

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:

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, $x 2) to ($x 2, $y 2) coordinates to draw a line.

3. 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:

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).

4. 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:

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.

5. Draw Ellipse 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:

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.

6. Draw Arc Imagearc ()

is achieved by 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:

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.

Let's look at an example of a 3D-sector statistic diagram generated by a picture processing function

The code is as follows Copy Code

<?php
Creates a canvas, returns a variable $image of a resource type, and opens a staging area in memory
$image = Imagecreatetruecolor (100, 100); Create a canvas size of 100x100

Set the desired color in the image, equivalent to the dye box prepared in the drawing
$white = Imagecolorallocate ($image, 0xFF, 0xFF, 0xFF); Assign a color to a white image
$gray = Imagecolorallocate ($image, 0xc0, 0xc0, 0xc0); Assign a color to the image to be gray
$darkgray = Imagecolorallocate ($image, 0x90, 0x90, 0x90); Assign a color to an image to be dark gray
$navy = Imagecolorallocate ($image, 0x00, 0x00, 0x80); Assign color to dark blue for an image
$darknavy = Imagecolorallocate ($image, 0x00, 0x00, 0x50); Assign a color to the image dark dark blue
$red = Imagecolorallocate ($image, 0xFF, 0x00, 0x00); Assign a color to a red image
$darkred = Imagecolorallocate ($image, 0x90, 0x00, 0x00); Assign a color to a dark red image

Imagefill ($image, 0, 0, $white); Fill background color for canvas background
Dynamic production of 3D effects
for ($i = $i >50; $i-) {//Loop 10 times draw three-dimensional effect
Imagefilledarc ($image, $i, M, -160, $darknavy, Img_arc_pie);
Imagefilledarc ($image, $i, M, N, $darkgray, Img_arc_pie);
Imagefilledarc ($image, $i, M, N, $darkred, Img_arc_pie);
}

Imagefilledarc ($image, M, -160, $navy, Img_arc_pie); Draw an elliptical arc and fill
Imagefilledarc ($image, $gray, M, Img_arc_pie);; Draw an elliptical arc and fill
Imagefilledarc ($image, N, $red, Img_arc_pie), and Draw an elliptical arc and fill

Imagestring ($image, 1, MB, ' 34.7% ', $white); To draw a line of strings horizontally
Imagestring ($image, 1,,, ' 55.5% ', $white); To draw a line of strings horizontally

Output a picture in GIF format to the browser
Header (' content-type:image/png '); Use the header function to tell the browser to process the following output in image mode
Imagepng ($image); Output to Browser
Imagedestroy ($image); Destroying image release resources
?>

Either request the script directly from the browser, or assign the URL of the script to the SRC attribute of the IMG tag in HTML, you can get the dynamic output image results as shown in the following illustration:

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.