php coded by vovka pw class Captcha2 protected code const KEY captcha_

 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
<?php
//coded by vovka.pw
class Captcha2 {
protected $code;
const KEY = "captcha_code";
public function __construct($check = false, $length = 5, $size = 200, $angle = true, $site = true, $font = 'font.ttf') {
if (!$check) {
$this->code = $this->genCode($length);
$this->setSession();
$this->draw($size, $font, $angle, $site);
}
}
protected function genCode($length) {
if($length <= 0)
return 'error';
$code = '';
$chars = "0123456789";
for($i = 0; $i < $length; $i++)
$code .= $chars{rand(0, strlen($chars) - 1)};
return $code;
}
protected function setSession() {
$_SESSION[self::KEY] = $this->code;
}
public function check($code) {
$flag = isset($_SESSION[self::KEY]) && (String) $code == $_SESSION[self::KEY];
unset($_SESSION[self::KEY]);
return $flag;
}
protected function draw($size, $font, $angle, $site) {
$angles = array();
$coords = array();
$heightLetters = 0;
$widthLetters = 0;
for($i = 0; $i < strlen($this->code); $i++) {
$angles[$i] = $angle ? rand(-15, 15) : 0;
$coords[$i] = imagettfbbox($size, $angles[$i], $font, $this->code{$i});
$widthLetters += $this->getWH(0, $coords[$i]);
$heightLetters = $this->getWH(1, $coords[$i]) > $heightLetters ? $this->getWH(1, $coords[$i]) : $heightLetters;
}
$img = imagecreatetruecolor($widthLetters, !$site ? $heightLetters : $heightLetters + $heightLetters * 0.25);
$bgcolor = $this->getRandColor();
imagefilledrectangle($img, 0, 0, $widthLetters, $heightLetters, imagecolorallocate($img, $bgcolor[0], $bgcolor[1], $bgcolor[2]));
$x = 0;
for($i = 0; $i < strlen($this->code); $i++) {
$letterColor = $this->getRandColor($bgcolor);
imagettftext($img, $size, $angles[$i], $x, round($heightLetters / 2) + round($this->getWH(1, $coords[$i]) / 2), imagecolorallocate($img, $letterColor[0], $letterColor[1], $letterColor[2]), $font, $this->code{$i});
$x += $this->getWH(0, $coords[$i]);
}
if($site) {
imagefilledrectangle($img, 0, $heightLetters + ($heightLetters * 0.25), $widthLetters, $heightLetters, 0x000000);
$coords1 = imagettfbbox(round($heightLetters * 0.18), 0, $font, $_SERVER['HTTP_HOST']);
imagettftext($img, round($heightLetters * 0.18), 0, round($widthLetters / 2) - round(($coords1[2] - $coords1[0]) / 2), $heightLetters + ($heightLetters * 0.22), 0x66FF33, 'font.ttf', $_SERVER['HTTP_HOST']);
}
imagepng($img);
}
protected function getWH($mode, $coords) {
$max = $min = $coords[$mode];
for($i = 2 + $mode; $i < 8; $i += 2) {
$max = $max < $coords[$i] ? $coords[$i] : $max;
$min = $min > $coords[$i] ? $coords[$i] : $min;
}
return $max - $min;
}
protected function getRandColor($rgb = array()) {
if(is_array($rgb) && count($rgb) == 3) {
$o = rand(30, 40); //количество оттенков для смены цветов RGB, если будет почти сливатся с фоном
$newcolor = array(rand(0, 255), rand(0, 255), rand(0, 255));
return array(abs($rgb[0] - $newcolor[0]) < $o ? ($newcolor[0] + $o <= 255 ? $newcolor[0] + $o : $newcolor[0] - $o) : $newcolor[0], abs($rgb[1] - $newcolor[1]) < $o ? ($newcolor[1] + $o <= 255 ? $newcolor[1] + $o : $newcolor[1] - $o) : $newcolor[1], abs($rgb[2] - $newcolor[2]) < $o ? ($newcolor[2] + $o <= 255 ? $newcolor[2] + $o : $newcolor[2] - $o) : $newcolor[2]);
}
return array(rand(0, 255), rand(0, 255), rand(0, 255));
}
}