九天鹰
 
  
专业版
Level: 29
HP: 578 / 795
MP: 154 / 927
Exp: 40%
帖子: 231
积分: 609
注册时间: 2006-01-23
|
<?
/**
* 汉字验证码
* 2007-12-12 2:20 zer0r2y
*/
$x_size = 220;
$y_size = 60;
$font = 'simsun.ttc';
// 生成数字和字母的验证码
$ychar = "1,1,3,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,N,P,Q,R,S,T,U,V,W,X,Y,Y";
$list = explode(",",$ychar);
$c = array();
for($i = 0; $i < 4; $i++){
$randnum = rand(0,35);
$c[$i] = $list[$randnum];
}
// 生成汉字验证码
$k[0] = num_rand();
$k[1] = num_rand();
$k[2] = num_rand();
$k[3] = num_rand();
// 生成数字+字母+汉字的字符验证码
$vcode[0] = iconv('gb2312','UTF-8',$k[0]);
$vcode[1] = iconv('gb2312','UTF-8',$k[1]);
$vcode[2] = iconv('gb2312','UTF-8',$k[2]);
$vcode[3] = iconv('gb2312','UTF-8',$k[3]);
$vcode[4] = $c[0];
$vcode[5] = $c[1];
$vcode[6] = $c[2];
$vcode[7] = $c[3];
// 随即取出6个字符,得到最终的验证码字符
srand ((float) microtime() * 10000000);
$rand_keys = array_rand ($vcode, 6);
// 生成图片
$pic = imagecreate($x_size,$y_size);
$background_color = imagecolorallocate ($pic, 255, 255, 255);
$black = imagecolorallocate($pic, 0, 0, 0);
// 加入干扰码
for($i = 0; $i < 6; $i++){
$fontcolor=imagecolorallocate($pic, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagearc($pic, mt_rand(-10, $x_size), mt_rand(-10, $y_size), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $fontcolor);
}
for($i = 0; $i < 60; $i++){
$fontcolor=imagecolorallocate($pic, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagesetpixel($pic, mt_rand(0, $x_size), mt_rand(0, $y_size), $fontcolor);
}
// 填充验证码字符
for($i = 0; $i < 6; $i++) {
$fontcolor = imagecolorallocate($pic, mt_rand(0,120), mt_rand(0,120), mt_rand(0,120));
imagettftext($pic, mt_rand(20, 24), mt_rand(-60, 60), 36*$i+10, mt_rand(30, 35), $fontcolor, $font, $vcode[$rand_keys[$i]]);
}
imagerectangle($pic, 0, 0, $x_size - 1, $y_size - 1,$black);
Imagepng($pic);
ImageDestroy($pic);
function c2ch($num){
$ch_h = chr(substr($num, 0, 2) + 160);
$ch_l = chr(substr($num, 2, 2) + 160);
return $ch_h.$ch_l;
}
function num_rand(){
mt_srand((double)microtime() * 1000000);
$d = mt_rand(16, 36);
$n = mt_rand(1, 22);
return c2ch($d * 100 + $n);
}
?>
关键词:程序
汉字
数字
验证
字母
|
|
|
| |
|