/**
* _code () is a CAPTCHA function
* @access Public
* @param int $_width Indicates the length of the Verification code
* @param int $_height Indicates the height of the verification code
* @param int $_rnd_code Indicates the number of digits of the verification code
* @param bool $_flag Indicates if the verification code requires a border
* @return void A verification code is generated after this function is executed
*/
function _code ($_width = 75,$_height = 25,$_rnd_code = 4,$_flag = False) {
Create Random Code
for ($i =0; $i <$_rnd_code; $i + +) {
$_nmsg. = Dechex (Mt_rand (0,15));
}
Save in session
$_session[' Code ' = $_nmsg;
Create an image
$_img = Imagecreatetruecolor ($_width,$_height);
White
$_white = Imagecolorallocate ($_img,255,255,255);
Fill
Imagefill ($_img,0,0,$_white);
if ($_flag) {
Black, border
$_black = Imagecolorallocate ($_img,0,0,0);
Imagerectangle ($_img,0,0,$_width-1,$_height-1,$_black);
}
6 lines are drawn immediately
for ($i =0; $i <6; $i + +) {
$_rnd_color = Imagecolorallocate ($_img,mt_rand (0,255), Mt_rand (0,255), Mt_rand (0,255));
Imageline ($_img,mt_rand (0,$_width), Mt_rand (0,$_height), Mt_rand (0,$_width), Mt_rand (0,$_height), $_rnd_color);
}
Immediately snowflakes
for ($i =0; $i <100; $i + +) {
$_rnd_color = Imagecolorallocate ($_img,mt_rand (200,255), Mt_rand (200,255), Mt_rand (200,255));
Imagestring ($_img,1,mt_rand (1,$_width), Mt_rand (1,$_height), ' * ', $_rnd_color);
}
Output Verification Code
for ($i =0; $i <strlen ($_session[' code '); $i + +) {
$_rnd_color = Imagecolorallocate ($_img,mt_rand (0,100), Mt_rand (0,150), Mt_rand (0,200));
Imagestring ($_img,5, $i *$_width/$_rnd_code+mt_rand (1,10), Mt_rand (1,$_HEIGHT/2), $_session[' code '] [$i],$_rnd_ color);
}
Output image
Header (' content-type:image/png ');
Imagepng ($_IMG);
Destroyed
Imagedestroy ($_IMG);
}
PHP Generate Verification Code