<?php
// author email: ugg.xchj@gmail.com
// 本代码仅供学习参考,不提供任何技术保证。
// 切勿使用本代码用于非法用处,违者后果自负。
include_once("files.php");
class valite
{
public function setImage($Image)
{
$this->ImagePath = $Image;
}
public function getData()
{
return $this->data;
}
public function setKeyValue($key,$value)
{
if(isset($this->Keys[$key]))
return $this->Keys[$key];
else
$this->Keys[$key]=$value;
return $value;
}
public function getResult()
{
return $this->DataArray;
}
public function getHec()
{
$res = imagecreatefromjpeg($this->ImagePath);
$size = getimagesize($this->ImagePath);
$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);
// =========================================================
// 任何验证码的数字和字母部分为了和验证码图片背景有所区别
// 都必须对文字和背景图片的RGB进行区分,下面的值是我根据
// 验证码的图片进行区分的,您可以分析您的图片,找到如下规律
// =========================================================
if($rgbarray['red'] < 125 || $rgbarray['green']<125
|| $rgbarray['blue'] < 125)
{
$data[$i][$j]=1;
}else{
$data[$i][$j]=0;
}
}
}
// 首列1
for($j=0; $j < $size[1]; ++$j)
{
$data[$j][0]=0;
}
$this->DataArray = $data;
$this->ImageSize = $size;
}
public function run()
{
// 做成字符串
// 做成字符串
$data = array();
$i = 0;
foreach($this->dealData as $key => $value)
{
$data[$i] = "";
foreach($value as $skey => $svalue)
{
$data[$i] .= implode("",$svalue);
}
++$i;
}
// 进行关键字匹配
foreach($data as $numKey => $numString)
{
$max=0.0;
$num = 0;
if(isset($this->Keys[$numString]))
{
$this->result[$numKey] = $this->Keys[$numString];
}else{
foreach($this->Keys as $key => $value)
{
$FindOk = false;
$percent=0.0;
// echo "keys:".$key."\n";
// echo "value:".$value."\n";
similar_text($key, $numString,$percent);
// print_r($percent);
// echo " ";
if(intval($percent) > $max)
{
$max = $percent;
$num = $value;
if(intval($percent) > 98){
$FindOk = true;
break;
}
}
}
$this->result[$numKey]=$num;
}
}
// 查找最佳匹配数字
return $this->result;
}
public function bmp2jpeg($file){
$res = $this->imagecreatefrombmp($file);
imagejpeg($res,$file.".jpeg");
}
public function filterInfo()
{
$data=array();
$num = 0;
$b = false;
$Continue = 0;
$XStart = 0;
for($y=0; $y<$this->ImageSize[1]; ++$y)
{
if($y < 9 || $y > 16)
{
$xstart = -1;
$num = 0;
for($x=1; $x<$this->ImageSize[0]; ++$x)
{
if($this->DataArray[$y][$x] == 1)
{
if($xstart == -1){
$xstart = $x;
}
}
if($num > 8)
{
for($xt=$xstart; $xt<$this->ImageSize[0]; ++$xt){
if($this->DataArray[$y][$xt] == 1)
{
$this->DataArray[$y][$xt] = 0;
}else{
$x = $xt-1;
break;
}
}
}
if($this->DataArray[$y][$x-1] == 1 && $this->DataArray[$y][$x] == 1){
++$num;
}else{
$xstart = -1;
$num = 0;
}
}
}
}
// 如果1的周围数字不为1,修改为了0
for($i=0; $i < $this->ImageSize[1]; ++$i)
{
for($j=0; $j < $this->ImageSize[0]; ++$j)
{
$num = 0;
if($this->DataArray[$i][$j] == 1)
{
// 上
if(isset($this->DataArray[$i-1][$j])){
$num = $num + $this->DataArray[$i-1][$j];
}
// 下
if(isset($this->DataArray[$i+1][$j])){
$num = $num + $this->DataArray[$i+1][$j];
}
// 左
if(isset($this->DataArray[$i][$j-1])){
$num = $num + $this->DataArray[$i][$j-1];
}
// 右
if(isset($this->DataArray[$i][$j+1])){
$num = $num + $this->DataArray[$i][$j+1];
}
// 上左
if(isset($this->DataArray[$i-1][$j-1])){
$num = $num + $this->DataArray[$i-1][$j-1];
}
// 上右
if(isset($this->DataArray[$i-1][$j+1])){
$num = $num + $this->DataArray[$i-1][$j+1];
}
// 下左
if(isset($this->DataArray[$i+1][$j-1])){
$num = $num + $this->DataArray[$i+1][$j-1];
}
// 下右
if(isset($this->DataArray[$i+1][$j+1])){
$num = $num + $this->DataArray[$i+1][$j+1];
}
}
if($num < 3){
$this->DataArray[$i][$j] = 0;
}
}
}
// 末尾部分处理
for($j=17; $j < $this->ImageSize[1]; ++$j)
{
for($i = 51; $i <$this->ImageSize[0]; ++$i)
{
$this->DataArray[$j][$i]=0;
}
}
for($j=0; $j < 5; ++$j)
{
for($i = 51; $i <$this->ImageSize[0]; ++$i)
{
$this->DataArray[$j][$i]=0;
}
}
// X 坐标
for($i=0; $i<$this->ImageSize[0]; ++$i)
{
// Y 坐标
for($j=0; $j<$this->ImageSize[1]; ++$j)
{
if($this->DataArray[$j][$i] == 1 || ($Continue > 0 && $Continue < 5))
{
$b = true;
++$Continue;
break;
}else{
$b = false;
}
}
if($b == true)
{
for($jj = 0; $jj < $this->ImageSize[1]; ++$jj)
{
$data[$num][$jj][$XStart] = $this->DataArray[$jj][$i];
}
++$XStart;
}else{
if($Continue > 0){
$XStart = 0;
$Continue = 0;
++$num;
}
}
}
// 粘连字符分割
$inum = 0;
for($num =0; $num < count($data); ++$num)
{
$itemp = 5;
$str = implode("",$data[$num][$itemp]);
// 超过标准长度
if(strlen($str) > $this->maxfontwith)
{
$len = (strlen($str)+1)/2;
$flen = strlen($str);
$ih = 0;
// $iih = 0;
foreach($data[$num] as $key => $value)
{
$ix = 0;
$ixx = 0;
foreach($value as $skey=>$svalue)
{
if($skey < $len)
{
$this->data[$inum][$ih][$ix] = $svalue;
++$ix;
}
if($skey > ($flen-$len-1))
{
$this->data[$inum+1][$ih][$ixx] = $svalue;
++$ixx;
}
}
++$ih;
}
++$inum;
}else{
$i = 0;
foreach($data[$num] as $key => $value){
$this->data[$inum][$i] = $value;
++$i;
}
}
++$inum;
}
// 去掉0数据
for($num = 0; $num < count($this->data); ++$num)
{
if(count($this->data[$num]) != $this->ImageSize[1])
{
foreach($this->data[$num] as $key=>$value)
{
$str = implode("",$value);
echo $str;
echo "\n";
}
return false;
}
for($i=0; $i < $this->ImageSize[1]; ++$i)
{
$str = implode("",$this->data[$num][$i]);
$pos = strpos($str, "1");
if($pos === false)
{
unset($this->data[$num][$i]);
}
}
}
return true;
}
public function dealwithData()
{
foreach($this->data as $key => $value)
{
$rand_keys = array_rand($value);
$with = count($value[$rand_keys]);
$hight = count($value);
$miniwith = array(3,3,3);
$minihight = array(3,3,3,3,3);
$bwithd = false;
// 获取第一个key
$tmpkey = array_keys($value);
$arrykey = $tmpkey[0];
switch($with)
{
case 10;
$miniwith[2]=4;
case 9;
break;
case 8;
$miniwith[0]=2;
break;
case 7:
$miniwith[0]=2;
$miniwith[2]=2;
break;
case 6:
$miniwith[0]=2;
$miniwith[1]=2;
$miniwith[2]=2;
break;
case 5:
$miniwith[0]=1;
$miniwith[1]=2;
$miniwith[2]=2;
break;
default:
$bwithd=true;
break;
}
if($bwithd)
{
if($bwithd < 4)
- 1
- 2
- 3
- 4
- 5
- 6
前往页