简单的验证码识别 发表于 2016-08-16 | 分类于 PHP日记 | 评论数: | 阅读次数: 本文字数: 3.7k | 阅读时长 ≈ 7 分钟 我直接贴代码了,发现验证码识别已经可以克服各种干扰,包括discuz系统的那种彩色,字体大小倾斜度位置都不一样的验证码都可以识别,而且准确度非常高。 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197<?phpinclude("curl.php");//这是一个抓去https链接的库,和内容不相干 /*通过计算发现验证码的背景灰度都为1,字体都为2,字母位置大小都不变变的只是颜色,和背景,没有线条和点的干扰,字体固定,识别度可以达到100%,是最简单的验证码*/// header("Content-type: image/gif");function ccc($file){$im = @imagecreatefromgif($file) or die("Cannot Initialize new GD image stream");$width=imagesx($im);$height=imagesy($im);for ($y=0; $y < $height; $y++) { for ($x=0; $x < $width; $x++) { $rgb = ImageColorAt($im, $x, $y); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> 8) & 0xFF; $b = $rgb & 0xFF; $Gray = ($r^2.2 * 0.2973 + $g^2.2 * 0.6274 + $b^2.2 * 0.0753)^(1/2.2); $res\[$x\]\[$y\] = $Gray; }}// imagepng($im);imagedestroy($im);$txt = "";for ($i=1; $i <6 ; $i++) { $n = zzz(xxx($i,$res)); if($n === false){ return false; } $txt .= $n;}return $txt;}file\_put\_contents("verify.gif",curlPost("https://hao.mhcf.net/rand.php"));echo '<img src="verify.gif?rand='.rand(1000,9999).'" />';var_dump(ccc("verify.gif"));// (8,5)-(x+8,y+10)为第一个字母// (x+17,y+10) 第二个字母function xxx($n,$res){ $n = (int)$n; if($n > 0){ $n =$n - 1; } // global $res; $x_end = 16 + 9*$n; $r = "";for ($y = 5; $y < 15; $y++) { for ($x = 8 + 9*$n; $x < $x_end; $x++) { $r .= $res\[$x\]\[$y\]; } if($y < 14) $r .= "\\n";}return $r;} function zzz($imgStr){ $data = data(); foreach ($data as $key=>$value) { $m = 图片匹配度($value,$imgStr); // echo $m."\\n"; if($m > 90){ return $key; } } return false;}function 图片匹配度($图片,$验证码){ $arr = explode("\\n",str_replace("\\r","",$图片)); $arr2= explode("\\n",str_replace("\\r","",$验证码)); $length = count($arr); $n=0; for ($i=0; $i < $length; $i++) { $n += match($arr\[$i\],$arr2\[$i\]); } return $n/$length;}function match($a,$b){ $la=strlen($a); $lb=strlen($b); $length=$la < $lb ? $la : $lb; $m=0; $t=0; for ($i=0; $i < $length; $i++) { if($a\[$i\]===$b\[$i\]){ $m++; } $t++; } $t += ($la-$lb) > 0 ? ($la-$lb) : ($lb - $la); return ($m/$t)*100;}function data(){$res\[0\]="11122111112222111221122122111122221111222211112222111122122112211122221111122111";$res\[1\]="11122111112221111222211111122111111221111112211111122111111221111112211112222221";$res\[2\]="11222211122112212211112211111122111112211111221111122111112211111221111122222222";$res\[3\]="12222211221112211111112211111221111222111111122111111122111111222211122112222211";$res\[4\]="11111221111122211112222111221221122112212211122122222222111112211111122111111221";$res\[5\]="22222221221111112211111122122211222112211111112211111122221111221221122111222211";$res\[6\]="11222211122112212211112122111111221222112221122122111122221111221221122111222211";$res\[7\]="22222222111111221111112211111221111122111112211111221111122111112211111122111111";$res\[8\]="11222211122112212211112212211221112222111221122122111122221111221221122111222211";$res\[9\]="11222211122112212211112222111122122112221122212211111122121111221221122111222211";return $res;} 打赏 微信支付 支付宝