forked from hestiacp/hestiacp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaptcha.php
More file actions
94 lines (75 loc) · 2.9 KB
/
captcha.php
File metadata and controls
94 lines (75 loc) · 2.9 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
<?php
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'vesta' . DIRECTORY_SEPARATOR . 'core' . DIRECTORY_SEPARATOR . 'VestaSession.class.php';
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'vesta' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'Config.class.php';
define('V_ROOT_DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR . 'vesta' . DIRECTORY_SEPARATOR);
class Captcha
{
protected $width = 200;
protected $image = null;
protected $color1 = null;
protected $color2 = null;
protected $color3 = null;
protected $keyword = '';
public $key_len = 7;
protected $chars = 'qw1e2r3t4y5u67o8p9as9d38f6g4h3j2k1l3z5x7c8v3b5n781234567890';
public function __construct()
{
VestaSession::start();
//var_dump(Config::get('session_dirname'));die();
$this->image = imagecreatetruecolor($this->width, 50);
$this->color1 = imagecolorallocate($this->image, 57, 58, 52);
$this->color2 = imagecolorallocate($this->image, 45, 44, 40);
$this->color3 = imagecolorallocate($this->image, 255, 255, 255);
imagefilledrectangle($this->image, 0, 0, 249, 249, $this->color1);
}
public function generateImage($offset = 0)
{
$values = array(
$offset, 15,
$offset, 40,
$offset + 14, 32,
$offset + 14, 8,
$offset, 15,
$offset, 15
);
imagefilledpolygon($this->image, $values, 6, $this->color2);
}
public function draw()
{
$this->generateKeyword();
for ($i = 0; $i < strlen($this->keyword) -1; $i++) {
$this->generateImage($i * 15);
}
$font_file = dirname(__FILE__).DIRECTORY_SEPARATOR.'css'.DIRECTORY_SEPARATOR.'arialbd.ttf';
imagefttext($this->image, 17, 0, 2, 31, $this->color3, $font_file, $this->keyword);
$this->slice();
}
public function slice()
{
$width = 15;
$height = 50;
$dest = imagecreatetruecolor(15 * $this->key_len + 2 * $this->key_len + 8, $height);
imagefilledrectangle($dest, 0, 0, 249, 249, $this->color1);
for ($i = 0; $i < $this->key_len; $i++) {
$dest_x = $i == 0 ? $i * 15 : $i * 15 + $i * 4;
imagecopy($dest, $this->image, $dest_x, 0, $i * 15, 0, $width, $height);
}
header('Content-type: image/jpeg');
imagepng($dest);
}
/**
*
*/
protected function generateKeyword()
{
$this->keyword = '';
for ($i = 0; $i < $this->key_len; $i++) {
$this->keyword .= $this->chars[rand(0, strlen($this->chars)-1)];
}
$_SESSION['captcha_key'] = $this->keyword;
return $this->keyword;
}
}
$c = new Captcha();
$c->draw();
?>