helper.captcha.php
2.96 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
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/*
* Captcha helper
*
*/
session_start();
function helper_captcha() {
$default_options = array(
'type' => 'num',
'name' => 'captcha',
'num' => 6,
'fontsize' => 15,
'imagewidth' => 70,
'imageheight' => 40,
'fontangle' => 1,
'font' => '../fonts/kelson/Kelson Sans Regular.otf',
'backgroundcolor' => '000000',
'textcolor' => 'FFFFFF'
);
$options = $default_options;
switch ($options['type']) {
case 'string':
$string = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
case 'both':
$string = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
default:
$string = '0123456789';
}
$text = '';
for ($i = 0; $i < $options['num']; $i++) {
$text .= $string[rand(0, strlen($string)-1)];
}
### Save random number to session
$_SESSION[$options['name']] = md5($text);
### Apply image background
//$im = imagecreatefrompng(Config()->ROOT_PATH . 'web/css/captcha/captcha.png');
//imagecopy($final_img, $im, $dst_x, $dst_y, 0, 0, 75, 75);
### Convert HTML backgound color to RGB
if( preg_match( "/([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/i", $options['backgroundcolor'], $bgrgb ) )
{$bgred = hexdec( $bgrgb[1] ); $bggreen = hexdec( $bgrgb[2] ); $bgblue = hexdec( $bgrgb[3] );}
### Convert HTML text color to RGB
if( preg_match( "/([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/i", $options['textcolor'], $textrgb ) )
{$textred = hexdec( $textrgb[1] ); $textgreen = hexdec( $textrgb[2] ); $textblue = hexdec( $textrgb[3] );}
### Create image
$im = imagecreate( $options['imagewidth'], $options['imageheight'] );
### Declare image's background color
$bgcolor = imagecolorallocatealpha($im,255,255,255,127);
### Declare image's text color
$fontcolor = imagecolorallocate($im, $textred,$textgreen,$textblue);
imagealphablending($im,true);
### Get exact dimensions of text string
$box = @imageTTFBbox($options['fontsize'],$options['fontangle'],$options['font'],$text);
### Get width of text from dimensions
$textwidth = abs($box[4] - $box[0]);
### Get height of text from dimensions
$textheight = abs($box[5] - $box[1]);
### Get x-coordinate of centered text horizontally using length of the image and length of the text
$xcord = ($options['imagewidth']/2)-($textwidth/2)-2;
### Get y-coordinate of centered text vertically using height of the image and height of the text
$ycord = ($options['imageheight']/2)+($textheight/2);
### Declare completed image with colors, font, text, and text location
imagettftext ( $im, $options['fontsize'], $options['fontangle'], $xcord, $ycord, $fontcolor, $options['font'], $text );
### Display completed image as PNG
imagealphablending($im,false);
imagesavealpha($im,true);
header('Content-Type: image/png');
imagepng($im);
### Close the image
imagedestroy($im);
}
helper_captcha();
?>