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ódigo php:
Ver original
$captcha = new Captcha(); $captcha->create(); $captcha->send();
Futuras actualizaciones
- Agregar efectos al texto
- Color del texto, grilla, fondo y figuras de fondo aleatorio
Código php:
Ver original
class Captcha { private $im; private $textcolor; private $polcolor; private $bgcolor; public function __construct() { } public function create() { $this->drawBg(); $this->drawRandomPolygons(); $this->drawRule(); $this->drawRandomString(); } public function send() { } public static function test($postvarname) { } private function drawBg() { } private function drawRandomPolygons() { $xstart; $polygons_drawn = 0; while($polygons_drawn <= IM_RANDOM_POLYGONS) { ), IM_POLYGON_POINTS, $this->polcolor ); $xstart += IM_ELEMENTS_OFFSET + (IM_ELEMENTS_OFFSET / 2); $polygons_drawn++; } } private function drawRule() { $xoffset; $yoffset; while($xpos <= IM_SIZE_WIDTH) { $xoffset = mt_rand(1, IM_RULECELL_MAXWIDTH); // 1 COMO VALOR MINIMO PARA QUE NO SE ESCRIBA UNA LINEA ENCIMA DE OTRA $xpos += $xoffset; } while($ypos <= IM_SIZE_HEIGHT) { $yoffset = mt_rand(1, IM_RULECELL_MAXHEIGHT); // 1 COMO VALOR MINIMO PARA QUE NO SE ESCRIBA UNA LINEA ENCIMA DE OTRA $ypos += $yoffset; } } private function drawRandomString() { $symbols = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $randomstr; $chars; $char; $chars = 0; $xpos = IM_STRING_XPOS; // COMIENZA A ESCRIBIR EN ESTA POSICION while($chars < IM_RANDOMSTR_LENGTH) { $randomstr .= $char; imagettftext($this->im, IM_FONTSIZE, mt_rand(-IM_CHAR_ANGLE, IM_CHAR_ANGLE), $xpos, IM_STRING_YPOS, $this->textcolor, 'TTF/Vera.ttf', $char); $xpos += IM_FONTSIZE; $chars++; } // GUARDAMOS ENCRIPTADA LA CADENA GENERADA } }
Espero que a alguien le sea útil esta otra alternativa para generar un captcha. Cualquier sugerencia, duda, mejora, crítica, etc. haganmelo saber.
Saludos.