Dynamic image processing-php (34)

Source: Internet
Author: User
: This article mainly introduces dynamic image processing-php (34). For more information about PHP tutorials, see. 1 Use of GD Library in PHP

PHP can not only generate HTML output, but also create and operate image files of different formats. PHP provides some built-in image information functions. you can also use the GD function library to create new images or process existing images. Currently, the GD2 library supports JPEG, PNG, and WBMP formats. However, the GIF format is no longer supported.

• JPEG is a standard name for compression. it is usually used to store photos or images with rich colors and color layers. This format uses lossy compression.

• PNG is a portable network image that adopts lossless compression standards.

• WBMP is a file format specially designed for wireless communication devices. But it is not widely used.

2. image generation steps

To create an image in PHP, follow these steps:
1. create a background image (also called a canvas). Subsequent operations are based on this background image.
2. draw an image profile or input text on the background.
3. output final image
4. release resources

 

3. canvas management

Imagecreate-create a palette-based image
?? Resource imagecreate (intx_size, inty_size)
?? This function is used to create an empty new canvas. the parameter is the image size in pixels ). Supports 256 colors.
?? Imagecreatetruecolor-create a true color image
?? Resource imagecreatetruecolor (intx_size, inty_size)
?? To create a true color image canvas, GD 2.0.1 or later is required. it cannot be used in GIF format.
?? Imagedestroy -- destroy an image
?? Boolimagedestroy (resource image)
?? Imagedestroy () releases the memory associated with the image.

Other functions

