1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 5.1.6 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
16 // ------------------------------------------------------------------------
19 * CodeIgniter CAPTCHA Helper
21 * @package CodeIgniter
24 * @author ExpressionEngine Dev Team
25 * @link http://codeigniter.com/user_guide/helpers/xml_helper.html
28 // ------------------------------------------------------------------------
34 * @param array array of data for the CAPTCHA
35 * @param string path to create the image in
36 * @param string URL to the CAPTCHA image folder
37 * @param string server path to font
40 if ( ! function_exists('create_captcha'))
42 function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '')
44 $defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200);
46 foreach ($defaults as $key => $val)
48 if ( ! is_array($data))
50 if ( ! isset($$key) OR $$key == '')
57 $$key = ( ! isset($data[$key])) ? $val : $data[$key];
61 if ($img_path == '' OR $img_url == '')
66 if ( ! @is_dir($img_path))
71 if ( ! is_writable($img_path))
76 if ( ! extension_loaded('gd'))
81 // -----------------------------------
83 // -----------------------------------
85 list($usec, $sec) = explode(" ", microtime());
86 $now = ((float)$usec + (float)$sec);
88 $current_dir = @opendir($img_path);
90 while ($filename = @readdir($current_dir))
92 if ($filename != "." and $filename != ".." and $filename != "index.html")
94 $name = str_replace(".jpg", "", $filename);
96 if (($name + $expiration) < $now)
98 @unlink($img_path.$filename);
103 @closedir($current_dir);
105 // -----------------------------------
106 // Do we have a "word" yet?
107 // -----------------------------------
111 $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
114 for ($i = 0; $i < 8; $i++)
116 $str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
122 // -----------------------------------
123 // Determine angle and position
124 // -----------------------------------
126 $length = strlen($word);
127 $angle = ($length >= 6) ? rand(-($length-6), ($length-6)) : 0;
128 $x_axis = rand(6, (360/$length)-16);
129 $y_axis = ($angle >= 0 ) ? rand($img_height, $img_width) : rand(6, $img_height);
131 // -----------------------------------
133 // -----------------------------------
135 // PHP.net recommends imagecreatetruecolor(), but it isn't always available
136 if (function_exists('imagecreatetruecolor'))
138 $im = imagecreatetruecolor($img_width, $img_height);
142 $im = imagecreate($img_width, $img_height);
145 // -----------------------------------
147 // -----------------------------------
149 $bg_color = imagecolorallocate ($im, 255, 255, 255);
150 $border_color = imagecolorallocate ($im, 153, 102, 102);
151 $text_color = imagecolorallocate ($im, 204, 153, 153);
152 $grid_color = imagecolorallocate($im, 255, 182, 182);
153 $shadow_color = imagecolorallocate($im, 255, 240, 240);
155 // -----------------------------------
156 // Create the rectangle
157 // -----------------------------------
159 ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color);
161 // -----------------------------------
162 // Create the spiral pattern
163 // -----------------------------------
171 for ($i = 0; $i < ($circles * $points) - 1; $i++)
173 $theta = $theta + $thetac;
174 $rad = $radius * ($i / $points );
175 $x = ($rad * cos($theta)) + $x_axis;
176 $y = ($rad * sin($theta)) + $y_axis;
177 $theta = $theta + $thetac;
178 $rad1 = $radius * (($i + 1) / $points);
179 $x1 = ($rad1 * cos($theta)) + $x_axis;
180 $y1 = ($rad1 * sin($theta )) + $y_axis;
181 imageline($im, $x, $y, $x1, $y1, $grid_color);
182 $theta = $theta - $thetac;
185 // -----------------------------------
187 // -----------------------------------
189 $use_font = ($font_path != '' AND file_exists($font_path) AND function_exists('imagettftext')) ? TRUE : FALSE;
191 if ($use_font == FALSE)
194 $x = rand(0, $img_width/($length/3));
200 $x = rand(0, $img_width/($length/1.5));
204 for ($i = 0; $i < strlen($word); $i++)
206 if ($use_font == FALSE)
208 $y = rand(0 , $img_height/2);
209 imagestring($im, $font_size, $x, $y, substr($word, $i, 1), $text_color);
210 $x += ($font_size*2);
214 $y = rand($img_height/2, $img_height-3);
215 imagettftext($im, $font_size, $angle, $x, $y, $text_color, $font_path, substr($word, $i, 1));
221 // -----------------------------------
223 // -----------------------------------
225 imagerectangle($im, 0, 0, $img_width-1, $img_height-1, $border_color);
227 // -----------------------------------
228 // Generate the image
229 // -----------------------------------
231 $img_name = $now.'.jpg';
233 ImageJPEG($im, $img_path.$img_name);
235 $img = "<img src=\"$img_url$img_name\" width=\"$img_width\" height=\"$img_height\" style=\"border:0;\" alt=\" \" />";
239 return array('word' => $word, 'time' => $now, 'image' => $img);
243 // ------------------------------------------------------------------------
245 /* End of file captcha_helper.php */
246 /* Location: ./system/heleprs/captcha_helper.php */