Captcha Class

This is an captcha class which displays some random characters, in different colors.

The user has to enter all characters in blue. If there are no blue characters, the user must enter a dash (-).

Download Download script as zip

Tag Tags: GD PHP

Source

  • captcha.class.php
  • captcha.php
  1. <?php
  2. /**
  3.  * Lucky's Captcha v3
  4.  *
  5.  * This captcha shows some random numbers on an image, in different colors,
  6.  * the user has to enter the number of digits in a specific
  7.  *
  8.  * Copyright 2007 by Lucas van Dijk (info@return1.net)
  9.  * For more info see http://www.return1.net
  10.  *
  11.  * @package Captcha
  12.  * @category Captcha
  13.  * @license http://www.opensource.org/licenses/gpl-license.php
  14.  * @author Lucas van Dijk
  15.  */
  16. class Captcha
  17. {
  18.     protected $image;
  19.     protected $image_size;
  20.     protected $colors;
  21.     protected $code;   
  22.  
  23.     public function __construct($size = 120)
  24.     {
  25.         if(session_id() == '')
  26.         {
  27.             throw new Exception('The session has not yet been started, please use the function session_start() before calling this class.');
  28.         }
  29.        
  30.         // Create a new GD resource
  31.         $this -> image = imagecreatetruecolor($size, $size);
  32.         $this -> image_size = $size;
  33.         $this -> colors = array();
  34.        
  35.         // Fill the image with a white background
  36.         $color = $this -> allocate_color('FFFFFF');
  37.         imagefill($this -> image, 0, 0, $color);
  38.     }
  39.    
  40.     private function __toString()
  41.     {
  42.         return $this -> code;
  43.     }
  44.    
  45.     protected function allocate_color($color)
  46.     {
  47.         if(substr($color, 0, 1) == '#')
  48.         {
  49.             $color = substr($color, 1);
  50.         }
  51.        
  52.         if(array_key_exists($color, $this -> colors))
  53.         {
  54.             return $this -> colors[$color];
  55.         }
  56.         else
  57.         {
  58.             $parts = str_split($color, 2);
  59.             eval('$this -> colors[$color] = imagecolorallocate($this -> image, 0x'.$parts[0].', 0x'.$parts[1].', 0x'.$parts[2].');');
  60.             return $this -> colors[$color];
  61.         }
  62.     }
  63.    
  64.     public function generate()
  65.     {
  66.         // Generate some lines, to divide the image in some smaller pieces
  67.         $color = $this -> allocate_color('000000');
  68.         $number_of_columns = mt_rand(3,5);
  69.         $square_width = round($this -> image_size / $number_of_columns);
  70.        
  71.         for($i = 0; $i < $number_of_columns; $i++)
  72.         {
  73.             // Vertical line
  74.             imageline($this -> image, 0, $i * $square_width, imagesx($this -> image), $i * $square_width, $color);
  75.            
  76.             // Horizontal Line
  77.             imageline($this -> image, $i * $square_width, 0, $i * $square_width, imagesy($this -> image) - 1, $color);
  78.         }
  79.        
  80.         imageline($this -> image, 0, imagesy($this -> image) - 1, imagesx($this -> image) - 1, imagesy($this -> image) - 1, $color);
  81.         imageline($this -> image, imagesx($this -> image) - 1, 0, imagesx($this -> image) - 1, imagesy($this -> image) - 1, $color);
  82.        
  83.         // Add the numbers to the squares
  84.         // Colors we're going to use
  85.         $colors = array();
  86.         $colors[] = 'FF0000'; // red
  87.         $colors[] = '00FF00'; // green
  88.         $colors[] = '0000FF'; // blue
  89.         $colors[] = 'FF9900'; // Orange
  90.        
  91.         $chars = array_merge(range('a', 'z'), range(0, 9));
  92.    
  93.         $row = 0;
  94.         $column = 0;
  95.         while($row < $number_of_columns)
  96.         {
  97.             // Calculate X and Y
  98.             $x = (($square_width / 2) - (imagefontwidth(2) / 2)) + ($column * $square_width);
  99.             $y = (($square_width / 2) - (imagefontheight(2) / 2)) + ($row * $square_width);
  100.            
  101.             // Select a color we're going to use
  102.             $rand_color = mt_rand(0, count($colors) - 1);
  103.             $color = $this -> allocate_color($colors[$rand_color]);
  104.            
  105.             $char = $chars[array_rand($chars)];
  106.             if($colors[$rand_color] == '0000FF')
  107.             {              
  108.                 $this -> code .= $char;
  109.             }
  110.            
  111.             imagestring($this -> image, 2, $x, $y, $char, $color);
  112.            
  113.             $column++;
  114.  
  115.             if($column == $number_of_columns)
  116.             {
  117.                 $row++;
  118.                 $column = 0;
  119.             }
  120.         }
  121.     }
  122.    
  123.     public function show()
  124.     {
  125.         imagepng($this -> image);
  126.     }
  127.    
  128.     public function get_code()
  129.     {
  130.        return $this -> code;
  131.     }
  132. }
  1. <?php
  2. include 'includes/captcha.class.php';
  3.  
  4. $captcha = new Captcha();
  5.  
  6. $captcha -> generate();
  7.  
  8. $_SESSION['captcha_code'] = strlen($captcha -> get_code()) == 0 ? '-' : $captcha -> get_code();
  9.  
  10. header("Content-Type: image/png");
  11. $captcha -> show();
  12.