-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCodeToNum.php
More file actions
124 lines (110 loc) · 6.29 KB
/
CodeToNum.php
File metadata and controls
124 lines (110 loc) · 6.29 KB
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
<?php
/**
* 识别简单的验证码
*/
class CodeToNum
{
protected $codeImagePath;
protected $width = 0;
protected $height = 0;
protected $imgRgbArray = [];
protected $dict = [
'0011100011001011000111100011110000110000011000001100000110000011100011110001101101100011100' => 0,
'0011100011011011000111100001110000110000011000001100000111000011100011110001101101100011100' => 0,
'0011100011011011000111100011110000110000011000001100000111000011100011110001101101100011100' => 0,
'0001100011110000011000001100000110000011000001100000110000011000001100000100000011000111111' => 1,
'0001100011110000011000001000000100000010000001000000100000011000001100000110000011000111111' => 1,
'0001100011110000011000001100000110000011000001100000110000011000001100000110000011000111111' => 1,
'0111110110001111000111100001000001100000110000110000010000110000000000010000110000011111111' => 2,
'0111110110001110000111100001000001100000110000110000110000010000010000010000110000011111111' => 2,
'0111110110001111000111100001000001100000110000110000110000110000010000010000110000011111111' => 2,
'0111110110001111000111100001000001100000110000110000110000110000000000010000110000011111111' => 2,
'0111110110001111000011100001000001100000110000110000110000110000010000010000110000011111111' => 2,
'0111110110001111000111100001000001100000110000110000010000110000010000010000110000011111111' => 2,
'0111110110001011000111100011000011000111000000010000001100000011100001110001111000110111110' => 3,
'0111100110001111000111100011000001000111000000010000001100000011100001110001111000110111110' => 3,
'0111110110001111000111100011000011000111000000010000001100000011100001110001111000110111110' => 3,
'0000110000011000011100011110000011001001101100110000011011111110000110000011000001100011111' => 4,
'0000110000011000011100001110000011001001101100110000011011111110000110000011000001100011111' => 4,
'0111111000000010000001000000100000011111101100011000000100000011100001110001111000110111110' => 5,
'0111111000000000000001000000100000011111101100011000000100000011100001110000111000110111110' => 5,
'0111111000000010000001000000000000011111101100011000000100000011100001110001111000110111110' => 5,
'0111111000000010000001000000100000011111101100011000000100000011100001110000111000110111110' => 5,
'0111111000000010000001000000100000011111101100011000000100000011100001100001111000110111110' => 5,
'0111111000000000000001000000100000011111101100011000000100000011100001110001111000110111110' => 5,
'0011110011001111000111100000110000011111101110011110000110000011000001110000101100110011110' => 6,
'0011110011001111000111100000110000011111101110011110000110000011100001110000101100110011110' => 6,
'0011110011001111000111100000110000011111101110011100000110000011100001110000101100110011110' => 6,
'1111111100000000000100000010000010000000000001100000100000010000011000001100000110000011000' => 7,
'1111111100000000000100000010000010000000000001000000100000010000011000001100000110000011000' => 7,
'0111110110001110000011000001110000101100000111110110011110000111000001100000111000110111110' => 8,
'0111110110001110000011000001110000001100000111100110011110000111000001100000111000110111110' => 8,
'0111110110001110000011000001110000101100000111110110011110000011000001100000111000110111110' => 8,
'0111110110001110000011000001110000101100000111100110011110000111000001100000111000110111110' => 8,
'0111110110001110000011000001110000101100000111110110001110000111000001100000111000110111110' => 8,
'0111100110001010000111000001100000110000111100111011111100000110000011110001111001100111100' => 9,
'0111100110001010000111000001100000110000111100111011111100000110000011110001011001100111100' => 9,
];
public function __construct($codeImagePath)
{
$this->codeImagePath = $codeImagePath;
}
protected function resolveImage()
{
if (!file_exists($this->codeImagePath)) {
throw new \Exception('the code Image is not exist');
}
$size = getimagesize($this->codeImagePath);
$this->width = $size[0];
$this->height = $size[1];
$temp = imagecreatefrompng($this->codeImagePath);
for ($i = 0; $i < $this->height; ++$i) {
for ($j = 0; $j < $this->width; ++$j) {
$rgb = imagecolorat($temp, $j, $i);
$this->imgRgbArray[$i][$j] = imagecolorsforindex($temp, $rgb);
}
}
}
public function getCode()
{
$this->resolveImage();
$numString = array_fill_keys(['one', 'two', 'three', 'four'], '');
for ($i = 3; $i < $this->height - 4; ++$i) {
for ($j = 5; $j < $this->width - 1; ++$j) {
if ($j <= 5 + 6) {
$numString['one'] .= $this->get1Or0($i, $j);
} elseif (5 + 6 + 5 <= $j && $j <= 5 + 6 + 4 + 7) {
$numString['two'] .= $this->get1Or0($i, $j);
} elseif (5 + 6 + 4 + 7 + 5 <= $j && $j <= 5 + 6 + 4 + 7 + 5 + 6) {
$numString['three'] .= $this->get1Or0($i, $j);
} elseif (5 + 6 + 4 + 7 + 5 + 6 + 5 <= $j) {
$numString['four'] .= $this->get1Or0($i, $j);
}
}
}
return $this->transform($numString);
}
protected function get1Or0($i, $j)
{
if ($this->imgRgbArray[$i][$j]['red'] == '255' || $this->imgRgbArray[$i][$j]['red'] == '254') {
return '1';
} else {
return '0';
}
}
protected function transform($numString)
{
$result = array_fill_keys(array_keys($numString), ['similar' => 0, 'code' => '']);
foreach ($numString as $key => $string) {
foreach ($this->dict as $str => $num) {
$n = similar_text($string, $str);
if ($result[$key]['similar'] == 0 || $n > $result[$key]['similar']) {
$result[$key]['similar'] = $n;
$result[$key]['code'] = $str;
$result[$key]['num'] = $num;
}
}
}
return implode('', array_column($result, 'num'));
}
}