captcha_function.php
1 <?php
2
/**
3  * Create CAPTCHA
4  *
5  * @access    public
6  * @param    array    array of data for the CAPTCHA
7  * @param    string    path to create the image in
8  * @param    string    URL to the CAPTCHA image folder
9  * @param    string    server path to font
10  * @link        http://codeigniter.com/user_guide/helpers/xml_helper.html
11  * @return    string
12  */
13
function captcha_function($data ''$img_path ''$img_url ''$font_path '') {
14     
// The captcha function requires the GD image library.
15     // Only the img_path and img_url are required.
16     // If a "word" is not supplied, the function will generate a random ASCII string. You might put together your own word library that you can draw randomly from.
17     // If you do not specify a path to a TRUE TYPE font, the native ugly GD font will be used.
18     // The "captcha" folder must be writable (666, or 777)
19     // The "expiration" (in seconds) signifies how long an image will remain in the captcha folder before it will be deleted. The default is two hours.
20     
$defaults = array(
21         
'word' => ''
22         
'img_path' => ''
23         
'img_url' => ''
24         
'img_width' => '150'
25         
'img_height' => '30'
26         
'font_path' => ''
27         
'expiration' => 7200
28     
);
29
30     foreach (
$defaults as $key => $val) {
31         if ( ! 
is_array($data)) {
32             if ( ! isset($
$key) || $$key == '') {
33                 $
$key $val;
34             }
35         } else {
36             $
$key = ( ! isset($data[$key])) ? $val $data[$key];
37         }
38     }
39
40     if (
$img_path == '' || $img_url == '') {
41         return 
FALSE;
42     }
43
44     if ( ! @
is_dir($img_path)) {
45         return 
FALSE;
46     }
47
48     if ( ! 
is_writable($img_path)) {
49         return 
FALSE;
50     }
51
52     if ( ! 
extension_loaded('gd')) {
53         return 
FALSE;
54     }
55
56     
// -----------------------------------
57     // Remove old images
58     // -----------------------------------
59
60     
list($usec$sec) = explode(" "microtime());
61     
$now = ((float)$usec + (float)$sec);
62
63     
$current_dir = @opendir($img_path);
64
65     while(
$filename = @readdir($current_dir)) {
66         if (
$filename != "." && $filename != ".." && $filename != "index.html") {
67             
$name str_replace(".jpg"""$filename);
68             
69             
// The expired images will be deleted when this function is called again
70             
if (($name $expiration) < $now) {
71                 @
unlink($img_path.$filename);
72             }
73         }
74     }
75
76     @
closedir($current_dir);
77
78     
// -----------------------------------
79     // Do we have a "word" yet?
80     // -----------------------------------
81
82    
if ($word == '') {
83         
$pool '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
84
85         
$str '';
86         for (
$i 0$i 8$i++) {
87             
$str .= substr($poolmt_rand(0strlen($pool) -1), 1);
88         }
89
90         
$word $str;
91    }
92
93     
// -----------------------------------
94     // Determine angle and position
95     // -----------------------------------
96
97     
$length    strlen($word);
98     
$angle = ($length >= 6) ? rand(-($length-6), ($length-6)) : 0;
99     
$x_axis    rand(6, (360/$length)-16);
100     
$y_axis = ($angle >= ) ? rand($img_height$img_width) : rand(6$img_height);
101
102     
// -----------------------------------
103     // Create image
104     // -----------------------------------
105
106     // PHP.net recommends imagecreatetruecolor(), but it isn't always available
107     
if (function_exists('imagecreatetruecolor')) {
108         
$im imagecreatetruecolor($img_width$img_height);
109     } else {
110         
$im imagecreate($img_width$img_height);
111     }
112
113     
// -----------------------------------
114     //  Assign colors
115     // -----------------------------------
116
117     
$bg_color imagecolorallocate ($im255255255);
118     
$border_color imagecolorallocate ($im153102102);
119     
$text_color imagecolorallocate ($im204153153);
120     
$grid_color    imagecolorallocate($im255182182);
121     
$shadow_color imagecolorallocate($im255240240);
122
123     
// -----------------------------------
124     //  Create the rectangle
125     // -----------------------------------
126
127     
ImageFilledRectangle($im00$img_width$img_height$bg_color);
128
129     
// -----------------------------------
130     //  Create the spiral pattern
131     // -----------------------------------
132
133     
$theta 1;
134     
$thetac 7;
135     
$radius 16;
136     
$circles 20;
137     
$points 32;
138
139     for (
$i 0$i < ($circles $points) - 1$i++) {
140         
$theta $theta $thetac;
141         
$rad $radius * ($i $points );
142         
$x = ($rad cos($theta)) + $x_axis;
143         
$y = ($rad sin($theta)) + $y_axis;
144         
$theta $theta $thetac;
145         
$rad1 $radius * (($i 1) / $points);
146         
$x1 = ($rad1 cos($theta)) + $x_axis;
147         
$y1 = ($rad1 sin($theta )) + $y_axis;
148         
imageline($im$x$y$x1$y1$grid_color);
149         
$theta $theta $thetac;
150     }
151
152     
// -----------------------------------
153     //  Write the text
154     // -----------------------------------
155
156     
$use_font = ($font_path != '' && file_exists($font_path) && function_exists('imagettftext')) ? TRUE FALSE;
157
158     if (
$use_font == FALSE) {
159         
$font_size 5;
160         
$x rand(0$img_width/($length/3));
161         
$y 0;
162     } else {
163         
$font_size    16;
164         
$x rand(0$img_width/($length/1.5));
165         
$y $font_size+2;
166     }
167
168     for (
$i 0$i strlen($word); $i++) {
169         if (
$use_font == FALSE) {
170             
$y rand($img_height/2);
171             
imagestring($im$font_size$x$ysubstr($word$i1), $text_color);
172             
$x += ($font_size*2);
173         } else {
174             
$y rand($img_height/2$img_height-3);
175             
imagettftext($im$font_size$angle$x$y$text_color$font_pathsubstr($word$i1));
176             
$x += $font_size;
177         }
178     }
179
180
181     
// -----------------------------------
182     //  Create the border
183     // -----------------------------------
184
185     
imagerectangle($im00$img_width-1$img_height-1$border_color);
186
187     
// -----------------------------------
188     //  Generate the image
189     // -----------------------------------
190
191     
$img_name $now.'.jpg';
192
193     
ImageJPEG($im$img_path.$img_name);
194
195     
$img "<img src=\"$img_url$img_name\" width=\"$img_width\" height=\"$img_height\" style=\"border:0;\" alt=\" \" />";
196
197     
ImageDestroy($im);
198
199     return array(
200         
'word' => $word
201         
'time' => $now
202         
'image' => $img
203     
);
204 }
205
206
/* End of file: ./system/functions/captcha_function.php */

Page URI: http://www.infopotato.com/index.php/code/function/captcha/