Changeset 3102

Show
Ignore:
Timestamp:
07/13/2008 05:26:17 AM (5 months ago)
Author:
OscarB
Message:

Captcha, updates to allow using multiple fonts

Location:
trunk/system
Files:
5 modified

Legend:

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

    r3080 r3102  
    1313 *  complexity - Difficulty level (0-10), usage depends on chosen style 
    1414 *  background - Path to background image file 
    15  *  font       - Path to font file 
     15 *  fontpath   - Path to font folder 
     16 *  fonts      - Font files 
    1617 *  promote    - Valid response count threshold to promote user (FALSE to disable) 
    1718 */ 
     
    2324        'complexity' => 4, 
    2425        'background' => '', 
    25         'font'       => SYSPATH.'fonts/DejaVuSerif.ttf', 
     26        'fontpath'   => SYSPATH.'fonts/', 
     27        'fonts'      => array('DejaVuSerif.ttf'), 
    2628        'promote'    => FALSE, 
    2729); 
  • trunk/system/libraries/Captcha.php

    r3101 r3102  
    2626                'complexity' => 4, 
    2727                'background' => '', 
    28                 'font'       => '', 
     28                'fontpath'   => '', 
     29                'fonts'      => array(), 
    2930                'promote'    => FALSE, 
    3031        ); 
     
    109110                } 
    110111 
    111                 // If using a font, check if it exists 
    112                 if ( ! empty($config['font'])) 
    113                 { 
    114                         self::$config['font'] = str_replace('\\', '/', realpath($config['font'])); 
    115  
    116                         if ( ! file_exists(self::$config['font'])) 
    117                                 throw new Kohana_Exception('captcha.file_not_found', self::$config['font']); 
     112                // If using any fonts, check if they exist 
     113                if ( ! empty($config['fonts'])) 
     114                { 
     115                        if (count($config['fonts']) === 1) 
     116                        { 
     117                                if ( ! file_exists(self::$config['fontpath'].self::$config['fonts'][0])) 
     118                                        throw new Kohana_Exception('captcha.file_not_found', self::$config['fontpath'].self::$config['fonts'][0]); 
     119                        } 
     120                        else 
     121                        { 
     122                                for ($i = 0; $i < count($config['fonts']); $i++) 
     123                                { 
     124                                        if ( ! file_exists(self::$config['fontpath'].self::$config['fonts'][$i])) 
     125                                                throw new Kohana_Exception('captcha.file_not_found', self::$config['fontpath'].self::$config['fonts'][$i]); 
     126                                } 
     127                        } 
    118128                } 
    119129 
  • trunk/system/libraries/drivers/Captcha/Alpha.php

    r3096 r3102  
    6161                for ($i = 0, $strlen = strlen($this->response); $i < $strlen; $i++) 
    6262                { 
     63                        // Use different fonts if available 
     64                        $font = Captcha::$config['fontpath'].Captcha::$config['fonts'][(mt_rand(0, (count(Captcha::$config['fonts']) - 1)))]; 
     65                         
    6366                        $angle = mt_rand(-40, 20); 
    6467                        // Scale the character size on image height 
    6568                        $size = $default_size / 10 * mt_rand(8, 12); 
    66                         $box = imageftbbox($size, $angle, Captcha::$config['font'], $this->response[$i]); 
     69                        $box = imageftbbox($size, $angle, $font, $this->response[$i]); 
    6770 
    6871                        // Calculate character starting coordinates 
     
    7578 
    7679                        // Write text character to image 
    77                         imagefttext($this->image, $size, $angle, $x, $y, $color, Captcha::$config['font'], $this->response[$i]); 
     80                        imagefttext($this->image, $size, $angle, $x, $y, $color, $font, $this->response[$i]); 
    7881 
    7982                        // Draw "ghost" alphabetic character 
    8083                        $text_color = imagecolorallocatealpha($this->image, mt_rand($color_limit + 8, 255), mt_rand($color_limit + 8, 255), mt_rand($color_limit + 8, 255), mt_rand(70, 120)); 
    8184                        $char = substr($chars, mt_rand(0, 14), 1); 
    82                         imagettftext($this->image, $size * 2, mt_rand(-45, 45), ($x - (mt_rand(5, 10))), ($y + (mt_rand(5, 10))), $text_color, Captcha::$config['font'], $char); 
     85                        imagettftext($this->image, $size * 2, mt_rand(-45, 45), ($x - (mt_rand(5, 10))), ($y + (mt_rand(5, 10))), $text_color, $font, $char); 
    8386                } 
    8487 
  • trunk/system/libraries/drivers/Captcha/Basic.php

    r3096 r3102  
    5656                for ($i = 0, $strlen = strlen($this->response); $i < $strlen; $i++) 
    5757                { 
     58                        // Use different fonts if available 
     59                        $font = Captcha::$config['fontpath'].Captcha::$config['fonts'][(mt_rand(0, (count(Captcha::$config['fonts']) - 1)))]; 
     60                         
    5861                        // Allocate random color, size and rotation attributes to text 
    5962                        $color = imagecolorallocate($this->image, mt_rand(0, 150), mt_rand(0, 150), mt_rand(0, 150)); 
     
    6265                        // Scale the character size on image height 
    6366                        $size = $default_size / 10 * mt_rand(8, 12); 
    64                         $box = imageftbbox($size, $angle, Captcha::$config['font'], $this->response[$i]); 
     67                        $box = imageftbbox($size, $angle, $font, $this->response[$i]); 
    6568 
    6669                        // Calculate character starting coordinates 
     
    6972 
    7073                        // Write text character to image 
    71                         imagefttext($this->image, $size, $angle, $x, $y, $color, Captcha::$config['font'], $this->response[$i]); 
     74                        imagefttext($this->image, $size, $angle, $x, $y, $color, $font, $this->response[$i]); 
    7275                } 
    7376 
  • trunk/system/libraries/drivers/Captcha/Black.php

    r3097 r3102  
    4444                } 
    4545 
     46                // Use different fonts if available 
     47                $font = Captcha::$config['fontpath'].Captcha::$config['fonts'][(mt_rand(0, (count(Captcha::$config['fonts']) - 1)))]; 
     48 
    4649                // Draw the character's white shadows 
    4750                $size = (int) min(Captcha::$config['height'] / 2, Captcha::$config['width'] * 0.8 / strlen($this->response)); 
     
    5053                $y = ((Captcha::$config['height'] - $size) / 2) + $size; 
    5154                $color = imagecolorallocate($this->image, 255, 255, 255); 
    52                 imagefttext($this->image, $size, $angle, $x + 1, $y + 1, $color, Captcha::$config['font'], $this->response); 
     55                imagefttext($this->image, $size, $angle, $x + 1, $y + 1, $color, $font, $this->response); 
    5356 
    5457                // Add more shadows for lower complexities 
    55                 (Captcha::$config['complexity'] < 10) and imagefttext($this->image, $size, $angle, $x - 1, $y - 1, $color, Captcha::$config['font'], $this->response); 
    56                 (Captcha::$config['complexity'] < 8)  and imagefttext($this->image, $size, $angle, $x - 2, $y + 2, $color, Captcha::$config['font'], $this->response); 
    57                 (Captcha::$config['complexity'] < 6)  and imagefttext($this->image, $size, $angle, $x + 2, $y - 2, $color, Captcha::$config['font'], $this->response); 
    58                 (Captcha::$config['complexity'] < 4)  and imagefttext($this->image, $size, $angle, $x + 3, $y + 3, $color, Captcha::$config['font'], $this->response); 
    59                 (Captcha::$config['complexity'] < 2)  and imagefttext($this->image, $size, $angle, $x - 3, $y - 3, $color, Captcha::$config['font'], $this->response); 
     58                (Captcha::$config['complexity'] < 10) and imagefttext($this->image, $size, $angle, $x - 1, $y - 1, $color, $font , $this->response); 
     59                (Captcha::$config['complexity'] < 8)  and imagefttext($this->image, $size, $angle, $x - 2, $y + 2, $color, $font , $this->response); 
     60                (Captcha::$config['complexity'] < 6)  and imagefttext($this->image, $size, $angle, $x + 2, $y - 2, $color, $font , $this->response); 
     61                (Captcha::$config['complexity'] < 4)  and imagefttext($this->image, $size, $angle, $x + 3, $y + 3, $color, $font , $this->response); 
     62                (Captcha::$config['complexity'] < 2)  and imagefttext($this->image, $size, $angle, $x - 3, $y - 3, $color, $font , $this->response); 
    6063 
    6164                // Finally draw the foreground characters 
    6265                $color = imagecolorallocate($this->image, 0, 0, 0); 
    63                 imagefttext($this->image, $size, $angle, $x, $y, $color, Captcha::$config['font'], $this->response); 
     66                imagefttext($this->image, $size, $angle, $x, $y, $color, $font, $this->response); 
    6467 
    6568                // Output