Changeset 3027

Show
Ignore:
Timestamp:
07/10/2008 05:20:53 AM (5 months ago)
Author:
Geert
Message:

Finished Basic Captcha Driver, and the image_gradient helper method.

Location:
trunk/system
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • trunk/system/controllers/captcha.php

    r3016 r3027  
    1717        { 
    1818                // Output the Captcha challenge resource (no html) 
    19                 $captcha = new Captcha; 
    20                 $captcha->render(FALSE); 
     19                Captcha::factory()->render(FALSE); 
    2120        } 
    2221 
  • trunk/system/libraries/Captcha.php

    r3016 r3027  
    144144         * 
    145145         * @param   boolean  TRUE to output html, e.g. <img src="#" /> 
    146          * @return  mixed 
     146         * @return  mixed    html string or void 
    147147         */ 
    148148        public function render($html = TRUE) 
  • trunk/system/libraries/drivers/Captcha.php

    r3026 r3027  
    1313 
    1414        protected $image;              // Image resource identifier 
    15         protected $image_type = 'png'; // 'png', 'gif', or 'jpeg' 
     15        protected $image_type = 'png'; // 'png', 'gif' or 'jpeg' 
    1616 
    1717        /** 
     
    7171         * If a background image is supplied, the image dimensions are used. 
    7272         * 
    73          * @chainable 
    7473         * @param   string  path to the background image file 
    75          * @return  object 
     74         * @return  void 
    7675         */ 
    7776        public function image_create($background = NULL) 
     
    9190                        $this->image = imagecreatetruecolor(Captcha::$config['width'], Captcha::$config['height']); 
    9291                } 
    93  
    94                 return $this; 
    9592        } 
    9693 
     
    9895         * Fills the background with a gradient. 
    9996         * 
    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 
    102101         */ 
    103         public function image_gradient() 
     102        public function image_gradient($color1, $color2, $direction = NULL) 
    104103        { 
    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                } 
    107156        } 
    108157 
     
    110159         * Outputs the image to the browser. 
    111160         * 
    112          * @return  void 
     161         * @param   boolean  html output 
     162         * @return  mixed    html string or void 
    113163         */ 
    114         public function image_output() 
     164        public function image_render($html) 
    115165        { 
     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 
    116170                // Send the correct HTTP header 
    117171                header('Content-Type: image/'.$this->image_type); 
     
    125179        } 
    126180 
    127         /** 
    128          * Generates html img element. 
    129          * 
    130          * @return  string 
    131          */ 
    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  
    137181} // End Captcha Driver 
  • trunk/system/libraries/drivers/Captcha/Basic.php

    r3026 r3027  
    3434                $this->image_create(Captcha::$config['background']); 
    3535 
    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                } 
    3970 
    4071                // Output 
    41                 return ($html) ? $this->image_html() : $this->image_output(); 
     72                return $this->image_render($html); 
    4273        } 
    4374