PHP drawing Basics

Source: Internet
Author: User
Tags image identifier
Title: PHP drawing basics Author: MoreWindowsBlog: blog. csdn. netMoreWindowsKeyWord: PHP painting point, line, Arc Drawing and fill area picture special effects color Christmas heavy snow pattern This article summarizes the commonly used PHP drawing functions. The content includes creating an image, assigning color, point, line, and arc to the image,

Title: PHP drawing base Author: MoreWindows Blog: http://blog.csdn.net/MoreWindows KeyWord: PHP drawing point, line, Arc Drawing and fill area picture special effects color Christmas snow pattern This article summarizes the commonly used PHP drawing function. The content includes creating an image, assigning color, point, line, and arc to the image,

Title: PHP drawing Basics

Author: MoreWindows

Blog: http://blog.csdn.net/MoreWindows

KeyWord: PHP plot point, line, arc plot and fill area picture special effect color Christmas snow Pattern

This article summarizes the commonly used PHP plot functions. The content includes creating images, assigning colors, points, draw lines, draw arcs, draw and fill areas to images, output characters and Chinese characters, and some common image effects such as reversed colors and relief. In addition, some interesting examples are provided, such as creating a colorful Christmas big snow map.


1. Create an image

ResourceImagecreate(Int$ X_size, Int$ Y_size)

Imagecreate ()Returns an image identifier, representing the size of an image.X_sizeAndY_size.

ResourceImagecreatetruecolor(Int$ X_size, Int$ Y_size)

Imagecreatetruecolor ()Returns an image identifier, representing the size of an image.X_sizeAndY_sizeBlack image. The imagecreatetruecolor () function is recommended in the PHP manual.

You can also create an image function by using a file such as ..gif).jpg.

ResourceImagecreatefromgif(String$ Filename)

ResourceImagecreatefrompng(String$ Filename)

ResourceImagecreatefromjpeg(String$ Filename)


2. assign color to the image

IntImagecolorallocate(Resource$ Image, Int$ Red, Int$ Green, Int$ Blue)

Imagecolorallocate ()Returns an identifier representing the given RGBThe color of the component.Red,GreenAndBlueThey are the red, green, and blue components of the required colors. These parameters are integers from 0 to 255 or hexadecimal values 0x00 to 0xFF. First image callImagecolorallocate ()Specifies the background color of the image.

IntImagecolorallocatealpha(Resource$ Image, Int$ Red, Int$ Green, Int$ Blue, Int$ Alpha)

Imagecolorallocatealpha ()The behavior is the same as imagecolorallocate (), but an additional transparency parameter is added.Alpha, Whose value is from0To127.0Indicates completely opaque,127Completely transparent.

Iii. Painting points

BoolImagesetpixel(Resource$ Image, Int$ X, Int$ Y, Int$ Color)

Note: the upper left corner of the image is (0, 0)

4. Draw a line

BoolImageline(Resource$ Image, Int$ X1, Int$ Y1, Int$ X2, Int$ Y2, Int$ Color)