Resource imagecreatefrompng (string filename)
Create an image from a PNG file or URL
?? Resource imagecreatefromjpeg (string filename)
?? Create an image from a JPEG file or URL
?? Resource imagecreatefromgif (string filename
?? Create an image from a GIF file or URL
?? Resource imagecreatefromwbmp (string filename)
Create an image from a WBMP file or URL
?? Intimagesx (resource image) --- get image width
?? Ntimagesy (resource image) --- get image height

4. set the color.

Imagecolorallocate -- assign color to an image
?? Syntax: intimagecolorallocate (resource image, intred, intgreen, intblue)
?? Imagecolorallocate () returns an identifier representing a color consisting of a given RGB component. The image parameter is
?? The return value of the imagecreatetruecolor () function. Red, green, and blue are the required colors respectively. These parameters are integers from 0 to 255 or hexadecimal values 0x00 to 0xFF. Imagecolorallocate () must be called to create the color of each image used in the image.

5. generate images

Imagegif -- output the image to a browser or file in GIF format
?? Syntax: boolimagegif (resource image [, string filename])
?? Imagejpeg -- output an image to a browser or file in JPEG format
?? Syntax: boolimagejpeg (resource image [, string filename [, intquality])
?? Imagepng -- output the image to a browser or file in PNG format
?? Syntax: boolimagepng (resource image [, string filename])
?? Imagewbmp -- output the image to a browser or file in WBMP format
?? Syntax: boolimagewbmp (resource image [, string filename [, intforeground])

6. draw images

Imagefill -- region filling
?? Syntax: boolimagefill (resourceimage, intx, inty, intcolor)
?? Imagefill () is filled with the color execution area (that is, with x, y points are in the same color and adjacent points are filled ).
?? Imagesetpixel-draw a single pixel
?? Syntax: boolimagesetpixel (resource image, intx, inty, intcolor)
?? Imagesetpixel () draws a point in the x and y coordinates (0, 0 in the upper left corner of the image) using color in the image.
?? Imageline -- draw a line segment
?? Syntax: boolimageline (resource image, intx1, inty1, intx2, inty2, intcolor)
?? Imageline () uses color to draw a line segment from the coordinates x1, y1 to x2, y2 (0 in the upper left corner of the image.

Imagerectangle -- draw a rectangle

?? Syntax: boolimagerectangle (resource image, intx1, inty1, intx2, inty2, intcol)
?? Imagerectangle () uses the col color to draw a rectangle in the image. the coordinates in the upper left corner are x1 and y1, and those in the lower right corner are x2 and y2. The coordinates in the upper left corner of the image are 0 and 0.
?? Imagefilledrectangle -- draw a rectangle and fill it
?? Syntax: boolimagefilledrectangle (resource image, intx1, inty1, intx2, inty2, intcolor)
?? Imagefilledrectangle () draws a rectangle filled with color in the image. the coordinates in the upper left corner are x1, y1, and the lower right corner are x2 and y2. 0, 0 is the top left corner of the image

Imagepolygon -- draw a polygon
?? Syntax: boolimagepolygon (resource image, array points, intnum_points, intcolor)
?? Imagepolygon () creates a polygon in the image. Points is a PHP array that contains the coordinates of each vertex of a polygon, that is, points [0] = x0, points [1] = y0, points [2] = x1, points [3] = y1, and so on. Num_points is the total number of vertices.
?? Imagefilledpolygon -- draw a polygon and fill it
?? Syntax: boolimagefilledpolygon (resource image, array points, intnum_points, intcolor)
?? Imagefilledpolygon () draws a filled polygon in the image. The points parameter is an array containing x and y coordinates of vertices of a polygon in order. The num_points parameter is the total number of vertices and must be greater than 3.

Imageellipse -- draw an ellipse
?? Syntax: boolimageellipse (resource image, intcx, intcy, intw, inth, intcolor)
?? Imageellipse () draws an elliptic with the Center cx and cy (0, 0 in the upper left corner of the image) in the image. W and h respectively specify the width and height of the ellipse, and the color of the ellipse is specified by color.
?? Imagefilledellipse -- draw an ellipse and fill it
?? Syntax: boolimagefilledellipse (resource image, intcx, intcy, intw, inth, intcolor)
?? Imagefilledellipse () draws an elliptical image centered on cx and cy (0, 0 in the upper left corner of the image. W and h specify the width and height of the elliptic respectively. Fill the ellipse with color. If the call succeeds, TRUE is returned. if the call fails, FALSE is returned.

Imagearc -- draw an elliptical arc
?? Boolimagearc (resource image, intcx, intcy, intw, inth, ints, inte, intcolor)
?? Imagearc () uses cx and cy (0, 0 in the upper left corner of the image) as the center to draw an elliptical arc in the image. W and h specify the width and height of the elliptic respectively. the start and end points are specified by the s and e parameters respectively. 0 ° is located at three o'clock and painted clockwise.
?? Imagefilledarc -- draw an elliptical arc and fill it
?? Boolimagefilledarc (resource image, intcx, intcy, intw, inth, ints, inte, intcolor, intstyle)
?? In the image, the image filledarc () uses cx and cy (0 in the upper left corner of the image) to draw an elliptical arc. If the call succeeds, TRUE is returned. if the call fails, FALSE is returned. W and h respectively specify the width and height of the elliptic, and s and e parameters specify the start and end points with an angle. Style can be the values after bitwise OR (OR): IMG_ARC_PIE, IMG_ARC_CHORD, IMG_ARC_NOFILL, and IMG_ARC_EDGED. IMG_ARC_PIE and IMG_ARC_CHORD are mutually exclusive. IMG_ARC_CHORD only uses a straight line to connect the start and end points. IMG_ARC_PIE generates a circular boundary (if both are used, IMG_ARC_CHORD takes effect ). IMG_ARC_NOFILL indicates that the arc or string has only the contour and is not filled. IMG_ARC_EDGED indicates that a straight line is used to connect the start and end points to the center. it is a good way to draw the outline of a pie chart with IMG_ARC_NOFILL (instead of filling)

6. draw text in the image

Imagestring -- draw a line of strings horizontally

?? Syntax: boolimagestring (resource image, intfont, intx, inty, string s, intcol)
?? Imagestring () uses the col color to draw string s to the x and y coordinates of the image (this is the coordinate of the upper left corner of the string, and the upper left corner of the entire image is 0, 0 ). If the font is 1, 2, 3, 4, or 5, the built-in font is used.
?? Imagestringup -- draw a line of string vertically
?? Syntax: boolimagestringup (resource image, intfont, intx, inty, string s, intcol)
?? Imagestring () uses the col color to vertically draw string s to the x and y coordinates of the image (the upper left corner of the image is 0, 0 ). If the font is 1, 2, 3, 4, or 5, the built-in font is used.

Imagechar-draw a character horizontally
?? Syntax: boolimagechar (resource image, intfont, intx, inty, string c, intcolor)
?? Imagechar () draws the first character of string c in the image. the upper left corner of the string is x, y (the upper left corner of the image is 0, 0), and the color is color. If the font is 1, 2, 3, 4, or 5, use the built-in font (larger numbers correspond to larger fonts ).
?? Imagecharup -- draw a character vertically
?? Syntax: boolimagecharup (resource image, intfont, intx, inty, string c, intcolor)
?? Imagecharup () draws character c vertically on the image, which is located in x, y (0, 0 in the upper left corner of the image) and color. If the font is 1, 2, 3, 4, or 5, the built-in font is used.
?? Imagettftext -- use TrueType to write text to the image
?? Syntax: array imagettftext (resource image, float size, float angle, intx, inty, intcolor, string fontfile, string text)

Example:

 

The above introduces the dynamic image processing-php (34), including the content, hope to be helpful to friends who are interested in PHP tutorials.

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.