<?php
//php验证码自动读取
//原理:获取验证码图片上每个像素的rgb值,通过判断rgb值获取验证码的01编码;对比编码表获取验证码值
define('WORD_WIDTH',8); //验证码文字宽
define('WORD_HIGHT',13); //验证码文字高
define('OFFSET_X',4); //上边距
define('OFFSET_Y',4); //左边距
define('WORD_SPACING',7); //验证码间隔
class valite
{
public function setImage($Image)
{
$this->ImagePath = $Image;
}
public function getData()
{
return $data;
}
public function getResult()
{
return $DataArray;
}
//获取编码
public function getHec()
{
$res = imagecreatefromjpeg($this->ImagePath);
$size = getimagesize($this->ImagePath);
//echo $res;exit;
$data = array();
for($i = 0; $i < $size[1]; ++$i)
{
for($j = 0; $j < $size[0]; ++$j)
{
$rgb = imagecolorat($res,$j,$i);
$rgbarray = imagecolorsforindex($res, $rgb);
if($rgbarray['red'] < 160 and $rgbarray['green'] <160 and $rgbarray['blue'] < 160)
{
$data[$i][$j]=1;
}else{
$data[$i][$j]=0;
}
}
}
$this->DataArray = $data;
$this->ImageSize = $size;
}
//获取验证码
public function run()
{
$result = "";
$data = array("","","","");
for($i = 0; $i < 4; ++$i)
{
$x = ($i * (WORD_WIDTH + WORD_SPACING)) + OFFSET_X;
$y = OFFSET_Y;
for($h = $y; $h < (OFFSET_Y + WORD_HIGHT); ++$h)
{
for($w = $x; $w < ($x + WORD_WIDTH); ++$w)
{
@$data[$i] .= $this->DataArray[$h][$w];
}
}
}
//var_dump($data);
foreach($data as $numKey => $numString)
{
$max = 0.0;
$num = 0;
foreach($this->Keys as $key => $value)
{
$percent = 0.0;
similar_text($key, $numString, $percent);
if(intval($percent) > $max)
{
$max = $percent;
$num = $value;
if(intval($percent) > 95)
break;
}
}
$result .= $num;
}
$this->data = $result;
return $result;
}
public function Draw()
{
for($i = 0; $i < $this->ImageSize[1]; ++$i)
{
for($j = 0; $j < $this->ImageSize[0]; ++$j)
{
echo $this->DataArray[$i][$j];
}
echo "\n";
}
}
//验证码编码
public function __construct()
{
$this->Keys = array(
'00111110011111110111011111100011111000111110001111100011111000111110001111100111011101110111111100111110'=>'0',
'00111000111110000011100000111000001110000011100000111000001110000011100000111000001110000011100011111110'=>'1',
'01111100111111101000111100000111000001110000011000001101000110000011000011100000110001001111111111111111'=>'2',
'01111100111111101000011100000111000011110111110001111100000011100000011100000111100011111111111001111100'=>'3',
'00000011000001111000111110011011000110110011001101100111110000111111111111111111000000110000001100000011'=>'4',
'11111111111111111100000011000000111110001111111000001111000001110000011100000111100011111111111001111000'=>'5',
'01011110001100110111000101100001111000001110111011110111111010111110101111100011011000110111011100011110'=>'6',
'11111111111111110000000100001011000001100000011000001100000111000001100000111000001100000111000001110000'=>'7',
'00111100011111101110011111110111111101110111111001111110111011111100011111000011111100111111111101111110'=>'8',
'00111100011101111110001111100011111000111110001101110111001110110000001101000011010001110110111000111100'=>'9',
'00011110101111110111001111100001111000011110000111100001111000011110000111100001011100110011111100011110'=>'0',
'00111110011101111110001111100011011100110011101100011101011011101110011111100011111000110111011100111110'=>'8',
'01111100111111101110011111000011110000111110011111111111011110110010011100000111100011111111111001111100'=>'9',
'00001100011111001111110011111100000111000001110000011100010111011001110000011100000111001111111111111111'=>'1',
'00011110001111110111001101110011011110110111111100111111011101111110011111100001111100010111111100111111'=>'8',
//'00111100011011100100011101000111000001110000111000111000000011100100011101000111010001110110111000111100'=>'3',
'00111110011111101110000011100000111000001101110011111110110001111100011111000111111001111111111000111100'=>'6',
'01111100111111101010011100000111000011110111110001111100000011100000011100000111100011111111111001111100'=>'3',
'00011100001101100110001101100011111000111110101111100011111000111110001101101011011000110011011000011110'=>'6',
'00011000111110000011100000111000001110000011100000111000001110000011100000111000001110000011100011111110'=>'1',
'00011111001111110111000001110000111000001110111011111111111000111110001111100011111100110111111100011110'=>'6',
'11111110111111101000000010000000100001001111100000011100000011100000111000001110100011101001110011111000'=>'5',
'01111100111111101100011111000111111011100111110001111100111011101110011111000111111001111111111011111100'=>'8',
'00111110011111111111000111100000110000001101111011111111111001111100001111000011111001111111111100111110'=>'6',
'00111111011111111110001111100011111101110011111000111110011101111110001111100011111100110111111100111110'=>'8',
'00111100011111111110001111100011111000111110001101111111001110110000001100001111000001110111111001111100'=>'9',
'00111000011011001100011111000110110001111100011111000111110001111100011111010110110001100110110000111000'=>'0',
'10111110011111110111011111100011111000111110001111100111111000111110001111100011011101110111111100111110'=>'0',
'00111110011111111110001111100011011100110011101100011100011011101110011111100011111000110111011100111110'=>'8',
'00001110001111100001111000101110001011100100111001001110100011101111111111111111000011100000111000111111'=>'4',
'01111000110111001000111010001110001011100001110001110000000111000000111010001110100011101101110001111000'=>'3'
);
}
protected $ImagePath;
protected $DataArray;
protected $ImageSize;
protected $data;
protected $Keys;
protected $NumStringArray;
}
?>
php规则验证码读取
最新推荐文章于 2024-02-07 11:30:00 发布