Home > Backend Development > PHP Tutorial > PHP uses the GD library to generate verification codes and draw Chinese characters on images

PHP uses the GD library to generate verification codes and draw Chinese characters on images

WBOY
Release: 2016-08-08 09:20:54
Original
1177 people have browsed it

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. Even more conveniently, PHP can directly output the image data stream to the browser. To use image processing functions in PHP, you need to GD library to compile PHP. The GD library and PHP may require other libraries, depending on the image format you want to process. 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 union exif Extensions used together, you can manipulate the information stored in the header of JPEG and TIFF image files, so that you can obtain the metadata generated by digital cameras. Exif related functions can be used without the GD library.

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


GD supported formats

PNG
Format supports reading Get support writing notes
JPEG TRUE TRUE GD 1.8+
TRUE TRUE
GIF TRUE TRUE Versions between GD 1.6 and GD 2.0.28 are not supported
XBM TRUE TRUE PHP 4.0.1 Start Support reading, PHP 5.0.0 starts to support writing
XPM TRUE FALSE PHP 4.0.1 starts to support reading. Not available on Windows platforms. GD library that requires PHP binding
WBMP TRUE TRUE GD 1.8+
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 the image size
?getimagesizefromstring — Get image size information from a string
?image_type_to_extension — Get the file suffix of the image type
?image_type_to_mime_type — Get getimagesize, exif_read_data , exif_thumbnail, exif_imagetype MIME type of the image type returned
?image2wbmp — Output the image to a browser or file in WBMP format
?imageaffine — Return the image after affine transformation, with optional clipping area
?imageaffinematrixconcat — Concat two matrices (as in doing many ops in one go)
?imageaffinematrixget — Return an image containing the affine tramsformed src image, using an optional clipping area
?imagealphablending — Set the blending mode of the image
?imageantialias — Whether to use anti-aliasing ( antialias) functions
?imagearc — Draw an elliptical arc
?imagechar — Draw a character horizontally
?imagecharup — Draw a character vertically
?imagecolorallocate — Assign a color to an image
?imagecolorallocatealpha — Assign a color + alpha to an image
?imagecolorat — Get the color index value of a certain pixel
?imagecolorclosest — Get the index value of the color closest to the specified color
?imagecolorclosestalpha — Get the color closest to the specified color plus transparency
?imagecolorclosesthwb — Get the given color The black and white index of the color closest to the color
?imagecolordeallocate — Cancel the allocation of image color
?imagecolorexact — Get the index value of the specified color
?imagecolorexactalpha — Get the index value of the specified color plus transparency
?imagecolormatch — Make an image The color of the medium palette version better matches the true color version
?imagecolorresolve — Get the index value of the specified color or the closest alternative value possible
?imagecolorresolvealpha — Get the index value of the specified color + alpha or possible value The closest replacement value
?imagecolorset — Set the color for the specified palette index
?imagecolorsforindex — Get the color of an index
?imagecolorstotal — Get the number of colors in the palette of an image
?imagecolortransparent — Convert a certain A color is defined as a transparent color
?imageconvolution — Apply a 3x3 convolution matrix with coefficients div and offset
?imagecopy — Copy a part of the image
?imagecopymerge — Copy and merge a part of the image
?imagecopymergegray — Copy and merge with grayscale Part of an image
?imagecopyresampled — Resample a portion of an image and resize it
?imagecopyresized — Copy a portion of an 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 part of the given GD2 file or URL
?imagecreatefromgd — Create a new image from a GD file or URL
?imagecreatefromgif — Create a new image from a file or URL.
?imagecreatefromjpeg — Create a new image from a file or URL.
?imagecreatefrompng — Create a new image from a file or URL.
?imagecreatefromstring — Create a new image from an image stream in a string
?imagecreatefromwbmp — Create a new image from a file or URL.
?imagecreatefromwebp — Create a new image from a file or URL.
?imagecreatefromxbm — Create a new image from a file or URL.
?imagecreatefromxpm — Create 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 one Dashed line
?imagedestroy — Destroy an image
?imageellipse — Draw an ellipse
?imagefill — Area fill
?imagefilledarc — Draw an elliptical arc and fill it
?imagefilledellipse — Draw an ellipse and fill it
?imagefilledpolygon — Draw a polygon and fill it
?imagefilledrectangle — draw a rectangle and fill it
?imagefilltoborder — fill the area to the border of the specified color
?imagefilter — apply a filter to the image
?imageflip — Flips an image using a given mode
?imagefontheight — get the font height
?imagefontwidth — Get the font width
?imageftbbox — Give a text box using a FreeType 2 font
?imagefttext — Write text to an image using a FreeType 2 font
?imagegammacorrect — Apply gamma correction to a GD image
?imagegd2 — Output a GD2 image to Browser or file
?imagegd — Export GD image to browser or file
?imagegif — Export image to browser or file.
?imagegrabscreen — Captures the whole screen
?imagegrabwindow — Captures a window
?imageinterlace — Activate or disable interlacing
?imageistruecolor — Check whether the image is a true color image
?imagejpeg — Output the image to a browser or file.
?imagelayereffect — Set the alpha blending flag to use the bundled libgd layering effect
?imageline — Draw a line segment
?imageloadfont — Load a new font
?imagepalettecopy — Copy a palette from one image to another
?imagepalettetotruecolor — Converts a palette based image to true color
?imagepng — Output an image in PNG format to a browser or file
?imagepolygon — Draw a polygon
?imagepsbbox — Give a text box using PostScript Type1 font
?imagepsencodefont — Change the character encoding vector in a font
?imagepsextendfont — Extend or condense a font
?imagepsfreefont — Free up the memory occupied by a PostScript Type 1 font
?imagepsloadfont — Load a PostScript Type 1 font from a file
?imagepsslantfont — Skew a font
?imagepstext — Draw a text string on an image using a PostScript Type1 font
?imagerectangle — Draw a rectangle
?imagerotate — Rotate an image by a given angle
?imagesavealpha — Set a flag to save a PNG image intact when saving it Alpha channel information (as opposed to a single transparent color)
?imagescale — Scale an image using the given new width and height
?imagesetbrush — Set the brush image used to draw lines
?imagesetinterpolation — Set the interpolation method
?imagesetpixel — Draw A single pixel
?imagesetstyle — Set the style of the line drawing
?imagesetthickness — Set the width of the line drawing
?imagesettile — Set the texture used for filling
?imagestring — Draw a line of string horizontally
?imagestringup — Vertically Draw a line of string
?imagesx — Get the image width
?imagesy — Get the image height
?imagetruecolortopalette — Convert a true color image to a palette image
?imagettfbbox — Get the range of text using TrueType fonts
?imagettftext — Use TrueType Font writes text to images
?imagetypes — Returns the image types supported by the current PHP version
?imagewbmp — Outputs images in WBMP format to a browser or file
?imagewebp — Outputs images in 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 — Parse binary IPTC blocks into individual tags
?jpeg2wbmp — Convert JPEG image files to WBMP Image file
?png2wbmp - Convert PNG image file to WBMP image file

