session_start();
$type=1;
$length=4;
$pixel=0;
$line=0;
$sess_name = "verify";
$width = 80;
$height = 80;
//创建画布
$image = imagecreatetruecolor ( $width, $height );
// 为一幅图像分配颜色 背景设为白色
//返回一个颜色值16777215
$white = imagecolorallocate ( $image, 255, 255, 255 );
//print_r($white1);die;
//把背景再设置成黑色
$black = imagecolorallocate ( $image, 0, 0, 0 );
//print_r($black);die;
//本函数将图片的封闭长方形区域着色 设置这个图片的这个矩形的颜色为白色
imagefilledrectangle ( $image, 1, 1, $width - 2, $height - 2, $white );
//返回4个数字
$chars = buildRandomString ( $type, $length );
//print_r($chars);die;
//把四个数字存到 session中
$_SESSION [$sess_name] = $chars;
//$fontfiles = array ("MSYH.TTF", "MSYHBD.TTF", "SIMLI.TTF", "SIMSUN.TTC", "SIMYOU.TTF", "STZHONGS.TTF" );
$fontfiles = array ("SIMYOU.TTF" );
for($i = 0; $i < $length; $i ++) {
//字号随机取一个 在14到18的范围内
$size = mt_rand ( 14, 18 );
//角度随机取一个在-15到15之间
$angle = mt_rand ( - 15, 15 );
$x = 5 + $i * $size;
// print_r($x);
$y = mt_rand ( 20, 26 );
$fontfile = "../fonts/" . $fontfiles [mt_rand ( 0, count ( $fontfiles ) - 1 )];
$color = imagecolorallocate ( $image, mt_rand ( 50, 90 ), mt_rand ( 80, 200 ), mt_rand ( 90, 180 ) );
//分别取出四个数字中的一个
$text = substr ( $chars, $i, 1 );
//本函数将 TTF (TrueType Fonts) 字型文字写入图片。
imagettftext ( $image, $size, $angle, $x, $y, $color, $fontfile, $text );
}
header ( "content-type:image/gif" );
imagegif ( $image );
imagedestroy ( $image );
网友评论