Nuestro conocimiento compartido. Nuestro tesoro compartido. Wikipedia.
Proyectos::Captcha
Permalink: http://www.treeweb.es/u/919/ 23/12/2010

Captcha

<? session_start(); $ANCHO = 350; $ALTO = 100; $SEPARACION = 5; $im = imagecreatetruecolor($ANCHO, $ALTO); $color1 = imagecolorallocatealpha($im, 0,0,0,0); $color2 = imagecolorallocatealpha($im, 255,255,255,0); imagealphablending($im, false); // Configuro el método de sustitución de píxeles //$fondo = imagecolorallocatealpha($im, 0, 0, 0, 127); imagefilledrectangle($im, 0, 0, $ANCHO, $ALTO, $color2); imagealphablending($im, true); // Configuro el método de sustitución de píxeles // Pinto las lineas for ($i=-1; $i<$ANCHO; $i+=$SEPARACION) { //imageline($im, $i, 0, $i, $ALTO, $color1); } for ($j=-1; $j<$ALTO; $j+=$SEPARACION) { imageline($im, 0, $j, $ANCHO, $j, $color1); } // Genero el texto $VOCALES = array('A', 'E', 'I', 'U'); $CONSONANTES = array('B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'V', 'W', 'Y', 'Z'); $NUMEROS = array('1', '3', '4', '6', '8', '9', ''); $texto = ''; $texto .= $CONSONANTES[rand(0,count($CONSONANTES)-1)]; $texto .= $VOCALES[rand(0,count($VOCALES)-1)]; $texto .= $CONSONANTES[rand(0,count($CONSONANTES)-1)]; $texto .= $VOCALES[rand(0,count($VOCALES)-1)]; $texto .= $NUMEROS[rand(0,count($NUMEROS)-1)]; $texto .= $NUMEROS[rand(0,count($NUMEROS)-1)]; $texto .= $NUMEROS[rand(0,count($NUMEROS)-1)]; $_SESSION['CAPTCHA'] = $texto; // Pinto el texto $margin_left = 30; $caracter_width = 45; for ($i=0; $i<strlen($texto); $i++) { imagettftext($im, 40, rand(-45,45), $margin_left + $i*$caracter_width, 60, $color2, 'fuentes/Arialbd.TTF', $texto[$i]); } // Envío información sobre el tipo de imagen: header("Content-Type: image/png"); // Fuerzo a no usar la caché del navegador header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); imagesavealpha($im,true); imagepng($im); // Libero los recursos de la imagen imagedestroy($im); ?>