|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @project Anticaptcha library |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Anticaptcha\Entity; |
|
7
|
|
|
|
|
8
|
|
|
use Anticaptcha\Exception\InvalidArgumentException; |
|
9
|
|
|
use Anticaptcha\TaskInterface; |
|
10
|
|
|
use function Anticaptcha\t; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class ImageToTextTask |
|
14
|
|
|
* |
|
15
|
|
|
* @author Dmitry Gladyshev <[email protected]> |
|
16
|
|
|
* @since 1.0 |
|
17
|
|
|
* @see https://anticaptcha.atlassian.net/wiki/spaces/API/pages/5079091/ImageToTextTask+solve+usual+image+captcha |
|
18
|
|
|
*/ |
|
19
|
|
|
class ImageToTextTask extends Entity implements TaskInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* File body encoded in base64. Make sure to send it without line breaks. |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $body; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* false - no requirements |
|
29
|
|
|
* true - worker must enter an answer with at least one "space". |
|
30
|
|
|
* @var bool |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $phrase = false; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* false - no requirements |
|
36
|
|
|
* true - worker will see a special mark telling that answer must be entered with case sensitivity. |
|
37
|
|
|
* @var bool |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $case = false; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* 0 - no requirements |
|
43
|
|
|
* 1 - only number are allowed |
|
44
|
|
|
* 2 - any letters are allowed except numbers |
|
45
|
|
|
* @var int |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $numeric = 0; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* false - no requirements |
|
51
|
|
|
* true - worker will see a special mark telling that answer must be calculated |
|
52
|
|
|
* @var bool; |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $math = false; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* 0 - no requirements |
|
58
|
|
|
* >1 - defines minimum length of the answer |
|
59
|
|
|
* |
|
60
|
|
|
* @var int |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $minLength = 0; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* 0 - no requirements |
|
66
|
|
|
* >1 - defines maximum length of the answer |
|
67
|
|
|
* @var int |
|
68
|
|
|
*/ |
|
69
|
|
|
protected $maxLength = 0; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* From image task builder. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $path # Path po the image |
|
75
|
|
|
* @param array $options # Recognition options |
|
76
|
|
|
* @return static |
|
77
|
|
|
* @throws InvalidArgumentException |
|
78
|
|
|
*/ |
|
79
|
|
|
public static function fromImage($path, array $options = []) |
|
80
|
|
|
{ |
|
81
|
|
|
if (!is_readable($path)) { |
|
82
|
|
|
throw new InvalidArgumentException(t('Image is not found.')); |
|
83
|
|
|
} |
|
84
|
|
|
$options['body'] = base64_encode(file_get_contents($path)); |
|
85
|
|
|
return new static($options); |
|
86
|
|
|
} |
|
87
|
|
|
/** |
|
88
|
|
|
* From Base64 task builder. |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $string # Base64 string |
|
91
|
|
|
* @param array $options # Recognition options |
|
92
|
|
|
* @return static |
|
93
|
|
|
* @throws InvalidArgumentException |
|
94
|
|
|
*/ |
|
95
|
|
|
public static function fromBase64($string, array $options = []) |
|
96
|
|
|
{ |
|
97
|
|
|
$options['body'] = $string; |
|
98
|
|
|
return new static($options); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* ImageToTextTask constructor. |
|
103
|
|
|
* |
|
104
|
|
|
* @param array $options |
|
105
|
|
|
* @throws InvalidArgumentException |
|
106
|
|
|
*/ |
|
107
|
|
|
public function __construct(array $options = []) |
|
108
|
|
|
{ |
|
109
|
|
|
if (empty($options['body'])) { |
|
110
|
|
|
throw new InvalidArgumentException(t('The \'body\' field is required.')); |
|
111
|
|
|
} |
|
112
|
|
|
parent::__construct($options); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return string |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getBody() |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->body; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return bool |
|
125
|
|
|
*/ |
|
126
|
|
|
public function isPhrase() |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->phrase; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @return bool |
|
133
|
|
|
*/ |
|
134
|
|
|
public function isCase() |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->case; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @return int |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getNumeric() |
|
143
|
|
|
{ |
|
144
|
|
|
return $this->numeric; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return bool |
|
149
|
|
|
*/ |
|
150
|
|
|
public function isMath() |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->math; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @return int |
|
157
|
|
|
*/ |
|
158
|
|
|
public function getMinLength() |
|
159
|
|
|
{ |
|
160
|
|
|
return $this->minLength; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @return int |
|
165
|
|
|
*/ |
|
166
|
|
|
public function getMaxLength() |
|
167
|
|
|
{ |
|
168
|
|
|
return $this->maxLength; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @return string |
|
173
|
|
|
*/ |
|
174
|
|
|
public function getType() |
|
175
|
|
|
{ |
|
176
|
|
|
return 'ImageToTextTask'; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @return array |
|
181
|
|
|
*/ |
|
182
|
|
|
public function toArray() |
|
183
|
|
|
{ |
|
184
|
|
|
return [ |
|
185
|
|
|
'type' => $this->getType(), |
|
186
|
|
|
'body' => $this->getBody(), |
|
187
|
|
|
'phrase' => $this->isPhrase(), |
|
188
|
|
|
'case' => $this->isCase(), |
|
189
|
|
|
'numeric' => $this->getNumeric(), |
|
190
|
|
|
'math' => $this->isMath(), |
|
191
|
|
|
'minLength' => $this->getMinLength(), |
|
192
|
|
|
'maxLength' => $this->getMaxLength() |
|
193
|
|
|
]; |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|