Ver Mensaje Individual
  #1 (permalink)  
Antiguo 07/02/2009, 06:37
mariano_donati
 
Fecha de Ingreso: marzo-2005
Mensajes: 1.418
Antigüedad: 19 años
Puntos: 9
[APORTE] Clase que modela un captcha

¿Qué hace?
Nos sirve para generar un captcha para evitar que robots ingresen en nuestro sistema.

Características a destacar
  • Cadena aleatoria de 6 carac. (digitos y letras mayusculas/minusculas)
  • Los caracteres se muestran en diferentes angulos de manera aleatoria
  • Grilla y figuras de fondo para dificultar la comprensión del texto por medio de máquinas
  • Tamaño de celdas de grilla aleatorio
¿Cómo se usa?
Código php:
Ver original
  1. $captcha = new Captcha();
  2. $captcha->create();
  3. $captcha->send();

Futuras actualizaciones
  • Agregar efectos al texto
  • Color del texto, grilla, fondo y figuras de fondo aleatorio
Código
Código php:
Ver original
  1. define('IM_SIZE_WIDTH', 200);
  2. define('IM_SIZE_HEIGHT', 40);
  3. define('IM_XSTART', 10);
  4. define('IM_ELEMENTS_OFFSET', 20);
  5. define('IM_RANDOM_POLYGONS', 5);
  6. define('IM_POLYGON_POINTS', 3);
  7. define('IM_RANDOMSTR_LENGTH', 6);
  8. define('IM_SYMBOLS_UBOUND', 59);
  9. define('IM_FONTSIZE', 26);
  10. define('IM_CHAR_ANGLE', 8);
  11. define('IM_STRING_XPOS', 24);
  12. define('IM_STRING_YPOS', 32);
  13. define('IM_RULECELL_MAXWIDTH', 20);
  14. define('IM_RULECELL_MAXHEIGHT', 10);
  15.  
  16. class Captcha
  17. {
  18.  
  19.     private $im;
  20.     private $textcolor;
  21.     private $polcolor;
  22.     private $bgcolor;
  23.  
  24.     public function __construct()
  25.     {
  26.  
  27.         $this->im = imagecreatetruecolor(IM_SIZE_WIDTH, IM_SIZE_HEIGHT);
  28.         $this->textcolor = imagecolorallocate($this->im, 140, 255, 220);
  29.         $this->polcolor = imagecolorallocate($this->im, 196, 225, 225);
  30.         $this->bgcolor = imagecolorallocate($this->im, 135, 194, 194);
  31.     }
  32.    
  33.     public function create()
  34.     {
  35.         $this->drawBg();
  36.         $this->drawRandomPolygons();
  37.         $this->drawRule();
  38.         $this->drawRandomString();
  39.     }
  40.    
  41.     public function send()
  42.     {
  43.         header('Content-type: image/png');
  44.         imagepng($this->im);
  45.         imagedestroy($this->im);    
  46.     }
  47.  
  48.     public static function test($postvarname)
  49.     {
  50.         return ($_SESSION['captcha'] == md5($postvarname));
  51.     }    
  52.  
  53.     private function drawBg()
  54.     {
  55.         imagefilledrectangle($this->im, 0, 0, IM_SIZE_WIDTH, IM_SIZE_HEIGHT, $this->bgcolor);
  56.     }
  57.    
  58.     private function drawRandomPolygons()
  59.     {
  60.         $xstart;
  61.         $polygons_drawn = 0;
  62.        
  63.         $xstart = mt_rand(IM_XSTART, IM_ELEMENTS_OFFSET);
  64.         while($polygons_drawn <= IM_RANDOM_POLYGONS)
  65.         {
  66.             imagefilledpolygon($this->im,
  67.                                array(
  68.                                         $xstart, mt_rand(5, IM_SIZE_HEIGHT),
  69.                                      $xstart + mt_rand(5, IM_ELEMENTS_OFFSET), mt_rand(5, IM_SIZE_HEIGHT),
  70.                                      $xstart - mt_rand(5, IM_ELEMENTS_OFFSET), mt_rand(5, IM_SIZE_HEIGHT)
  71.                                      ),
  72.                                      IM_POLYGON_POINTS,
  73.                                      $this->polcolor
  74.                               );
  75.             $xstart += IM_ELEMENTS_OFFSET + (IM_ELEMENTS_OFFSET / 2);
  76.             $polygons_drawn++;                  
  77.         }
  78.     }
  79.    
  80.     private function drawRule()
  81.     {
  82.         $xoffset;
  83.         $yoffset;
  84.        
  85.         while($xpos <= IM_SIZE_WIDTH)
  86.         {
  87.             $xoffset = mt_rand(1, IM_RULECELL_MAXWIDTH);  // 1 COMO VALOR MINIMO PARA QUE NO SE ESCRIBA UNA LINEA ENCIMA DE OTRA
  88.             imageline($this->im, $xpos, 0, $xpos, IM_SIZE_HEIGHT, $this->textcolor);  
  89.             $xpos += $xoffset;
  90.         }
  91.        
  92.         while($ypos <= IM_SIZE_HEIGHT)
  93.         {
  94.             $yoffset = mt_rand(1, IM_RULECELL_MAXHEIGHT); // 1 COMO VALOR MINIMO PARA QUE NO SE ESCRIBA UNA LINEA ENCIMA DE OTRA
  95.             imageline($this->im, 0, $ypos, IM_SIZE_WIDTH, $ypos, $this->textcolor);
  96.             $ypos += $yoffset;
  97.         }        
  98.     }
  99.    
  100.     private function drawRandomString()
  101.     {
  102.         $symbols = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  103.         $randomstr;
  104.         $chars;
  105.         $char;
  106.        
  107.         $chars = 0;
  108.         $xpos = IM_STRING_XPOS;                    // COMIENZA A ESCRIBIR EN ESTA POSICION
  109.         while($chars < IM_RANDOMSTR_LENGTH)
  110.         {
  111.             $char = $symbols[(int)mt_rand(0, IM_SYMBOLS_UBOUND)];
  112.             $randomstr .= $char;
  113.             imagettftext($this->im, IM_FONTSIZE, mt_rand(-IM_CHAR_ANGLE, IM_CHAR_ANGLE), $xpos, IM_STRING_YPOS, $this->textcolor, 'TTF/Vera.ttf', $char);
  114.             $xpos += IM_FONTSIZE;
  115.             $chars++;
  116.         }
  117.        
  118.         // GUARDAMOS ENCRIPTADA LA CADENA GENERADA
  119.         $_SESSION['captcha'] = md5($randomstr);
  120.     }
  121.  
  122. }

Espero que a alguien le sea útil esta otra alternativa para generar un captcha. Cualquier sugerencia, duda, mejora, crítica, etc. haganmelo saber.
Saludos.
__________________
Add, never Remove