The GD library can be used under both Linux and Windows systems. The functions are exactly the same, but the graphics coordinates will be offset. If systems are migrated to each other, the interface must be reviewed again.

Draw Chinese on image

<?php
    // 定义输出为图像类型
    header("content-type:image/gif");       

    // 创建画布
    $im = imagecreate(400, 200);     
    // 背景
    imagecolorallocate($im, 225, 66, 255);
        
    // 文本颜色
    $text_color = imagecolorallocate($im, 233, 14, 91);    
    $motto = "Test String 长白山";
    //imagestring 默认英文编码,只支持UTF-8
    //imagestring($im, 2, 0, 0, $motto, $text_color);
    
    //当代码文件为:
    //ANSI编码,需要转换
    //UTF-8编码,不需要转换
    //$motto = iconv("gb2312", "utf-8", $motto);
    imageTTFText($im, 20, 0, 0, 100, $text_color, "c:/windows/fonts/simhei.ttf", $motto);  
    
    imagegif($im);
    imagedestroy($im);     
?>
Copy after login

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);
?>
Copy after login

Copyright statement: This article is a blog Original article by Zhulangya Studio, not yet published No reproduction is allowed without the permission of the blogger.

The above introduces PHP's use of the GD library to generate verification codes and draw Chinese characters on images, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template