This article to you to share is the use of PHP's GD library to implement the verification code function and all the code, the need for small partners can refer to the following
<?php require_once ' string.func.php ';//verify code via GD Library/** * Add validation text * @param int $type * @param int $length */function Buildrand Omstring ($type =1, $length =4) {$row = '; if ($type ==1) {$row =join (", Range (0, 9)); }else if ($type ==2) {$row =join (",", Array_merge (Range (' A ', ' Z '), Range (' A ', ' Z '))); }else if ($type ==3) {$row =join (",", Array_merge (Range (' A ', ' Z '), Range (' A ', ' Z '), range (0, 9)); }; $row =str_shuffle ($row); $row =substr ($row, 0, $length); return $row;} /** * generates thumbnails * @param int $type//contains numbers or English * @param int $length How many characters * @param int $pixel The density of the interfering dots * @param int $DST The _h of the interference line Degree * @param string//verification code name in $_session */function verifyimage ($type =1, $length =4, $pixel =0, $line =0, $sess _name = "Verify") {//session_start (); Create canvas $width = 100; $height = 40; $image = Imagecreatetruecolor ($width, $height); $white = Imagecolorallocate ($image, 255, 255, 255); $black = imagecolorallocate ($image, 0, 0, 0); Fills the canvas with a filled rectangle imagefilledrectangle ($image, 1, 1, $width-2, $height-2, $white); $chars = buildrandomstring ($type, $length); $_session [$sess _name] = $chars; $fontfiles = Array ("Msyh. TTF "," MSYHBD. TTF "," Simli. TTF "," SimSun. TTC "," simyou. TTF "," stzhongs. TTF "); $fontfiles = Array ("Simkai. TTF "); Because the font file is larger, just keep a font, if the students need to add their own fonts, fonts in your computer's Fonts folder, directly run input fonts can see the corresponding font for ($i = 0; $i < $length; $i + +) {$s ize = Mt_rand (14, 18); $angle = Mt_rand (-15, 15); $x = 5 + $i * $size; $y = Mt_rand (20, 26); $fontfile = ". /fonts/". $fontfiles [Mt_rand (0, Count ($fontfiles)-1)]; $color = Imagecolorallocate ($image, Mt_rand (), Mt_rand (90, 180), mt_rand (+); $text = substr ($chars, $i, 1); Imagettftext ($image, $size, $angle, $x, $y, $color, $fontfile, $text); if ($pixel) {for ($i = 0; $i <; $i + +) {Imagesetpixel ($image, Mt_rand (0, $width-1), Mt_rand (0, $height-1), $black); }} if ($line) {for ($i = 1; $i < $line; $i + +) {$color = ImagecolOrallocate ($image, Mt_rand, Mt_rand, Mt_rand (90, 180)); Imageline ($image, Mt_rand (0, $width-1), Mt_rand (0, $height-1), Mt_rand (0, $width-1), Mt_rand (0, $height -1), $color); }} header ("Content-type:image/gif"); Imagegif ($image); Imagedestroy ($image);}
Main points:
1. If Session_Start () is not stated earlier, it is necessary to declare;
2, the font can be downloaded in the CMF input fonts to their own definition of the Fonts folder;
3. $_session [$sess _name] can be compared by $_post to obtain the verification code entered by the user.
Summary: The above is the entire content of this article, I hope to be able to help you learn.