<?
session_start();
$C_IMAGE_TYPE = 'PNG';
$C_WIDTH = rand(138,160); //150;
$C_HEIGHT = rand(74,85); //80;
$C_NUM_GENSIGN = 4;
$path_fonts = './fonts/';
$alpha_bg = 100;
$fontsize = 6;//4.7
$count_letters_bg = 900;//900
$koleb_fontsize_bg = 50;//10
$LETTERS = array('1','2','3','4','5','6','7','8','9','0');
$C_FONT_SIZE = intval($C_HEIGHT/(($C_HEIGHT/$C_WIDTH)*$fontsize));
$C_NUM_SIGN = intval(($C_WIDTH*$C_HEIGHT)/$count_letters_bg);
$CODE = array();
$FIGURES = array('1','900');
$src = imagecreatetruecolor($C_WIDTH,$C_HEIGHT);
$fon = imagecolorallocate($src,255,255,255);
imagefill($src,0,0,$fon);
$FONTS = array();
$dir=opendir($path_fonts);
while($fontName = readdir($dir)){
   if($fontName != "." && $fontName != ".."){
     if(strtolower(strrchr($fontName,'.'))=='.ttf') $FONTS[] = $path_fonts.$fontName;
   }
}
closedir($dir);
for($i=0;$i<$C_NUM_SIGN;$i++){
 $h = 1;
 $color = imagecolorallocatealpha($src,120,120,120,$alpha_bg);// ($src,rand(0,255),rand(0,255),rand(0,255),$alpha_bg)
 $font = $FONTS[rand(0,sizeof($FONTS)-1)];
 $letter = $LETTERS[rand(0,sizeof($LETTERS)-1)];
 $size = rand($C_FONT_SIZE-$koleb_fontsize_bg,$C_FONT_SIZE+$koleb_fontsize_bg);
 $angle = rand(0,60);
 if($h == rand(1,2)) $angle = rand(360,300);
 imagettftext($src,$size,$angle,rand($C_WIDTH*0.1,$C_WIDTH-$C_WIDTH*0.1),rand($C_HEIGHT*0.2,$C_HEIGHT),$color,$font,$letter);
}
for($i=0;$i<$C_NUM_GENSIGN;$i++){
 $h = 1;
 $color = imagecolorallocatealpha($src,80,80,80,rand(10,40)); //($src,$FIGURES[rand(0,sizeof($FIGURES)-1)],$FIGURES[rand(0,sizeof($FIGURES)-1)],$FIGURES[rand(0,sizeof($FIGURES)-1)],rand(10,30))
 $font = $FONTS[rand(0,sizeof($FONTS)-1)];
 $letter = $LETTERS[rand(0,sizeof($LETTERS)-1)];
 $size = rand($C_FONT_SIZE*2.1-1,$C_FONT_SIZE*2.1+1);
 $x = (empty($x)) ? $C_WIDTH*0.08 : $x + ($C_WIDTH*0.8)/$C_NUM_GENSIGN+rand(0,$C_WIDTH*0.01);
 $y = ($h == rand(1,2)) ? (($C_HEIGHT*1.15*3)/4) + rand(0,$C_HEIGHT*0.02) : (($C_HEIGHT*1.15*3)/4) - rand(0,$C_HEIGHT*0.02);
 $angle = rand(5,20);
 $CODE[] = $letter;
 if($h == rand(0,1)) $letter = strtoupper($letter);
 if($h == rand(1,2)) $angle = rand(355,340);
 imagettftext($src,$size,$angle,$x,$y,$color,$font,$letter);
}
$_SESSION['securityCode'] = implode('',$CODE);
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-cache, must-revalidate"); 
header("Pragma: no-cache");
if($C_IMAGE_TYPE=='PNG'){header ("Content-type: image/png"); imagepng($src);}
elseif($C_IMAGE_TYPE=='JPEG'){header ("Content-type: image/jpeg"); imagejpeg($src);}
else{header ("Content-type: image/gif"); imagegif($src);}
imagedestroy($src);
?>