-
Notifications
You must be signed in to change notification settings - Fork 0
/
codecheck.php
157 lines (130 loc) · 3.32 KB
/
codecheck.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
// 验证码解析
class CodeCheck {
private $filename;
private $zimo;
public $opt = array(
'left' => 10, // 首字母开始坐标x
'top' => 5, // 首字母开始坐标y
'word_spacing' => 0, // 字符间距
'height' => 10, // 单字符高度
'width' => 9, // 单字符宽度
'color' => 215, // 二值化阀值
'size_num' => 5, //
'tolerance' => 12, // 单字错误数量
);
public function __construct($filename, $zimofile){
$this->filename = $filename;
$this->zimo = require $zimofile;
}
public function SetOpt($opt) {
foreach ($opt as $key => $value) {
$this->opt[$key] = $value;
}
}
public function Check(){
$filename = $this->filename;
list($width,$height) = getimagesize($filename);
$rs = imagecreatefrompng($this->filename);
//取特征值
for ($i=0; $i < $height; $i++) {
for ($j=0; $j < $width; $j++) {
$index = imagecolorat($rs, $j, $i);
$rgb = imagecolorsforindex($rs, $index);
if ($rgb['red']> $this->opt['color'] && $rgb['blue']> $this->opt['color'] && $rgb['green']> $this->opt['color']) {
$sourceData[$i][$j]=1;
}else{
$sourceData[$i][$j]=0;
}
}
}
for ($i=0; $i < $height; $i++) {
for ($j=0; $j < $width; $j++) {
echo $sourceData[$i][$j];
}
echo "\n";
}
$desData = $sourceData;
$chData = $this->getCH($desData, 5);
$digtal = $this->vertifyCode($chData);
return $digtal;
}
//去噪点 阀值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){
//第一个左上角坐标
$y = $this->opt['top'];
$x = $this->opt['left'];
$height = $this->opt['height'];
$width = $this->opt['width'];
$chData = array();
for ($z=0; $z < $this->opt['size_num']; $z++) {
$ch = 0;
$startX = $x + $z * $this->opt['width'];
if ($z > 0) {
$startX += ($z-1) * $this->opt['word_spacing'];
}
for ($i=$y; $i < $y + $this->opt['height']; $i++) {
for ($j=$startX; $j < $startX + $this->opt['width']; $j++) {
$chData[$z][$ch] = $data[$i][$j];
$ch++;
}
}
}
return $chData;
}
function vertifyCode($chData){
//字模
$typehead = $this->zimo;
$ch = array();
$w = count($chData[0]);
for ($i=0; $i < count($chData); $i++) {
for ($k=0; $k < 10; $k++) {
if (!is_array($typehead[$k])) {
if ($typehead[$k] == $chData[$i]){
$ch[$i] = $k;
break;
}
}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 < $this->opt['tolerance']) {
$ch[$i] = $k;
break;
}
}
}
if (!isset($ch[$i])) {
$ch[$i] = "?";
}
}
return $ch;
}
}
?>