<?php
/* Make sure NOT to have ANY whitespaces before the <?php tag! */

function random_str($len)
{
    $ch = "A2B3C4D5E6F7G8H9JKMNZXY";
    $l = strlen($ch) - 1;
    $str = "";
    for ($i=0; $i < $len; $i++) {
        $x = rand(0, $l);
        $str .= $ch[$x];
    }
    return $str;
}

 

$t = random_str(4);
$w = 150;
$h = 34;
 
 
$gb = imagecreate($w, $h);
$bgc = imagecolorallocate($gb, 255, 255, 255);
$grc = imagecolorallocate($gb, 200, 200, 200);
putenv('GDFONTPATH=' . realpath('./fonts'));
$fonts = array();

$fonts[] = "arialbd";
$fonts[] = "VeraBd";
$fonts[] = "pala";



for ( $x = 0; $x <= strlen( $t ); $x++ )
{
  $angle = mt_rand( -20, 25 );
  $fgc = imagecolorallocate($gb, mt_rand( 50, 200 ), mt_rand( 0, 200 ), mt_rand( 100, 150 ));

  $padding_left = mt_rand( 20, 25 );
  imagettftext($gb, mt_rand( 12, 15 ), $angle, $padding_left + $x * mt_rand( 25,30 ) , 16 + mt_rand( 0, 9 ), $fgc, $fonts[mt_rand( 0, 2 )], $t[$x]);
}

/*
$dot_color = imagecolorallocate( $gb, mt_rand( 130, 170 ), mt_rand( 130, 170 ), mt_rand( 0, 50) );


for ( $i = 0; $i < ($w*$h)/mt_rand( 50, 50 ); $i++ )
{
  imagefilledellipse( $gb, mt_rand( 0, $w ), mt_rand( 0, $h ), 2, 2, $dot_color );
}
*/
setcookie( "captcha_code", md5( $t ) , NULL, "/" );

/* Make sure the image will not be cached */ 
header("Expires: Mon, 23 Jul 1993 05:00:00 GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);

// HTTP/1.0
header("Pragma: no-cache");

header('Content-Type: image/png');
imagePNG($gb);
imagedestroy($gb);


/* Make sure NOT to have ANY whitespaces after the ?> tag! */
?>