Changeset 3027
- Timestamp:
- 07/10/2008 05:20:53 AM (5 months ago)
- Location:
- trunk/system
- Files:
-
- 4 modified
-
controllers/captcha.php (modified) (1 diff)
-
libraries/Captcha.php (modified) (1 diff)
-
libraries/drivers/Captcha.php (modified) (6 diffs)
-
libraries/drivers/Captcha/Basic.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/system/controllers/captcha.php
r3016 r3027 17 17 { 18 18 // Output the Captcha challenge resource (no html) 19 $captcha = new Captcha; 20 $captcha->render(FALSE); 19 Captcha::factory()->render(FALSE); 21 20 } 22 21 -
trunk/system/libraries/Captcha.php
r3016 r3027 144 144 * 145 145 * @param boolean TRUE to output html, e.g. <img src="#" /> 146 * @return mixed 146 * @return mixed html string or void 147 147 */ 148 148 public function render($html = TRUE) -
trunk/system/libraries/drivers/Captcha.php
r3026 r3027 13 13 14 14 protected $image; // Image resource identifier 15 protected $image_type = 'png'; // 'png', 'gif' ,or 'jpeg'15 protected $image_type = 'png'; // 'png', 'gif' or 'jpeg' 16 16 17 17 /** … … 71 71 * If a background image is supplied, the image dimensions are used. 72 72 * 73 * @chainable74 73 * @param string path to the background image file 75 * @return object74 * @return void 76 75 */ 77 76 public function image_create($background = NULL) … … 91 90 $this->image = imagecreatetruecolor(Captcha::$config['width'], Captcha::$config['height']); 92 91 } 93 94 return $this;95 92 } 96 93 … … 98 95 * Fills the background with a gradient. 99 96 * 100 * @chainable 101 * @return object 97 * @param resource gd image color identifier for start color 98 * @param resource gd image color identifier for end color 99 * @param string direction: 'horizontal' or 'vertical', 'random' by default 100 * @return void 102 101 */ 103 public function image_gradient( )102 public function image_gradient($color1, $color2, $direction = NULL) 104 103 { 105 // TODO: build 106 return $this; 104 $directions = array('horizontal', 'vertical'); 105 106 // Pick a random direction if needed 107 if ( ! in_array($direction, $directions)) 108 { 109 $direction = $directions[array_rand($directions)]; 110 111 // Switch colors 112 if (mt_rand(0, 1) === 1) 113 { 114 $temp = $color1; 115 $color1 = $color2; 116 $color2 = $temp; 117 } 118 } 119 120 // Extract RGB values 121 $color1 = imagecolorsforindex($this->image, $color1); 122 $color2 = imagecolorsforindex($this->image, $color2); 123 124 // Preparations for the gradient loop 125 $steps = ($direction === 'horizontal') ? Captcha::$config['width'] : Captcha::$config['height']; 126 127 $r1 = ($color1['red'] - $color2['red']) / $steps; 128 $g1 = ($color1['green'] - $color2['green']) / $steps; 129 $b1 = ($color1['blue'] - $color2['blue']) / $steps; 130 131 if ($direction === 'horizontal') 132 { 133 $x1 =& $i; 134 $y1 = 0; 135 $x2 =& $i; 136 $y2 = Captcha::$config['height']; 137 } 138 else 139 { 140 $x1 = 0; 141 $y1 =& $i; 142 $x2 = Captcha::$config['width']; 143 $y2 =& $i; 144 } 145 146 // Execute the gradient loop 147 for ($i = 0; $i <= $steps; $i++) 148 { 149 $r2 = $color1['red'] - floor($i * $r1); 150 $g2 = $color1['green'] - floor($i * $g1); 151 $b2 = $color1['blue'] - floor($i * $b1); 152 $color = imagecolorallocate($this->image, $r2, $g2, $b2); 153 154 imageline($this->image, $x1, $y1, $x2, $y2, $color); 155 } 107 156 } 108 157 … … 110 159 * Outputs the image to the browser. 111 160 * 112 * @return void 161 * @param boolean html output 162 * @return mixed html string or void 113 163 */ 114 public function image_ output()164 public function image_render($html) 115 165 { 166 // Output html element 167 if ($html) 168 return '<img alt="Captcha" src="'.url::site('captcha').'" width="'.Captcha::$config['width'].'" height="'.Captcha::$config['height'].'" />'; 169 116 170 // Send the correct HTTP header 117 171 header('Content-Type: image/'.$this->image_type); … … 125 179 } 126 180 127 /**128 * Generates html img element.129 *130 * @return string131 */132 public function image_html()133 {134 return '<img alt="Captcha" src="'.url::site('captcha').'" width="'.Captcha::$config['width'].'" height="'.Captcha::$config['height'].'" />';135 }136 137 181 } // End Captcha Driver -
trunk/system/libraries/drivers/Captcha/Basic.php
r3026 r3027 34 34 $this->image_create(Captcha::$config['background']); 35 35 36 // TODO: everything, font-size, spacing, colors, background, etc. 37 $color = imagecolorexact($this->image, 255, 255, 255); 38 imagefttext($this->image, 20, 5, 10, 40, $color, Captcha::$config['font'], Captcha::$answer); 36 // Add a random gradient 37 $color1 = imagecolorallocate($this->image, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100)); 38 $color2 = imagecolorallocate($this->image, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100)); 39 $this->image_gradient($color1, $color2); 40 41 // Add a few random lines 42 for ($i = 0, $count = mt_rand(5, 10); $i < $count; $i++) 43 { 44 $color = imagecolorallocatealpha($this->image, mt_rand(100, 255), mt_rand(100, 255), mt_rand(100, 255), mt_rand(60, 120)); 45 imageline($this->image, mt_rand(0, Captcha::$config['width']), mt_rand(0, Captcha::$config['height']), mt_rand(0, Captcha::$config['width']), mt_rand(0, Captcha::$config['height']), $color); 46 } 47 48 // Calculate character font-size and spacing 49 $default_size = min(Captcha::$config['width'], Captcha::$config['height'] * 2) / strlen(Captcha::$answer); 50 $spacing = (int) (Captcha::$config['width'] * 0.9 / strlen(Captcha::$answer)); 51 52 // Draw each Captcha character with varying attributes 53 for ($i = 0, $strlen = strlen(Captcha::$answer); $i < $strlen; $i++) 54 { 55 // Allocate random color, size and rotation attributes to text 56 $color = imagecolorallocate($this->image, mt_rand(150, 255), mt_rand(200, 255), mt_rand(0, 255)); 57 $angle = mt_rand(-40, 20); 58 59 // Scale the character size on image height 60 $size = $default_size / 10 * mt_rand(8, 12); 61 $box = imageftbbox($size, $angle, Captcha::$config['font'], Captcha::$answer[$i]); 62 63 // Calculate character starting coordinates 64 $x = $spacing / 4 + $i * $spacing; 65 $y = Captcha::$config['height'] / 2 + ($box[2] - $box[5]) / 4; 66 67 // Write text character to image 68 imagefttext($this->image, $size, $angle, $x, $y, $color, Captcha::$config['font'], Captcha::$answer[$i]); 69 } 39 70 40 71 // Output 41 return ($html) ? $this->image_html() : $this->image_output();72 return $this->image_render($html); 42 73 } 43 74
