本篇文章带大家介绍超简单的php验证码识别。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。
超简单的php验证码识别网站的登陆页、注册页等等等到处都是验证码,然而你的验证码真的安全么?也许只需要一段简单的小程序,你的验证码就会如同虚设。本文只是简单实现,不会太过深入。有攻就有防写这篇文章完全是因为同事的公众号发了一篇文章叫实践-写个验证码,你简单写了一下,我就简单破解一下试试,生活处处有乐趣啊~
生成验证码copy代码,执行,生成如下验证码:
如图我们能发现,这个验证码格式特别规范,字体大小一样,颜色都是黑色,让我们省了不少事儿。
二值化程序读图,二值化(关键点在于查找字体颜色的阈值,这个验证码都是黑色,so...),通过程序一个像素点一个像素点判断,将属于字体颜色的标记为*,非字体颜色标记为0
<center>从上面的图,能够大概看出验证码的样子(ytad)</center>
分析图像,切割切割出字符串(先切绿线,再分别切蓝线,这样即使这个字符上下移动一下,也不太容易影响我们的切割)
提取特征码将字符串拆分后,我们多次获取验证码,将a-z,a-z,0-9等验证码的特征码全部记录下来。
<center>这个是提取出来的字母y</center>
识别识别的过程就是重复上面的:二值化->切割->提取特征码,再加上和之前提取的特征码比对相似度,就ok了。
php代码实现/** * 简单验证码识别 * @author zhjx922 */class vcode{ //字符特征码 private $_wordkeys = array ( 'a' => '000**00000****000**00**0**0000****0000****0000************0000****0000****0000**', 'b' => '******00**000**0**0000****000**0******00**000**0**0000****0000****000**0******00', 'c' => '00*****00**000****00000***000000**000000**000000**000000**00000*0**000**00*****0', 'd' => '******00**000**0**0000****0000****0000****0000****0000****0000****000**0******00', 'e' => '*********00000**00000**00000******0**00000**00000**00000**00000*******', 'f' => '**********000000**000000**000000******00**000000**000000**000000**000000**000000', 'g' => '00*****00**000****000000**000000**000000**000*****0000****0000**0**000**00*****0', 'h' => '**0000****0000****0000****0000************0000****0000****0000****0000****0000**', 'i' => '******00**0000**0000**0000**0000**0000**0000**0000**00******', 'j' => '00****0000**0000**0000**0000**0000**0000***000****0**00***00', 'k' => '**0000****000**0**00**00**0**000****0000****0000**0**000**00**00**000**0**0000**', 'l' => '**00000**00000**00000**00000**00000**00000**00000**00000**00000*******', 'm' => '**0000*****00*************0**0****0**0****0**0****0000****0000****0000****0000**', 'n' => '**0000*****000******00******00****0**0****0**0****00******000*****000*****0000**', 'p' => '*******0**0000****0000****0000*********0**000000**000000**000000**000000**000000', 'q' => '00****000**00**0**0000****0000****0000****0000****0**0****00****0**00**000****0*', 'r' => '*******0**0000****0000****0000*********0*****000**00**00**000**0**0000****0000**', 's' => '0******0**0000****000000**0000000******0000000**000000**000000****0000**0******0', 't' => '********000**000000**000000**000000**000000**000000**000000**000000**000000**000', 'u' => '**0000****0000****0000****0000****0000****0000****0000****0000**0**00**000****00', 'v' => '**0000****0000****0000**0**00**00**00**00**00**000****0000****00000**000000**000', 'w' => '**0000****0000****0000****0000****0**0****0**0****0**0*************00*****0000**', 'x' => '**0000****0000**0**00**000****00000**000000**00000****000**00**0**0000****0000**', 'y' => '**0000****0000**0**00**000****00000**000000**000000**000000**000000**000000**000', 'z' => '*******00000**00000**0000**0000**0000**0000**0000**00000**00000*******', 'a' => '00*****00**000**000000**0*********0000****000***0****0**', 'b' => '**000000**000000**000000**0***00***00**0**0000****0000****0000*****00**0**0***00', 'c' => '00*****00**000****000000**000000**0000000**000**00*****0', 'd' => '000000**000000**000000**00***0**0**00*****0000****0000****0000**0**00***00***0**', 'e' => '00****000**00**0**0000************0000000**000**00*****0', 'f' => '000****000**00**00**00**00**000000**0000******0000**000000**000000**000000**0000', 'g' => '0*****0***000*****000**0**000**00*****00**0000000******0**0000**0******0', 'h' => '**000000**000000**000000**0***00***00**0**0000****0000****0000****0000****0000**', 'i' => '00**0000**000000000***0000**0000**0000**0000**0000**00******', 'k' => '**00000**00000**00000**00**0**0**00****000****000**0**00**00**0**000**', 'l' => '***00**00**00**00**00**00**00**00**0****', 'm' => '*0**0**0**0**0****0**0****0**0****0**0****0**0****0**0**', 'n' => '**0***00***00**0**0000****0000****0000****0000****0000**', 'o' => '00****000**00**0**0000****0000****0000**0**00**000****00', 'p' => '**0***00***00**0**0000****0000****0000*****00**0**0***00**000000**000000', 'q' => '00***0**0**00*****0000****0000****0000**0**00***00***0**000000**000000**', 'r' => '**0****00***00**0**000000**000000**000000**000000**00000', 's' => '0******0**0000****0000000******0000000****0000**0******0', 't' => '00**000000**0000******0000**000000**000000**000000**000000**00**000****0', 'u' => '**0000****0000****0000****0000****0000**0**00***00***0**', 'v' => '**0000****0000**0**00**00**00**000****0000****00000**000', 'w' => '**0000****0000****0**0****0**0****0**0**********0**00**0', 'x' => '**0000**0**00**000****00000**00000****000**00**0**0000**', 'y' => '**0000****0000****0000****0000****0000**0**00***00***0***00000**0******0', 'z' => '******0000**000**000**000**000**0000******', '0' => '000**00000****000**00**0**0000****0000****0000****0000**0**00**000****00000**000', '1' => '00**000***00****0000**0000**0000**0000**0000**0000**00******', '2' => '00****000**00**0**0000**000000**00000**00000**00000**00000**00000**00000********', '3' => '0*****00**000**0000000**00000**0000***0000000**0000000**000000****000**00*****00', '4' => '00000**00000***0000****000**0**00**00**0**000**0********00000**000000**000000**0', '5' => '*******0**000000**000000**0***00***00**0000000**000000****0000**0**00**000****00', '6' => '00****000**00**0**0000*0**000000**0***00***00**0**0000****0000**0**00**000****00', '7' => '********000000**000000**00000**00000**00000**00000**00000**00000**000000**000000', '8' => '00****000**00**0**0000**0**00**000****000**00**0**0000****0000**0**00**000****00', '9' => '00****000**00**0**0000****0000**0**00***00***0**000000**0*0000**0**00**000****00', ); /** * 生成验证码 * @author 武老师 */ public function make($vercode = '') { if(empty($vercode)) { $basechars = 'abcdefghijklmnpqrstuvwxyzabcdefghigklmnopqrstuvwxyz0123456789'; $vercode = ''; $codecharlenth = 4; for ($i = 1; $i <= $codecharlenth; $i++) { // 通过字符串下标形式随机获取 $vercode .= $basechars{mt_rand(0, strlen($basechars) - 1)}; } } // 以下代码是将生成的验证码生成图片 $font_size = 20; $width = 60; $height = 30; $img = imagecreate($width, $height); // 新建一个基于调色板的图像 $bgr = mt_rand(50, 200); //r(ed) $bgg = mt_rand(50, 200); //g(reen) $bgb = mt_rand(50, 200); //b(lue) $background = imagecolorallocate($img, $bgr, $bgg, $bgb); // 背景色 $black = imagecolorallocate($img, 0, 0, 0); imagestring($img, 5, 9, 8, $vercode, $black); // 水平地画一行字符串 ob_start(); imagepng($img); $image = ob_get_contents(); ob_end_clean(); return array( 'image' => $image, 'code' => $vercode ); } /** * 获取原始图像数组 * @param string $imagestring * @return array */ public function getimage($imagestring) { $im = imagecreatefromstring($imagestring); list($width, $height) = getimagesizefromstring($imagestring); $image = array(); for($x = 0;$x < $width;$x++) { for($y =0;$y < $height;$y++) { $rgb = imagecolorat($im, $x, $y); $rgb = imagecolorsforindex($im, $rgb); if($rgb['red'] == 0 && $rgb['green'] == 0 && $rgb['blue'] == 0) { $image[$y][$x] = '*'; } else { $image[$y][$x] = 0; } } } return $image; } /** * 移除无用数据 * @param array $image * @return array */ public function remove($image) { //计算x和y轴的 $xcount = count($image[0]); //60 $ycount = count($image); //30 $xfilter = array(); for($x = 0;$x < $xcount;$x++) { $filter = true; for($y = 0;$y < $ycount;$y++) { $filter = $filter && ($image[$y][$x] == '0'); } if($filter) { $xfilter[] = $x; } } //有字符的列 $ximage = array_values(array_diff(range(0, 59), $xfilter)); //存放关键字 $wordimage = array(); $prex = $ximage[0] - 1; $wordcount = 0; foreach($ximage as $xkey => $x) { if($x != ($prex + 1)) { $wordcount++; } $prex = $x; for($y = 0;$y < $ycount;$y++) { $wordimage[$wordcount][$y][$x] = $image[$y][$x]; } } foreach($wordimage as $key=>$image) { $wordimage[$key] = $this->removebyline($image); } return $wordimage; } /** * 按行移除无用数据 * @param array $image * @return array */ public function removebyline($image) { $isfilter = false; foreach($image as $y => $yimage) { if($isfilter == true || array_filter($yimage)) { $isfilter = true; } else { unset($image[$y]); } } krsort($image); $isfilter = false; foreach($image as $y => $yimage) { if($isfilter == true || array_filter($yimage)) { $isfilter = true; } else { unset($image[$y]); } } ksort($image); return $image; } /** * 获取关键字字符串 * @param array $wordimage * @return string */ public function getwordstring($wordimage) { $wordstring = ''; foreach($wordimage as $image) { foreach($image as $string) { $wordstring .= $string; } } return $wordstring; } /** * 匹配关键字 * @param array $image * @return array */ public function match($image) { $match = array( 'min' => '', 'key' => '' ); foreach($this->_wordkeys as $k => $v) { $percent = 0.0; similar_text($this->getwordstring($image), $v, $percent); if($match['min'] == '') { $match['min'] = $percent; $match['key'] = $k; } else { if($percent > $match['min']) { $match['min'] = $percent; $match['key'] = $k; } } } return $match; } /** * 终端显示验证码 * @param $image */ public function show($image) { foreach($image as $ximage) { foreach($ximage as $yimage) { echo $yimage; } echo php_eol; } echo php_eol; }}$vcode = new vcode();$codeimage = $vcode->make();$imagestring = $codeimage['image'];$image = $vcode->getimage($imagestring);//原图$vcode->show($image);//去除干扰边框、拆字$newimage = $vcode->remove($image);$word = array();$code = '';foreach($newimage as $image) { $vcode->show($image); $code .= $vcode->match($image)['key'];}echo 生成的验证码为:{$codeimage['code']} . php_eol;echo 识别的验证码为:{$code} . php_eol;/*//用来批量生成验证码的特征码。识别他人网站验证码,需要自己采集多张,人肉标记特征码$vcode = new vcode();$string = 'abcdefghijklmnpqrstuvwxyzabcdefghigklmnopqrstuvwxyz0123456789';$max = ceil(strlen($string) / 4);$wordkeys = array();for($i=0;$i<$max;$i++) { $code = substr($string, $i * 4, 4); $imagestring = $vcode->make($code)['image']; $image = $vcode->getimage($imagestring); $newimage = $vcode->remove($image); foreach($newimage as $key => $image) { $word = $vcode->getwordstring($image); isset($code[$key]) && $wordkeys[$code[$key]] = $word; }}echo var_export($wordkeys);*/
运行结果:
以上就是特别简单的php验证码识别的详细内容。