Código PHP:
<?php
class Captcha
{
private $width;
private $height;
private $texto;
private $imagen;
private $fuente;
private $color_fondo;
private $color_negro;
private $color_inv;
private $rgb=array();
public function __construct($width,$height)
{
$this->width=$width;
$this->height=$height;
$this->imagen = imagecreatetruecolor($this->width,$this->height);
}
// Si no dominais el uso del imagefttext dejar el valor size_font por default.
public function configuracion($string,$num_caracteres,$font="monofont.ttf",$size_font=20)
{
$this->color_fondo=$color_fondo;
$this->color_negro=$color_negro;
$this->fuente=$fuente;
$this->rgb[0]=mt_rand(0,255);
$this->rgb[1]=mt_rand(0,255);
$this->rgb[2]=mt_rand(0,255);
if($num_caracteres > 6)
{
echo "El numero maximo de caracteres es 6";
exit();
}
$color_fondo=imagecolorallocate($this->imagen,$this->rgb[0],$this->rgb[1],$this->rgb[2]);
$color_negro=imagecolorallocate($this->imagen,0,0,0);
$this->texto=$string;
$string = substr(str_replace("O","",str_replace(0,"",strtoupper(md5(mt_rand(9999,99999))))),0,$num_caracteres);
imagefill($this->imagen,0,0,$color_fondo);
imagefttext($this->imagen,$size_font,mt_rand(-10,10),10,25,$color_negro,$font,$string);
}
public function seguridad($nivel)
{
$this->color_inv=$color_random;
$color_random=imagecolorallocate($this->imagen,255-$this->rgb[0],255-$this->rgb[1],255-$this->rgb[2]);
switch($nivel)
{
case 0:
break;
case 1:
imageline($this->imagen,15,0,25,$this->height,$color_random);
imageline($this->imagen,30,0,5,$this->height,$color_random);
imageline($this->imagen,30,0,$this->width,$this->height/2,$color_random);
imageline($this->imagen,0,0,$this->width,$this->height,$color_random);
imageline($this->imagen,$this->width,0,25,$this->height,$color_random);
break;
case 2:
for($w=0;$w <= 700;$w++){
$ancho_pix=mt_rand(0,$this->width);
$largo_pix=mt_rand(0,$this->height);
imagesetpixel($this->imagen,$ancho_pix,$largo_pix,$color_random);
}
break;
case 3:
imageline($this->imagen,15,0,25,$this->height,$color_random);
imageline($this->imagen,30,0,5,$this->height,$color_random);
imageline($this->imagen,30,0,$this->width,$this->height/2,$color_random);
imageline($this->imagen,0,0,$this->width,$this->height,$color_random);
imageline($this->imagen,$this->width,0,25,$this->height,$color_random);
for($w=0;$w <= 700;$w++){
$ancho_pix=mt_rand(0,$this->width);
$largo_pix=mt_rand(0,$this->height);
imagesetpixel($this->imagen,$ancho_pix,$largo_pix,$color_random);
}
break;
default:
print("El nivel maximo de seguridad es 3");
break;
}
}
public function visualizar()
{
header("Content-type: image/png");
imagepng($this->imagen);
imagedestroy($this->imagen);
}
}
$captcha=new Captcha(80,35);
$captcha->configuracion("Hola",6); // El valor default es 5
$captcha->seguridad(3);
$captcha->visualizar();
?>