<?php
ob_start();
include_once(dirname(__FILE__)."/class/session.php");
header('content-Type:image/jpeg');
mt_srand(make_seed());
$randval  = mt_rand();
$seccode = substr($randval,-4);
$length    =  strlen($seccode);
$width = $length * 14;
$height = 25;
$_SESSION['seccode']  =  $seccode;
$bgimage = dirname(__FILE__).'/seccode_bg.gif';
if(is_file($bgimage)){ 
  $img = imagecreatefromgif($bgimage);
}else{
  $img = imagecreate( $width , $height);
  $bgcolor = imagecolorallocate($img,40,40,40);
}


for  ($i = 0; $i < $length ;  $i++)  {
     $color = imagecolorallocate($img,abs(rand(128,256)),abs(rand(128,256)),abs(rand(128,256))); //随机取色
     imagechar($img,5,abs(mt_rand()%4)+$i*13,abs(mt_rand()%5),$seccode[$i],$color);//随机每个字符的x,y值   
} 
imagejpeg($img);
imageDestroy($img);
ob_end_flush();

/*
设置随机数种子，从php手册中抄来的。
*/
function make_seed()
{
    list($usec, $sec) = explode(' ', microtime());
    return (float) $sec + ((float) $usec * 100000);
}
?>