识别难度更大

识别难度更大

<?php

function getCode($length = 32, $mode = 0) {
switch ($mode) {
case '1' :
$str = '123456789';
break;
case '2' :
$str = 'abcdefghijklmnopqrstuvwxyz';
break;
case '3' :
$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
case '4' :
$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
break;
case '5' :
$str = 'ABCDEFGHIJKLMNPQRSTUVWXYZ123456789';
break;
case '6' :
$str = 'abcdefghijklmnopqrstuvwxyz1234567890';
break;
case '7' ://中文验证码
break;
default :
$str = 'ABCDEFGHIJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789';
break;
}
$result = '';

for($i = 0; $i < $length; $i ++) {
if ($mode == 7) {
$str [$i] = chr ( mt_rand ( 176, 215 ) ) . chr ( mt_rand ( 161, 249 ) );
$str [$i] = iconv ( "GB2312", "UTF-8", $str [$i] ); //imagettftext是utf-8的,所以先转换下
$result .= $str [$i];
} else {
$l = strlen ( $str ) - 1;
$num = mt_rand ( 0, $l );
$result .= $str [$num];
}
}
return $result;
}

//建立验证图片
function createAuthNumImg($randStr, $fontName, $imgW = 100, $imgH = 40) {
header ( "content-type: image/png" );
$image = imagecreate ( $imgW, $imgH );
$fontSize = mt_rand(17,20);//字号
//$green = imagecolorallocate($image,0x6b,0xc1,0x46);

$gray = imagecolorallocate ( $image, 255, 255, 255 ); //灰色
$red  = imagecolorallocate ( $image, 255, 102, 204 );//粉色
$blue = imagecolorallocate($image,0x53,0x68,0xbd);
$colors = array($red, $gray, $blue);

$color_b = imagecolorallocate ( $image, 0, 0, 0 ); //黑色
$color_d = imagecolorallocate ( $image,98,178, 28 ); //绿色
$color_e = imagecolorallocate ( $image,38,98, 223 ); //蓝色
$color_c = imagecolorallocate ( $image,255,0, 0 ); //红色
$color_f = imagecolorallocate ( $image,128,0, 255 ); //紫色
$color_g = imagecolorallocate ( $image,0,128, 128 ); //
$color = array($color_b, $color_c, $color_d,$color_e);

shuffle($color);

$colorrnd=imagecolorallocate ( $image,mt_rand(0,255),mt_rand(0,255), mt_rand(0,255) );
for($i = 0; $i < 1000; $i ++) { //绘背景干扰点
//imagesetpixel ( $image, mt_rand ( 0, $imgW ), mt_rand ( 0, $imgH ), $colors[rand(0,count($colors)-1)]);
}
imagerectangle ( $image, 0, 0, $imgW - 1, $imgH - 1, $color_b );//绘制边框

$str1=substr($randStr,0,1);
$str2=substr($randStr,1,1);
$str3=substr($randStr,2,1);
$str4=substr($randStr,3,1);
$range=mt_rand(-20,20);
//imagettftext ( $image, $fontSize, 5, 3, 25, $color_b, $fontName, $randStr);///将验证字符绘入图片 字符旋转
for($i=1;$i<=4;$i++) {
$p=$i-1;
$str=$randStr["$p"];

//imagettftext ( $image, mt_rand(17,20), mt_rand(-20,20), $i*12, mt_rand(20,25), $color[mt_rand(0,count($color)-1)], $fontName, $str);///将验证字符绘入图片 字符旋转
imagettftext ( $image, mt_rand(17,20), mt_rand(-20,20), $i*12, mt_rand(20,25), $color[$i-1], $fontName, $str);///将验证字符绘入图片 字符旋转

}
//imagettftext ( $image, mt_rand(17,20), mt_rand(-20,20), mt_rand(2,5), mt_rand(20,25), $color_b, $fontName, $str1);///将验证字符绘入图片 字符旋转
//imagettftext ( $image, mt_rand(17,20), mt_rand(-20,20), mt_rand(16,18), mt_rand(20,25), $color_c, $fontName, $str2);///将验证字符绘入图片 字符旋转

//imagettftext ( $image, mt_rand(17,20), mt_rand(-20,20), mt_rand(28,30), mt_rand(20,25), $color_d, $fontName, $str3);///将验证字符绘入图片 字符旋转

//imagettftext ( $image, mt_rand(17,20), mt_rand(-20,20), mt_rand(36,38), mt_rand(20,25), $color_e, $fontName, $str4);///将验证字符绘入图片 字符旋转

for($i=0; $i<4; $i++){ //绘背景干扰线
//imageline($image, mt_rand(0,5), mt_rand(6,18), mt_rand(65,$imgW), mt_rand(6,$imgH), $colorrnd);//一条干扰线
$h=30;
$h1=rand(-5,5);
$h2=rand(-1,1);
$w2=rand(10,15);
$h3=rand(4,6);
$w=75;

for($i=-$w/2;$i<$w/2;$i=$i+0.1)
{
$y=$h/$h3*sin($i/$w2)+$h/2+$h1;
imagesetpixel($image,$i+$w/2,$y,$color[$p]);
$h2!=0?imagesetpixel($image,$i+$w/2,$y+$h2,$color[$p]):null;
}
}

imagepng ( $image );
imagedestroy ( $image );
}

session_start ();
$verifyCode = GetCode ( 4 );
$_SESSION ['VERIFY_CODE'] = $verifyCode ;
createAuthNumImg ( $verifyCode, "font.ttf", 75, 30); //字体存放路径,如果你没有文件就去C:\WINDOWS\Fonts文件中找一个吧。
/** 问答模式
$a=GetCode(2,1);
$b=GetCode(1,1);
$passPort = $a."+".$b."=?";
$verifyCode = $a+$b;

$_SESSION ['VERIFY_CODE'] = $verifyCode ;
createAuthNumImg ( $passPort, "font.ttf", 75, 30);
*/
?>


 

最后更新于 2022年7月8日

超强php验证码升级版
标签: