php验证码破解

header('Content-type:text/html;charset=uft-8');


define('WORD_SPACING',9);//字符间距

define('LEFT',10);

define('TOP',9);

define('HEIGHT',15);

define('WIDTH',10);

define('COLOR', 215);//二值化阀值


$filePath = $_GET['p'].'.png';

list($width,$height) = getimagesize($filePath);

$rs = imagecreatefrompng($filePath);

$sourceData =array();


//取特征值

for ($i=0; $i < $height; $i++) {

for ($j=0; $j < $width; $j++) {

$index = imagecolorat($rs, $j, $i);

$rgb = imagecolorsforindex($rs, $index);

//print_r($rgb);

if ($rgb['red']>COLOR && $rgb['blue']>COLOR && $rgb['green']>COLOR) {

//echo '1';

$sourceData[$i][$j]=0;

}else{

//echo '0';

$sourceData[$i][$j]=1;

}

}

}


//去噪点 阀值5

function clear($sourceData){

$desData = array();

$h =count($sourceData,0);

$w =count($sourceData[0]);


for ($i=1; $i < $h-1; $i++) {

for ($j=1; $j < $w-1; $j++) {

$value = $sourceData[$i-1][$j]+$sourceData[$i+1][$j]+$sourceData[$i][$j-1]+$sourceData[$i][$j+1]

亚博登录不上+$sourceData[$i-1][$j-1]+$sourceData[$i+1][$j+1]+$sourceData[$i-1][$j+1]+$sourceData[$i+1][$j-1];

if ($value>=5) {

$desData[$i-1][$j-1] = 1;

}else{

$desData[$i-1][$j-1] = 0;

}

}

}

return $desData;

}


//字符分割

function getCH($data){


//第一个左上角坐标

$x = LEFT -1;

$y = TOP -1;


$ch=0;

for ($i=$y; $i < $y + HEIGHT; $i++) {

for ($j=$x; $j < $x + WIDTH; $j++) {

$chData[0][$ch] = $data[$i][$j];

$ch++;

}

}


$ch=0;

for ($i=$y; $i < $y + HEIGHT; $i++) {

for ($j=$x + WIDTH + WORD_SPACING; $j < $x + 2 * WIDTH + WORD_SPACING; $j++) {

$chData[1][$ch] = $data[$i][$j];

$ch++;

}

}


$ch=0;

for ($i=$y; $i < $y + HEIGHT; $i++) {

for ($j=$x + 2*(WIDTH + WORD_SPACING); $j < $x + 3 * WIDTH + 2 * WORD_SPACING; $j++) {

$chData[2][$ch] = $data[$i][$j];

$ch++;

}

}


$ch=0;

for ($i=$y; $i < $y + HEIGHT; $i++) {

for ($j=$x + 3*(WIDTH + WORD_SPACING); $j < $x + 4 * WIDTH + 3 * WORD_SPACING; $j++) {

$chData[3][$ch] = $data[$i][$j];

$ch++;

}

}


return $chData;

}


/*显示

foreach ($desData as $value) {

foreach ($value as $v) {

if($v==1){

echo '■';

} else{

echo '□';

}//echo $v;

}

echo "
";

}*/


/*学习功能 制作字模库

foreach ($chData as $value) {

echo "'";

foreach ($value as $v) {

echo $v;

}

echo "',
";

}*/


//识别验证码

function vertifyCode($chData){


//字模

$typehead = require './zimo.php';


$ch = array();

$w = count($chData[0]);

/*单个字符识别

for ($i=0; $i < 10; $i++) {

$mount=0;

for ($j=0; $j < $w; $j++) {

if ($chData[0][$j]==$typehead[$i][$j]) {

$mount++;

}

}

if ($w-$mount<15) {

$ch[0]=$i;

break;

}

}*/

for ($i=0; $i < 4; $i++) {

$result = array();

for ($k=0; $k < 10; $k++) {

$mount = 0;

if (!is_array($typehead[$k])) {

for ($j=0; $j < $w; $j++) {

/*

if (is_array($typehead[$k])) {

$subMount = 0;

foreach ($typehead[$k] as $v) {

for ($m=0; $m < $w; $w++) {

if ($chData[$i][$m] == $v[$m]) {

$subMount++;

}

}

$sub[]=$subMount;

}

$mount = max($sub);


} else {*/

if ($chData[$i][$j]==$typehead[$k][$j]) {

$mount++;

}

//}

}

}else{

foreach ($typehead[$k] as $v) {

$subMount = 0;

$sub = array();

for ($m=0; $m < $w; $m++) {

if ($chData[$i][$m] == $v[$m]) {

$subMount++;

}

}

$sub[]=$subMount;

}

$mount = max($sub);

}



if ($w-$mount<3) {

$ch[$i]=$k;

break;

}else{

$result[$k]=$w-$mount;

}

}

//未匹配成功则取最优结果

if (!isset($ch[$i])) {

$ch[$i]=current(array_keys($result,min($result)));

}

}

return $ch;

}


$desData = clear($sourceData);

$chData = getCH($desData);

$digtal = vertifyCode($chData);


$resul = '';

foreach ($digtal as $v) {

$resul .= $v;

}


//输出识别结果

echo '识别结果:',$resul;


/*test

foreach ($desData as $value) {

foreach ($value as $v) {

echo $v;

}

echo "
";

}

*/


/*test

$im = imagecreate($width-OFFSET_X-RIGHT, $height - OFFSET_Y - BOTTOM);

$black = imagecolorallocate($im, 0, 0, 0);

$white = imagecolorallocate($im, 255, 255, 255);

$h = count($desData,0);

$w = count($desData[0]);


for ($i=0; $i < $h; $i++) {

for ($j=0; $j < $w; $j++) {

if ($desData[$i][$j]==1) {

imagesetpixel($im, $j, $i, $white);

}

}

}

header("Content-type: image/jpeg");

imagejpeg($im);

*/

?>

return array(

//0

array(

'0001111000011111111011110011101110000111110000001111000000111100000011110000001111

00000011110000011111000001111111000111011101111001111111100001111110',

'0101111100011111111011110011111111000111110000001111000000111100000011110000001111

00000011110000011111000001111110000111011100111001111111100001111100',

'0001111100011111111001110011101110000111110000011111000000111100000011111000001111

00000011111000011111000000111110000111011100111001111111100111111000',

'0001111100011111111001110011101110000111110000001111000000111100000011110000001111

00000011110000011111000001111110000111111100111101111111100001111000',

'0001111000011111111001110011101110000111110000001111000000111100000011110000001111

00000011110000011111100001111110000111111110111011111111100001111000',

),

//1

array(

'0000110000000011100000011110000011111000011011100000000110000000011000000001100000000

11000000001100000000110000000011000000001100000000110000000000000',

'0000000000000011100000011110000011111000001111100000000110000000011000000001100000000

11000000011100000000110000000011000000001100000000110000000001000',

'000000000000001110000001111100001111100000101110000000011000000011100000000110000000

111000000001100000000110000000011000000001100000000110000000001100',

'0000010000000011100000011110000011111000011011100000000110000000011000000001100000000

11000000001100000000110000000011000000001100000000110000000000000',

'0000000000000011100000011110000011111000001011100000000110000000011101000001100000000

11000000001100000000110000000011000000001100000000110000000010000',

'0000000000000011100000011110000011111000001011100000000110000000011000000001100000000

11000000001100000000111000000011100000001110000000111000000000000',

),

//2

array(

'0001111000011111111011101111100000001111000000011100000000110000000111000000111000000

11100000011100000011100000011100000011110001001111111100111111110',

'0011111000011111111001101111110000000111000000011100000000110000000111000000111000000

11100000011100000011100000011100000011110101011111111100111111110',

'0001111000011111110001110111100100000111000000011000000000100000001110000000111000000

11100000011100000011100000011110000011111000001111111100111111110',

),

//3

array(

'0011110000011111110011110011100100000110000000111000000011000000011110000001111000000

11110000000011100000011110100001111011101111001111111100001111000',

'00011110000111111100111100111011000011100000001110000001110000000111100000011110000000

1110000000001100100001110100000111011101111001111111110001111110',

'00011100000111111100111100110001110001100000001110000011111000000111100000011110000000

1110000000011100000001110100001111111100111001111111100011111000',

'00011100000111111100011111110001100001100000001110000000110000000111100000011110000000

1110000000001100000001110100000111011101111001111111100001111000',

'00111100000111111100011111110001100001100000001110000000110000000111100000011110000000

1110000000011100000001111100000111111100111001111111100001111000',

),

//4

array(

'00000000000000001100000011110000001111000001111100000111110000110011000110001100111111

1110111111111001111111110000011110000000110000000011000000000000',

'00000000000000001100000001110000001111000001111100000111110000110011000110001100011101

1110111111111001111111100000011110000000110000000011000000000000',

'00000010000000001100000001110000001111000001111100000111110000110011000110001100011101

1110111111111001111111100101011110000000110000000011000000000000',

'00000000000000001100000001110000001111000001111100000111110000110011000110001110011101

1110111111111101111111100000011110000000111000000011000000000000',

'00000000000000001110000001110000001111100001111100001111110001111111000110001100011101

1110111111111001111111100000011110000000110000000011000000000000',

'00000000000000001100000001110000001111000001111110000111110000111111100111001100111111

1110111111111001111111100000011110000000110000000011000000000100',

'00000000000000011100000001110000001111000001111100000111110000110011000110001100011101

1110111111111001111111100000011110000000110000000011000000000000',

),

//5

array(

'00011111010011111100011110000001100000000111100000011111100011111111100111011110010100

0111000000001100000001110100000111011001111001111111100001111000',

),

//6

array(

'01011111100111111110011111111001110000101111010110111111110011111111101111001111111000

0111110000001111000000110110000111011101111000111111100001111000',

'00001110000011111110111101111101110000101110000000111111100011111111101111001110111000

0111111000011111000001110110001111011110111100111111100001111000',

'00111110000011111110011110111001100000101110000000111111100011111111101111001110111000

0111110000011111000000110110000111011100111100111111100001111000',

'00001110000011111110011100111001100001101110000100111111110011111111101111101110111000

0111111000001111000000111111000111111100111001111111100001111000',

'00001110000011111110011110111111110000101111100000111111100011111111101111011110111000

0111110000011111000001111110000111111110111001111111100011111000',

'00001110000011111110111100111111110000101111000000111111100011111111101111001110111000

0111111000011111100000110110001111011100111000111111100001111100',

),

//7

array(

'01111111101111111111000000111100000011100000001110000001110000000110000000111000000011

0000000110000000011000000001100000001110000000111000000000000000',

'01111111100111111111010101111100000011100000001100000011100000001110000000110000001111

0000000110000000111000000001100000001110000000110000000000000000',

'01111111100111111111000000111100000001100000011110000011100000000110000000110000000011

0000000110000000011000000001100000001110000000110000000001000000',

'01111111100111111111000010111100000011100000001100000001100000000110000000110000000011

0000000110000000111000000001100000001110000000111000000000000000',

'01111111100111111111000010111000000011100000001100000001100000001110000001111000000011

1000000111000000011100000011100000001110000001110000000001000000',

'01111111100111111111000001111100000001100000011110000001110000001110000000111000000011

0000000110000000011000000011100000001110000001111000000001100000',

)

?>