-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaptcha.php
More file actions
executable file
·81 lines (62 loc) · 2.14 KB
/
Captcha.php
File metadata and controls
executable file
·81 lines (62 loc) · 2.14 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
<?php
/*
* Copyright (c) 2023. Ankio. All Rights Reserved.
*/
namespace library\captcha;
namespace library\captcha;
use cleanphp\App as CleanApp;
use cleanphp\base\Session;
use cleanphp\base\Variables;
class Captcha
{
private string $result = "999";
public function getResult(): string
{
return $this->result;
}
public function verify(string $scene, int $code): bool
{
$this->result = Session::getInstance()->get($scene,"999");
Session::getInstance()->delete($scene);
return $code === intval($this->result);
}
public function create(string $scene): void
{
$image = imagecreate(200, 100);
imagecolorallocate($image, 0, 0, 0);
for ($i = 0; $i <= 9; $i++) {
imageline($image, rand(0, 200), rand(0, 100), rand(0, 200), rand(0, 100), $this->color($image));
}
for ($i = 0; $i <= 100; $i++) {
imagesetpixel($image, rand(0, 200), rand(0, 100), $this->color($image));
}
$str = $this->generateCode($scene);
for ($i = 0; $i < 4; $i++) {
imagettftext($image, rand(20, 38), rand(0, 30), $i * 50 + 25, rand(30, 70), $this->color($image), Variables::getLibPath('captcha', 'fonts', 'Bitsumishi.ttf'), $str[$i]);
}
@header('Content-type:image/jpeg');
imagejpeg($image);
imagedestroy($image);
CleanApp::exit('输出验证码图片');
}
private function color($image)
{
return imagecolorallocate($image, rand(127, 255), rand(127, 255), rand(127, 255));
}
private function generateCode(string $scene): string
{
$chars = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
$operators = ["+", "-"];
$num1 = $chars[rand(0, sizeof($chars) / 2)];
$num2 = $chars[rand(sizeof($chars) / 2, sizeof($chars) - 1)];
$operator = $operators[rand(0, 1)];
$str = $num1 . $operator . $num2 . "=";
if ($operator == "+") {
$this->result = $num1 + $num2;
} else {
$this->result = $num1 - $num2;
}
Session::getInstance()->set($scene, $this->result, 300);
return $str;
}
}