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
|
<?php class captcha { protected $width = 160; protected $height = 45;
private $pool = '23456789abdefghijnqrtuyABDEFGHJLNQRTY';
private $fonts = array();
public function __construct($width = null, $height = null) { if (!empty($width)) { $this->width = $width; } if (!empty($height)) { $this->height = $height; } }
public function create_captcha() { $image = @imagecreatetruecolor($this->width, $this->height) or die("Cannot Initialize new GD image stream");
// set background to white and allocate drawing colours $background = imagecolorallocate($image, 0x00, 0x00, 0x8B); imagefill($image, 0, 0, $background); $linecolor = imagecolorallocate($image, 0x1F, 0x3F, 0x70); $textcolors = array( $textcolor = imagecolorallocate($image, 0xB4, 0x95, 0x3C), $textcolor = imagecolorallocate($image, 0xE8, 0x21, 0x25), );
for ($i = 0; $i < 6; $i++) { imagesetthickness($image, rand(1, 3)); imageline($image, 0, rand(0, $this->height), $this->width, rand(0, $this->height), $linecolor); }
$fonts = array( dirname(__FILE__) . DIRECTORY_SEPARATOR . "fonts" . DIRECTORY_SEPARATOR . 'dejavu/1.ttf', dirname(__FILE__) . DIRECTORY_SEPARATOR . "fonts" . DIRECTORY_SEPARATOR . 'dejavu/2.ttf', dirname(__FILE__) . DIRECTORY_SEPARATOR . "fonts" . DIRECTORY_SEPARATOR . 'dejavu/3.ttf', dirname(__FILE__) . DIRECTORY_SEPARATOR . "fonts" . DIRECTORY_SEPARATOR . 'dejavu/4.ttf', dirname(__FILE__) . DIRECTORY_SEPARATOR . "fonts" . DIRECTORY_SEPARATOR . 'dejavu/5.ttf', );
$captcha = ''; for ($x = round($this->width / 16); $x <= round($this->width * 13 / 16); $x += round($this->width / 5.5)) { $captcha .= ($char = $this->pool[rand(0, strlen($this->pool) - 1 )]); imagettftext($image, rand(round($this->width / 8.2), round($this->width / 7.6)), rand(-30, 30), $x, rand(round($this->height / 2.25), round($this->height / 1.1)), $textcolors[array_rand($textcolors)], $fonts[array_rand($fonts)], $char); }
if (!session_id()) { session_start(); } $_SESSION['captcha'] = $captcha;
header('Content-type: image/png'); imagepng($image); imagedestroy($image); } } $captcha = new captcha(); $captcha->create_captcha();
|