<?php
session_start();
header("Content-type: image/png");
$width = 150;
$height = 35;
$length = 6;
$fonts = array("font1","font2","font3","font4","font5");
$font = "../../fonts/%s.ttf";
$im = imagecreatetruecolor($width,$height);
$bg = imagecolorallocate($im, rand(120, 255), rand(120, 255), rand(120, 255) );
imagefilledrectangle($im,0,0,$width,$height,$bg);
for( $i = 0; $i < 3; $i++){
$c = imagecolorallocatealpha($im, rand(150, 255), rand(150, 255), rand(150, 255), rand(50, 100) );
$p = array( rand(0, $width), rand(0, $height), rand(0, $width), rand(0, $height),
rand(0, $width), rand(0, $height), rand(0, $width), rand(0, $height),
rand(0, $width), rand(0, $height), rand(0, $width), rand(0, $height),
rand(0, $width), rand(0, $height), rand(0, $width), rand(0, $height)
);
imagefilledpolygon($im, $p, count($p)/2, $c);
}
$captcha = "";
for( $i = 0; $i < $length; $i++ ){
$c = imagecolorallocate($im, rand(0, 150), rand(0, 150), rand(0, 150));
$x = $width/($length+1) * $i + rand(10,15);
$y = rand( 15, $height - $height/4 );
$a = rand(315,405);
$f = sprintf($font, $fonts[rand(0,count($fonts)-1)]);
$char = chr( rand(65, 90) );
$captcha .= $char;
imagettftext($im, rand(12,16), $a, $x, $y, $c, $f, $char);
}
$_SESSION["captcha"] = $captcha;
imagepng($im);
imagedestroy($im);
?>