session_start();
/*Set these avriables as you see fit */
$fontsize = 19;
$numcharacters = 4;
$bgcolor = "3A1906";
$textcolor = "FFD59F";
$num_interference_lines = 2;
/*At least one of these MUST be true */
$str_includes_numbers = true;
$str_includes_uppercase = false;
$str_includes_lowercase = false;
/*Generate Captcha String */
$_SESSION['teassocaptcha'] = RandString($numcharacters, $str_includes_numbers, $str_includes_uppercase, $str_includes_lowercase);
/*Send a generated image to the browser */
create_image($numcharacters, $fontsize, $bgcolor, $textcolor, $num_interference_lines);
exit();
function create_image($length, $fontsize, $bgcolor, $textcolor, $numlines)
{
/*Set the image width and height */
$trifont = 1.2 * $fontsize;
$width = ($trifont * $length);
$height = 2.5 * $fontsize;
/*Create the image resource */
$image = imagecreate($width, $height);
/*We are making colors */
$forecolor = imagecolorallocate($image, hexdec(substr($textcolor,0,2)), hexdec(substr($textcolor,2,2)), hexdec(substr($textcolor,4,2)));
$backcolor = imagecolorallocate($image, hexdec(substr($bgcolor,0,2)), hexdec(substr($bgcolor,2,2)), hexdec(substr($bgcolor,4,2)));
/*Make the background backcolor */
imagefill($image, 0, 0, $backcolor);
/*Add randomly generated string in forecolor to the image */
/*imagestring($image, 7, 30, 3, $pass, $forecolor); */
/*Tell the browser what kind of file is come in */
header("Content-Type: image/jpeg");
/*imageloadfont("chick.ttf"); */
for($i = 0; $i < $length; $i++) {
$source = imagecreate($trifont, $trifont);
/*We are making colors */
$forecolors = imagecolorallocate($source, hexdec(substr($textcolor,0,2)), hexdec(substr($textcolor,2,2)), hexdec(substr($textcolor,4,2)));
$backcolors = imagecolorallocate($source, hexdec(substr($bgcolor,0,2)), hexdec(substr($bgcolor,2,2)), hexdec(substr($bgcolor,4,2)));
imagefill($source, 0, 0, $backcolors);
/*imagestring($source, 5, 3, 0, substr($_SESSION['teassocaptcha'], $i, 1), $forecolors); */
imagettftext($source, $fontsize, 0, 0, 18, $forecolors, "BOOKOS.TTF", substr($_SESSION['teassocaptcha'], $i, 1));
/*Rotate Somehow */
switch(mt_rand(1,8)) {case 1:$source = imagerotate($source, -20, $backcolors);
break;case 2:
$source = imagerotate($source, 20, $backcolors); break; case 3:
$source = imagerotate($source, -15, $backcolors); break; case 4:
$source = imagerotate($source, 15, $backcolors); break; case 5:
$source = imagerotate($source, -10, $backcolors); break; case 6:
$source = imagerotate($source, 10, $backcolors); break; case 7:
$source = imagerotate($source, -2, $backcolors); break; case 8:
$source = imagerotate($source, 2, $backcolors); break;
}
/*Set Location variables */
$loc_x = 5+($trifont*$i);
$loc_y = $trifont/2 + mt_rand((-1 * $fontsize/10),($fontsize/10));
/*Set Size Variables */
$size_x = $trifont * (mt_rand(100, 110) / 100);
$size_y = $trifont * (mt_rand(100, 110) / 100);
imagecopymerge($image,$source,$loc_x,$loc_y,0,0,$size_x,$size_y,90);
imagedestroy($source);
}
/*Throw in some lines to make it a little bit harder for any bots to break */
imagesetstyle($image, array($forecolor, $forecolor, $forecolor, $forecolor, $forecolor, $forecolor, $forecolor, $forecolor, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT));
imagerectangle($image,0,0,$width-1,$height-1,$forecolor);
/*for($i = 1; $i < mt_rand(1,5); $i++) { */
for($i = 1; $i < (1 + $numlines); $i++) {
$from_x = mt_rand(0, $width);
$from_y = mt_rand(0, $height);
$to_x = mt_rand(0, $width);
$to_y = mt_rand(0, $height);
imageline($image, $from_x, $from_y, $to_x, $to_y, IMG_COLOR_STYLED);
}
/*Output the newly created image in jpeg format */
imagejpeg($image);
/*Free up resources */
imagedestroy($image);
}
function RandString($len, $num, $uc, $lc) {
if (!$len || $len<1 || $len>100) {
print "Error: \"Length\" out of range (1-100)
\n";
return;
}
if (!$num && !$uc && !$lc) {
print "Error: No character types specified
\n";
return;
}
$s=""; $i=0; do {
switch(mt_rand(1,3)) {
/* get number - ASCII characters (0:48 through 9:57) */
case 1: if ($num==1) {
$s .= chr(mt_rand(48,57)); $i++;
}
break;
/* get uppercase letter - ASCII characters (a:65 through z:90) */
case 2: if ($uc==1) {
$s .= chr(mt_rand(65,90)); $i++;
}
break;
/* get lowercase letter - ASCII characters (A:97 through Z:122) */
case 3: if ($lc==1) {
$s .= chr(mt_rand(97,122)); $i++;
}
break;
}
} while ($i<$len); return $s;
}?>