PHP uses GD library to generate verification code to draw Chinese characters on image

Source: Internet
Author: User
Tags dashed line transparent color webp

PHP is not limited to creating HTML output, it can also create and process images in a variety of formats including GIF, PNG, JPEG, WBMP, and XPM. More conveniently, PHP can directly output the image data stream to the browser. To use the image processing function in PHP, you need to compile PHP together with the GD Library. The GD library and PHP may require additional libraries, depending on the format of the image you are working with. You can use the image functions in PHP to get the size of images in the following formats: JPEG, GIF, PNG, SWF, TIFF, and JPEG2000. If combined with an exif extension, you can manipulate the information stored in the header of the JPEG and TIFF image files so that the metadata generated by the digital camera can be obtained. EXIF-related functions do not need to be used by the GD library.

You can use phpinfo.php to obtain the installation configuration information for the GD library:

Formats supported by GD

format Support Read Support for writing Notes
Jpeg TRUE TRUE GD 1.8+
Gif TRUE TRUE The version between GD 1.6 and GD 2.0.28 does not support
XBM TRUE TRUE PHP 4.0.1 started to support read, PHP 5.0.0 began to support writing
XPM TRUE FALSE PHP 4.0.1 started to support reading. Not available on Windows platforms. GD libraries that require PHP bindings
WebP TRUE TRUE GD 2.1+, PHP 5.5+

GD and image processing functions

Gd_info-get information about the currently installed GD library
? getimagesize-Get Image size
? getimagesizefromstring-getting image size information from a string
image_type_to_extension-get the file suffix of the image type
? image_type_to_mime_type-gets the MIME type of the image type returned by Getimagesize,exif_read_data,exif_thumbnail,exif_imagetype
image2wbmp-exporting images to a browser or file in WBMP format
? imageaffine-returns the image after affine transformation, and the clipping region is optional
? imageaffinematrixconcat-concat matrices (as in doing many ops in one go)
? Imageaffinematrixget-return an image containing the affine tramsformed src image, using a optional clipping area
? imagealphablending-set the color-blending mode of the image
? imageantialias-whether to use anti-aliasing (antialias) function
imagearc-to draw an elliptical arc.
? imagechar-to draw a character horizontally
? imagecharup-to draw a character vertically
? imagecolorallocate-assigning colors to an image
? imagecolorallocatealpha-assigning color + alpha to an image
? imagecolorat-gets the color index value of a pixel
? imagecolorclosest-gets the index value of the color closest to the specified color
? imagecolorclosestalpha-gets the color that is closest to the specified color and transparency
Imagecolorclosesthwb-gets the black and white index of the color closest to the given colour
imagecolordeallocate-to cancel the assignment of image colors
? imagecolorexact-gets the index value of the specified color
Imagecolorexactalpha-gets the index value of the specified color plus transparency
? imagecolormatch-makes the color of the palette version in an image more compatible with the true color version
Imagecolorresolve-gets the index value of the specified color or the closest possible alternative value
Imagecolorresolvealpha-gets the index value of the specified color + alpha or the nearest alternative value that may be obtained
? imagecolorset-sets the color for the specified palette index
? imagecolorsforindex-gets the color of an index
? imagecolorstotal-gets the number of colors in the palette of an image
? imagecolortransparent-defines a color as a transparent color
? imageconvolution-apply a 3x3 convolution matrix with a coefficient div and offset
? imagecopy-part of a copy image
? imagecopymerge-copy and merge part of the image
? imagecopymergegray-copy and merge part of the image with grayscale
? imagecopyresampled-resample Copy part of the image and resize it
? imagecopyresized-copy part of the image and resize it
? imagecreate-Create a new palette-based image
? imagecreatefromgd2-create a new image from a GD2 file or URL
? imagecreatefromgd2part-create a new image from a given GD2 file or part of a URL
? imagecreatefromgd-create a new image from a GD file or URL
? imagecreatefromgif-creates a new image from a file or URL.
? imagecreatefromjpeg-creates a new image from a file or URL.
? imagecreatefrompng-creates a new image from a file or URL.
? imagecreatefromstring-create a new image from an image stream in a string
? imagecreatefromwbmp-creates a new image from a file or URL.
? imagecreatefromwebp-creates a new image from a file or URL.
? imagecreatefromxbm-creates a new image from a file or URL.
? imagecreatefromxpm-creates a new image from a file or URL.
? imagecreatetruecolor-Create a new true color image
? Imagecrop-crop an image using the given coordinates and size, x, y, Width and height
? Imagecropauto-crop an image automatically using one of the available modes
? imagedashedline-draw a dashed line
? imagedestroy-destroying an image
? imageellipse-Draw an Ellipse
? imagefill-Area Fill
? imagefilledarc-draws an elliptical arc and fills
? imagefilledellipse-draw an Ellipse and fill
? imagefilledpolygon-draw a polygon and fill
? imagefilledrectangle-draw a rectangle and fill
The imagefilltoborder-area fills the bounds of the specified color
? imagefilter-using filters on images
? imageflip-flips an image using a given mode
? imagefontheight-Get Font height
? imagefontwidth-Get Font width
? imageftbbox-gives a text box that uses the FreeType 2 font
? imagefttext-writing text to an image using FreeType 2 font
imagegammacorrect-apply gamma correction to GD image
? imagegd2-output GD2 images to a browser or file
imagegd-to export GD images to a browser or file
imagegif-output images to a browser or file.
? imagegrabscreen-captures the whole screen
? imagegrabwindow-captures a Window
? imageinterlace-activates or disables interlaced scanning
imageistruecolor-Check if the image is a true color image
imagejpeg-output images to a browser or file.
? imagelayereffect-set Alpha color-blending flag to use bound LIBGD layering effects
? imageline-Draw a line segment
? imageloadfont-loading a new font
? imagepalettecopy-copy the palette from one image to another
? Imagepalettetotruecolor-converts a palette based image to True Color
imagepng-exporting images to a browser or file in PNG format
? imagepolygon-Draw a polygon
? imagepsbbox-gives a text box with a PostScript Type1 font
imagepsencodefont-change the character encoding vector in a font
? imagepsextendfont-expanding or streamlining fonts
? imagepsfreefont-releases the memory occupied by a PostScript Type 1 font
? imagepsloadfont-loading a PostScript type 1 font from a file
? imagepsslantfont-Tilt A font
imagepstext-use PostScript Type1 font to draw text strings on the image
? imagerectangle-Draw a rectangle
? imagerotate-rotate an image with a given angle
? imagesavealpha-set tags to save full alpha channel information when you save a PNG image (as opposed to a single transparent color)
? Imagescale-scale an image using the given new width and height
? imagesetbrush-set the brush image for line drawing
? Imagesetinterpolation-set the Interpolation method
? imagesetpixel-draw a single pixel
? imagesetstyle-set the style of the drawing line
? imagesetthickness-set the width of the drawing line
? imagesettile-setting the map for filling
? imagestring-to draw a line of string horizontally
? imagestringup-to draw a line of string vertically
? imagesx-Get Image width
? imagesy-Get Image Height
? imagetruecolortopalette-converting a true color image to a palette image
Imagettfbbox-get the range of text using TrueType fonts
? imagettftext-writing text to an image with TrueType fonts
? imagetypes-returns the type of image supported by the current PHP version
imagewbmp-exporting images to a browser or file in WBMP format
? imagewebp-output an image of WebP format to a browser or file
? imagexbm-output XBM images to a browser or file
? iptcembed-embed binary IPTC data into a JPEG image
? iptcparse-to parse a binary IPTC block into a single tag
jpeg2wbmp-Convert a JPEG image file to a WBMP image file
png2wbmp-Convert a PNG image file to a WBMP image file

