These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Vicens\Captcha; |
||
4 | |||
5 | use Illuminate\Support\Facades\Session; |
||
6 | |||
7 | class Captcha |
||
8 | { |
||
9 | /** |
||
10 | * 存储在session中的key |
||
11 | */ |
||
12 | const SESSION_NAME = '_captcha'; |
||
13 | |||
14 | /** |
||
15 | * 验证码配置 |
||
16 | * @var array |
||
17 | */ |
||
18 | protected $config = [ |
||
19 | /** |
||
20 | * 调试模型 |
||
21 | */ |
||
22 | 'debug' => false, |
||
23 | /** |
||
24 | * 默认验证码长度 |
||
25 | * @var int |
||
26 | */ |
||
27 | 'length' => 4, |
||
28 | /** |
||
29 | * 验证码字符集 |
||
30 | * @var string |
||
31 | */ |
||
32 | 'charset' => 'abcdefghijklmnpqrstuvwxyz123456789', |
||
33 | /** |
||
34 | * 默认验证码宽度 |
||
35 | * @var int |
||
36 | */ |
||
37 | 'width' => 150, |
||
38 | /** |
||
39 | * 默认验证码高度 |
||
40 | * @var int |
||
41 | */ |
||
42 | 'height' => 40, |
||
43 | /** |
||
44 | * 指定文字颜色 |
||
45 | * @var string |
||
46 | */ |
||
47 | 'textColor' => null, |
||
48 | /** |
||
49 | * 文字字体文件 |
||
50 | * @var string |
||
51 | */ |
||
52 | 'textFont' => null, |
||
53 | /** |
||
54 | * 指定图片背景色 |
||
55 | * @var string |
||
56 | */ |
||
57 | 'backgroundColor' => null, |
||
58 | /** |
||
59 | * 开启失真模式 |
||
60 | * @var bool |
||
61 | */ |
||
62 | 'distortion' => true, |
||
63 | /** |
||
64 | * 最大前景线条数 |
||
65 | * @var int |
||
66 | */ |
||
67 | 'maxFrontLines' => null, |
||
68 | /** |
||
69 | * 最大背景线条数 |
||
70 | * @val int |
||
71 | */ |
||
72 | 'maxBehindLines' => null, |
||
73 | /** |
||
74 | * 文字最大角度 |
||
75 | * @var int |
||
76 | */ |
||
77 | 'maxAngle' => 8, |
||
78 | /** |
||
79 | * 文字最大偏移量 |
||
80 | * @var int |
||
81 | */ |
||
82 | 'maxOffset' => 5 |
||
83 | ]; |
||
84 | |||
85 | /** |
||
86 | * Captcha constructor. |
||
87 | * @param array $config |
||
88 | */ |
||
89 | public function __construct(array $config = []) |
||
90 | { |
||
91 | $this->setConfig($config); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * 设置验证码配置 |
||
96 | * |
||
97 | * @param array|string $config 配置数组或配置项key |
||
98 | * @param mixed $value 配置项值 |
||
99 | * @return $this |
||
100 | */ |
||
101 | public function setConfig($config, $value = null) |
||
102 | { |
||
103 | |||
104 | if (!is_array($config)) { |
||
105 | $config = [$config => $value]; |
||
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
106 | } |
||
107 | |||
108 | foreach ($config as $key => $value) { |
||
109 | if (array_key_exists($key, $this->config)) { |
||
110 | $this->config[$key] = $value; |
||
111 | } |
||
112 | } |
||
113 | |||
114 | return $this; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * 获取配置 |
||
119 | * |
||
120 | * @param string|null $key 配置项key |
||
121 | * @return string|number|array |
||
122 | */ |
||
123 | public function getConfig($key = null) |
||
124 | { |
||
125 | if ($key !== null) { |
||
126 | return $this->config[$key]; |
||
127 | } |
||
128 | |||
129 | return $this->config; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * 生成验证码 |
||
134 | * |
||
135 | * @return Image |
||
136 | */ |
||
137 | public function make() |
||
138 | { |
||
139 | $code = $this->generate(); |
||
140 | |||
141 | $hash = password_hash($code, PASSWORD_BCRYPT, array('cost' => 10)); |
||
142 | |||
143 | if ($hash === false) { |
||
144 | throw new \RuntimeException('Bcrypt hashing not supported.'); |
||
145 | } |
||
146 | |||
147 | Session::put(self::SESSION_NAME, $hash); |
||
148 | |||
149 | return new Image($this->build($code)); |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * 仅测试正确性, 不删除验证码 |
||
154 | * |
||
155 | * @param string $input |
||
156 | * @return bool |
||
157 | */ |
||
158 | public function test($input) |
||
0 ignored issues
–
show
function test() does not seem to conform to the naming convention (^(?:is|has|should|may|supports) ).
This check examines a number of code elements and verifies that they conform to the given naming conventions. You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.
Loading history...
|
|||
159 | { |
||
160 | if ($this->config['debug']) { |
||
161 | return true; |
||
162 | } elseif (!(Session::has(self::SESSION_NAME) && $input)) { |
||
163 | return false; |
||
164 | } |
||
165 | |||
166 | //返回验证结果 |
||
167 | return password_verify(strtolower($input), Session::get(self::SESSION_NAME)); |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * 检测正确性,并删除验证码 |
||
172 | * |
||
173 | * @param string $input |
||
174 | * @return bool |
||
175 | */ |
||
176 | public function check($input) |
||
0 ignored issues
–
show
function check() does not seem to conform to the naming convention (^(?:is|has|should|may|supports) ).
This check examines a number of code elements and verifies that they conform to the given naming conventions. You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.
Loading history...
|
|||
177 | { |
||
178 | $result = $this->test($input); |
||
179 | Session::forget(self::SESSION_NAME); |
||
180 | |||
181 | return $result; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * 生成验证码 |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | protected function generate() |
||
190 | { |
||
191 | $characters = str_split($this->getConfig('charset')); |
||
192 | $length = $this->getConfig('length'); |
||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues.
Loading history...
|
|||
193 | |||
194 | $code = ''; |
||
195 | for ($i = 0; $i < $length; $i++) { |
||
196 | $code .= $characters[rand(0, count($characters) - 1)]; |
||
197 | } |
||
198 | |||
199 | return strtolower($code); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * 创建验证码图片 |
||
204 | * |
||
205 | * @param string $code |
||
206 | * @return resource |
||
207 | */ |
||
208 | protected function build($code) |
||
209 | { |
||
210 | |||
211 | // 图片宽 |
||
212 | $width = $this->getConfig('width'); |
||
213 | // 图片高 |
||
214 | $height = $this->getConfig('height'); |
||
215 | // 背景颜色 |
||
216 | $backgroundColor = $this->getConfig('backgroundColor'); |
||
217 | |||
218 | // 随机取一个字体 |
||
219 | $font = $this->getTextFont(); |
||
220 | |||
221 | // 根据宽高创建一个背景画布 |
||
222 | $image = imagecreatetruecolor($width, $height); |
||
223 | |||
224 | if ($backgroundColor === null) { |
||
225 | $backgroundColor = imagecolorallocate($image, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255)); |
||
226 | } else { |
||
227 | $color = $backgroundColor; |
||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues.
Loading history...
|
|||
228 | $backgroundColor = imagecolorallocate($image, $color[0], $color[1], $color[2]); |
||
229 | } |
||
230 | // 填充背景色 |
||
231 | imagefill($image, 0, 0, $backgroundColor); |
||
232 | |||
233 | // 绘制背景干扰线 |
||
234 | $this->drawLines($image, $this->getConfig('maxBehindLines')); |
||
235 | |||
236 | // 写入验证码文字 |
||
237 | $color = $this->renderText($image, $code, $font); |
||
238 | |||
239 | // 绘制前景干扰线 |
||
240 | $this->drawLines($image, $this->getConfig('maxFrontLines'), $color); |
||
241 | |||
242 | if ($this->getConfig('distortion')) { |
||
243 | // 创建失真 |
||
244 | $image = $this->createDistortion($image, $width, $height, $backgroundColor); |
||
245 | } |
||
246 | |||
247 | //如果不指定字体颜色和背景颜色,则使用图像过滤器修饰 |
||
248 | if (function_exists('imagefilter') && is_null($backgroundColor) && is_null($this->getConfig('textColor'))) { |
||
249 | // 颜色翻转 - 1/2几率 |
||
250 | if (mt_rand(0, 1) == 0) { |
||
251 | imagefilter($image, IMG_FILTER_NEGATE); |
||
252 | } |
||
253 | // 用边缘检测来突出图像的边缘 - 1/11几率 |
||
254 | if (mt_rand(0, 10) == 0) { |
||
255 | imagefilter($image, IMG_FILTER_EDGEDETECT); |
||
256 | } |
||
257 | // 改变图像的对比度 |
||
258 | imagefilter($image, IMG_FILTER_CONTRAST, mt_rand(-50, 10)); |
||
259 | |||
260 | if (mt_rand(0, 5) == 0) { |
||
261 | // 用高斯算法和指定颜色模糊图像 |
||
262 | imagefilter($image, IMG_FILTER_COLORIZE, mt_rand(-80, 50), mt_rand(-80, 50), mt_rand(-80, 50)); |
||
263 | } |
||
264 | } |
||
265 | return $image; |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * 创建失真 |
||
270 | * |
||
271 | * @param resource $image |
||
272 | * @param int $width |
||
273 | * @param int $height |
||
274 | * @param int $backgroundColor |
||
275 | * @return resource |
||
276 | */ |
||
277 | protected function createDistortion($image, $width, $height, $backgroundColor) |
||
278 | { |
||
279 | //创建失真 |
||
280 | $contents = imagecreatetruecolor($width, $height); |
||
281 | $rWidth = mt_rand(0, $width); |
||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues.
Loading history...
|
|||
282 | $rHeight = mt_rand(0, $height); |
||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues.
Loading history...
|
|||
283 | $phase = mt_rand(0, 10); |
||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues.
Loading history...
|
|||
284 | $scale = 1.1 + mt_rand(0, 10000) / 30000; |
||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues.
Loading history...
|
|||
285 | |||
286 | for ($x = 0; $x < $width; $x++) { |
||
287 | for ($y = 0; $y < $height; $y++) { |
||
288 | $vX = $x - $rWidth; |
||
0 ignored issues
–
show
|
|||
289 | $vY = $y - $rHeight; |
||
0 ignored issues
–
show
|
|||
290 | $vN = sqrt($vX * $vX + $vY * $vY); |
||
0 ignored issues
–
show
|
|||
291 | |||
292 | if ($vN != 0) { |
||
293 | $vN2 = $vN + 4 * sin($vN / 30); |
||
294 | $nX = $rWidth + ($vX * $vN2 / $vN); |
||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues.
Loading history...
|
|||
295 | $nY = $rHeight + ($vY * $vN2 / $vN); |
||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues.
Loading history...
|
|||
296 | } else { |
||
297 | $nX = $rWidth; |
||
298 | $nY = $rHeight; |
||
299 | } |
||
300 | $nY = $nY + $scale * sin($phase + $nX * 0.2); |
||
301 | |||
302 | $pixel = $this->getColor($image, round($nX), round($nY), $backgroundColor); |
||
303 | |||
304 | if ($pixel == 0) { |
||
305 | $pixel = $backgroundColor; |
||
306 | } |
||
307 | |||
308 | imagesetpixel($contents, $x, $y, $pixel); |
||
309 | } |
||
310 | } |
||
311 | |||
312 | return $contents; |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * 获取一个字体 |
||
317 | * |
||
318 | * @return string |
||
0 ignored issues
–
show
Should the return type not be
string|integer|double|array ? Also, consider making the array more specific, something like array<String> , or String[] .
This check compares the return type specified in the If the return type contains the type array, this check recommends the use of
a more specific type like
Loading history...
|
|||
319 | */ |
||
320 | protected function getTextFont() |
||
321 | { |
||
322 | // 指定字体 |
||
323 | if ($this->getConfig('textFont') && file_exists($this->getConfig('textFont'))) { |
||
324 | return $this->getConfig('textFont'); |
||
325 | } |
||
326 | // 随机字体 |
||
327 | return __DIR__ . '/../fonts/' . mt_rand(0, 5) . '.ttf'; |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * 写入验证码到图片中 |
||
332 | * |
||
333 | * @param resource $image |
||
334 | * @param string $phrase |
||
335 | * @param string $font |
||
336 | * @return int |
||
337 | */ |
||
338 | protected function renderText($image, $phrase, $font) |
||
339 | { |
||
340 | $length = strlen($phrase); |
||
341 | if ($length === 0) { |
||
342 | return imagecolorallocate($image, 0, 0, 0); |
||
343 | } |
||
344 | |||
345 | // 计算文字尺寸 |
||
346 | $size = $this->getConfig('width') / $length - mt_rand(0, 3) - 1; |
||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues.
Loading history...
|
|||
347 | $box = imagettfbbox($size, 0, $font, $phrase); |
||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues.
Loading history...
|
|||
348 | $textWidth = $box[2] - $box[0]; |
||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues.
Loading history...
|
|||
349 | $textHeight = $box[1] - $box[7]; |
||
350 | $x = ($this->getConfig('width') - $textWidth) / 2; |
||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 10 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues.
Loading history...
|
|||
351 | $y = ($this->getConfig('height') - $textHeight) / 2 + $size; |
||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 10 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues.
Loading history...
|
|||
352 | |||
353 | if (!count($this->getConfig('textColor'))) { |
||
354 | $textColor = array(mt_rand(0, 150), mt_rand(0, 150), mt_rand(0, 150)); |
||
355 | } else { |
||
356 | $textColor = $this->getConfig('textColor'); |
||
357 | } |
||
358 | $color = imagecolorallocate($image, $textColor[0], $textColor[1], $textColor[2]); |
||
359 | |||
360 | // 循环写入字符,随机角度 |
||
361 | for ($i = 0; $i < $length; $i++) { |
||
362 | $box = imagettfbbox($size, 0, $font, $phrase[$i]); |
||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues.
Loading history...
|
|||
363 | $w = $box[2] - $box[0]; |
||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues.
Loading history...
|
|||
364 | $angle = mt_rand(-$this->getConfig('maxAngle'), $this->getConfig('maxAngle')); |
||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues.
Loading history...
|
|||
365 | $offset = mt_rand(-$this->getConfig('maxOffset'), $this->getConfig('maxOffset')); |
||
366 | imagettftext($image, $size, $angle, $x, $y + $offset, $color, $font, $phrase[$i]); |
||
367 | $x += $w; |
||
368 | } |
||
369 | |||
370 | return $color; |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * 画线 |
||
375 | * |
||
376 | * @param resource $image |
||
377 | * @param int $width |
||
378 | * @param int $height |
||
379 | * @param int|null $color |
||
380 | */ |
||
381 | protected function renderLine($image, $width, $height, $color = null) |
||
382 | { |
||
383 | $color = $color ?: imagecolorallocate($image, mt_rand(100, 255), mt_rand(100, 255), mt_rand(100, 255)); |
||
0 ignored issues
–
show
|
|||
384 | |||
385 | if (mt_rand(0, 1)) { |
||
386 | // 横向 |
||
387 | $xA = mt_rand(0, $width / 2); |
||
0 ignored issues
–
show
|
|||
388 | $yA = mt_rand(0, $height); |
||
0 ignored issues
–
show
|
|||
389 | $xB = mt_rand($width / 2, $width); |
||
0 ignored issues
–
show
|
|||
390 | $yB = mt_rand(0, $height); |
||
0 ignored issues
–
show
|
|||
391 | } else { |
||
392 | // 纵向 |
||
393 | $xA = mt_rand(0, $width); |
||
394 | $yA = mt_rand(0, $height / 2); |
||
395 | $xB = mt_rand(0, $width); |
||
396 | $yB = mt_rand($height / 2, $height); |
||
397 | } |
||
398 | imagesetthickness($image, mt_rand(1, 3)); |
||
399 | imageline($image, $xA, $yA, $xB, $yB, $color); |
||
400 | } |
||
401 | |||
402 | /** |
||
403 | * 画线 |
||
404 | * |
||
405 | * @param resource $image |
||
406 | * @param int $max |
||
407 | * @param int|null $color |
||
408 | */ |
||
409 | protected function drawLines($image, $max, $color = null) |
||
410 | { |
||
411 | $square = $this->getConfig('width') * $this->getConfig('height'); |
||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues.
Loading history...
|
|||
412 | $effects = mt_rand($square / 3000, $square / 2000); |
||
413 | |||
414 | // 计算线条数 |
||
415 | if ($max != null && $max > 0) { |
||
416 | $effects = min($max, $effects); |
||
417 | } |
||
418 | |||
419 | if ($max !== 0) { |
||
420 | for ($e = 0; $e < $effects; $e++) { |
||
421 | |||
422 | if ($color !== null) { |
||
423 | $this->renderLine($image, $this->getConfig('width'), $this->getConfig('height'), $color); |
||
424 | } else { |
||
425 | $this->renderLine($image, $this->getConfig('width'), $this->getConfig('height')); |
||
426 | } |
||
427 | |||
428 | } |
||
429 | } |
||
430 | } |
||
431 | |||
432 | /** |
||
433 | * 获取颜色 |
||
434 | * |
||
435 | * @param resource $image |
||
436 | * @param int $width |
||
437 | * @param int $height |
||
438 | * @param int $background |
||
439 | * @return int |
||
440 | */ |
||
441 | protected function getColor($image, $width, $height, $background) |
||
442 | { |
||
443 | $sWidth = imagesx($image); |
||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues.
Loading history...
|
|||
444 | $sHeight = imagesy($image); |
||
445 | if ($width < 0 || $width >= $sWidth || $height < 0 || $height >= $sHeight) { |
||
446 | return $background; |
||
447 | } |
||
448 | |||
449 | return imagecolorat($image, $width, $height); |
||
450 | } |
||
451 | |||
452 | /** |
||
453 | * @param string $name |
||
454 | * @param array $arguments |
||
455 | * @return $this |
||
456 | */ |
||
457 | public function __call($name, $arguments) |
||
458 | { |
||
459 | if (array_key_exists($name, $this->config)) { |
||
460 | $this->config[$name] = $arguments[0]; |
||
461 | } |
||
462 | |||
463 | return $this; |
||
464 | } |
||
465 | } |