This article brings the content is about the PHP implementation to generate mixed verification code and image Verification code and test (code), there is a certain reference value, the need for friends can refer to, I hope to help you.
Generate a hybrid verification code and encapsulate it into a function, file name: buildVerifyCode.func.php
Range (' A ', ' Z ') generates an indexed array of contents in parentheses as a key value//array_merge ($array 1, $array 2) merges the key values in two arrays, generates a new indexed array//array_flip () will enclose the contents of the parentheses, Key name and key value//array_rand ($array, $length) randomly takes the key name of the $length length in the $array as the key value of the new array, generates an indexed array//join (", $array) to the value in the null connection array, Generates a string as the contents of an array
<?phpfunction Buildverifycode ($type =2, $length =4) {switch ($type) {case 0: $string =join (", Array_rand (range (0,9), $ length)); Break;case 1: $string =join ("", Array_rand (Array_flip (range (' A ', ' Z '), Range (' A ', ' Z ')), $length ); Break;case 2: $string =join ("", Array_rand (Array_flip (' A ', ' Z '), Range (' A ', ' Z '), Range (Array_merge)), $ length)); break;} return $string;}
Test generated verification code is correct, file name: getcode.php
<?phprequire ' buildVerifyCode.func.php '; echo buildverifycode ();//$fontfiles =[' MSYH.TTC ', ' MSYHBD.TTC ', ' MSYHL.TTC ', ' SIMSUN.TTC ', ' SITKA.TTC '];//$fontfile = $fontfiles [Mt_rand (0,count ($fontfiles)-1)];//Var_dump ($ Fontfile);
Generate an image verification code, the specific comment is empty and then write, file name: getVerifyCodeImg.func.php
<?php$width=100; $height =30;//Create canvas, default background black, Rgb0,0,0$image=imagecreatetruecolor ($width, $height);//Create white, easy to overlay canvas $ White=imagecolorallocate ($image, 255,255,255);//Create a white rectangle to cover the original canvas Imagefilledrectangle ($image, 2, $width-2, $height, $white); require ' buildVerifyCode.func.php '; $type =2; $length =4; $verifyCode =buildverifycode ($type, $length); for ($i = 0; $i < $length; $i + +) {$color =imagecolorallocate ($image, Mt_rand (50,90), Mt_rand (80,200), Mt_rand (90,150)); $size = Mt_rand (14,16); $angle =mt_rand ( -15,15); $x = ($i *100/5) + $size; $y =mt_rand (20,25); $fontfiles =[' MSYH.TTC ', ' MSYHBD.TTC ', ' MSYHL.TTC ', ' SIMSUN.TTC ', ' SITKA.TTC ']; $fontfile = ". /fonts/". $fontfiles [Mt_rand (0,count ($fontfiles)-1)]; $text =substr ($verifyCode, $i, 1); Imagettftext ($image, $size, $ Angle, $x, $y, $color, $fontfile, $text);} $pixel =120;if ($pixel) {$pixelcolor =imagecolorallocate ($image, Mt_rand (150,170), Mt_rand (100,140), Mt_rand (90,160)) ; for ($i =0; $i < $pixel; $i + +) {Imagesetpixel ($image, Mt_rand (0, $width-1), Mt_rand (0, $height-1), $pixelcolor);}} $line =4;if ($line) {for ($i =0; $i <$Line; $i + +) {imageline ($image, Mt_rand (0, $width-1), Mt_rand (0, $height-1), Mt_rand (0, $width-1), Mt_rand (0, $height-1), $pixelcolor);}} Header (' content-type:image/png '); Imagepng ($image); Imagedestroy ($image);