The GD library can be used under both Linux and Windows, and the functions are exactly the same, but the graphics coordinates are offset. If the system is ported to each other, the interface must be re-viewed.

Drawing Chinese on an image

<? php
     // Define output as image type
     header ("content-type: image / gif");

     // create canvas
     $ im = imagecreate (400, 200);
     // background
     imagecolorallocate ($ im, 225, 66, 255);
     // text color
     $ text_color = imagecolorallocate ($ im, 233, 14, 91);
     $ motto = "Test String Changbai Mountain";
     // imagestring default English encoding, only UTF-8 is supported
     // imagestring ($ im, 2, 0, 0, $ motto, $ text_color);
     // When the code file is:
     // ANSI encoding, need conversion
     // UTF-8 encoding, no conversion required
     // $ motto = iconv ("gb2312", "utf-8", $ motto);
     imageTTFText ($ im, 20, 0, 0, 100, $ text_color, "c: /windows/fonts/simhei.ttf", $ motto);
     imagegif ($ im);
     imagedestroy ($ im);

Generate Verification Code

<? php
session_start ();

header ("Content-type: image / png");
$ imWidth = 70;
$ imHeight = 18;
$ im = imagecreate ($ imWidth, $ imHeight);
imagecolorallocate ($ im, 255, 255, 255);

srand (microtime () * 100000);
for ($ ix = 0; $ ix <4; $ ix ++)
     $ number. = dechex (mt_rand (0, 15));

$ _SESSION [check_check] = $ number;

for ($ ix = 0; $ ix <4; $ ix ++)
     $ x = $ imWidth * $ ix / 4 + mt_rand (1, 8);
     $ y = mt_rand (1, $ imHeight / 4);
     $ ft = mt_rand (3, 5);
     $ cor = imagecolorallocate ($ im, mt_rand (0, 100), mt_rand (0, 150), mt_rand (0, 200));
     imagestring ($ im, $ ft, $ x, $ y, $ _SESSION [check_check] [$ ix], $ cor);
imagepng ($ im);
imagedestroy ($ im);

Copyright NOTICE: This article is the original article of Bo-Lang's work room, without the permission of Bo Master not to reprint.

PHP uses GD library to generate verification code to draw Chinese characters on image

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