From (x1, y1) to (x2, y2 ). The line style can be changed by boolImagesetstyle(Resource$ Image, Array$ Style. Width by boolImagesetthickness(Resource$ Image, Int$ Thickness) Control, note that this width also takes effect when drawing a rectangle or an arc.

V. Draw an elliptical arc

BoolImagearc(Resource$ Image, Int$ Cx, Int$ Cy, Int$ W, Int$ H, Int$ S, Int$ E, Int$ Color)

Imagearc ()ToCx,Cy(0 in the upper left corner of the image)ImageDraw an elliptical arc in the image.WAndHSpecify the width and height of the ellipse, respectively.SAndEThe parameter is specified as an angle. 0 degrees is located at three o'clock and painted clockwise. For example:

$ Black = imagecolorallocate ($ img, 0, 0, 0 );

Imagearc ($ img, 100,100,150,180, 0, 90, $ black );

An arc ranging from 0 to 90 degrees with a width of 100,100 and a height of 150 will be drawn at (180), as shown in (for reference, the right side is the full graph ):


6. Draw a region

Rectangle

BoolImagerectangle(Resource$ Image, Int$ X1, Int$ Y1, Int$ X2, Int$ Y2, Int$ Col)

Elliptic

BoolImageellipse(Resource$ Image, Int$ Cx, Int$ Cy, Int$ W, Int$ H, Int$ Color)

Polygon

BoolImagepolygon(Resource$ Image, Array$ Points, Int$ Num_points, Int$ Color)

7. Filling Area

Filling Area

BoolImagefill(Resource$ Image, Int$ X, Int$ Y, Int$ Color)

Imagefill () is used to fill the image (x, y) with the color execution area (that is, the color of the (x, y) vertex is the same and the adjacent vertex is filled ). For example, the following code snippet first draws a blue oval, and then fills the interior of the oval with red.

$blue_color = imagecolorallocate($img, 0, 0, 255);             $red_color = imagecolorallocate($img, 255, 0, 0);imageellipse($img, 300, 200, 300, 200, $blue_color);imagefill($img, 300, 200, $red_color);

The running effect is as follows:



Draw an ellipse and fill it

BoolImagefilledellipse(Resource$ Image, Int$ Cx, Int$ Cy, Int$ W, Int$ H, Int$ Color)

There is no border for this method of painting elliptic, of course, it can also be implemented as follows:

$ Export cy_color = imagecolorallocatealpha ($ img, 0, 0, 0,126); // 127 is completely transparent. 0 is completely transparent. $ red_color = imagecolorallocate ($ img, 255, 0, 0 ); imageellipse ($ img, 300,200,300,200, $ cy_color); imagefill ($ img, 300,200, $ red_color); // imagefilledellipse ($ img, 300,200,300,200, $ red_color );

Draw and fill a rectangle

BoolImagefilledrectangle(Resource$ Image, Int$ X1, Int$ Y1, Int$ X2, Int$ Y2, Int$ Color)

It is similar to drawing an elliptic and filling it.

Draw and fill an elliptical arc

BoolImagefilledarc(Resource$ Image, Int$ Cx, Int$ Cy, Int$ W, Int$ H, Int$ S, Int$ E, Int$ Color, Int$ Style)

There are four values for the last parameter description:

IMG_ARC_PIEGenerate a Circular Boundary (if both are used,IMG_ARC_CHORD).

IMG_ARC_CHORDUses a straight line to connect the start and end points.

IMG_ARC_NOFILLDraw an arc, only the outline, not filled.

IMG_ARC_EDGEDSpecifies to use a straight line to connect the start and end points to the center point

Check the actual effect (the arc angle ranges from 0 to 210 degrees ):


This function will be used to draw a pie chart.


8. Character

Draw a character horizontally

BoolImagechar(Resource$ Image, Int$ Font, Int$ X, Int$ Y, String$ C, Int$ Color)

Draw a vertical character

BoolImagecharup(Resource$ Image, Int$ Font, Int$ X, Int$ Y, String$ C, Int$ Color)

Draw a line of strings horizontally

BoolImagestring(Resource$ Image, Int$ Font, Int$ X, Int$ Y, String$ S, Int$ Col)

Draw a line of string vertically

BoolImagestringup(Resource$ Image, Int$ Font, Int$ X, Int$ Y, String$ S, Int$ Col)

Pay attention to the $ font parameter. either use the built-in font (from 1 to 5) or use the intImageloadfont(String$ File) Load the font and then set it.

You can use the output * to obtain a colorful Christmas snowflake chart. The Code is as follows:

 

The running effect is as follows:



IX. Text

ArrayImagettftext(Resource$ Image, Float$ Size, Float$ Angle, Int$ X, Int$ Y, Int$ Color, String$ Fontfile, String$ Text)

Several parameters are explained:

The second parameter $ size is the font size.

The third parameter $ angle is the text rotation angle, and 0 degrees is the text read from left to right. A higher value indicates the clockwise rotation. For example, 90 degrees indicates the text read from the bottom up.

The seventh parameter $ fontfile indicates the font file, for example, "c: \ WINDOWS \ Fonts \ simhei. ttf ".

Note! Use this function with imagecreatetruecolor () instead of imagecreate ().

The following code uses imagettftext () to replace the above imagechar to generate a colored Christmas snow pattern:
 

The running effect is as follows:


10. image effects

BoolImagefilter(Resource$ Src_im, Int$ Filtertype[, Int$ Arg1[, Int$ Arg2[, Int$ Arg3])

Many special effects are provided here, such as relief, reversed color (negative color), grayscale adjustment, brightness, contrast, blur, and so on. This only shows several common special effects. For more information, visit http://www.php.net/manual/zh/function.imagefilter.php.

Source image:


Save the graph to D: \ 1234.pngto execute the following code.

IMG_FILTER_NEGATE: Invert all colors in the image (negative color ).


Code:

 

IMG_FILTER_EMBOSS: Embossed the image.


Code:

 

This article introduces this, "PHP drawing application verification code bar chart" will use the function described in this article to draw the verification code and column chart.

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.