呵呵,这个程序没有问题
imagettftext($im,20,0,3,25,$font,"comic.ttf",$str);
这里需要一个comic.ttf字体文件,必须放在那个php程序同目录下
你是不是没有这个字体文件
如果没有,可以到系统的C:\Windows\Fonts目录下拷贝一个(拷个几百KB大小的就足够用了)
我试了你的程序,生成的图片效果不是很好,我以前写了一个,给你参考下把,虽然也是修改的网上的:
<?
//获取验证码图片模块
session_start();
//记得去掉下面注释
if (!isset($_GET["sid"])||$_GET["sid"]==""||$_GET["sid"]!=session_id())die();
Header("Content-type:image/png");
//定义header,声明图片文件,最好是png,无版权之扰;
//生成新的四位整数验证码
//session_id($_GET["sid"]);
//开启session;
$authnum_session = '';
$str = 'ABCDEFGHIJKMNPQRSTUVWXYZ1234567890';
//定义用来显示在图片上的数字和字母;
$l = strlen($str);
//得到字串的长度;
//循环随机抽取四位前面定义的字母和数字;
for ($i=1; $i<=4; $i++)
{
$num=rand(0,$l-1);
//每次随机抽取一位数字;从第一个字到该字串最大长度,
//减1是因为截取字符是从0开始起算;这样34字符任意都有可能排在其中;
$authnum_session.= $str[$num];
//将通过数字得来的字符连起来一共是四位;
}
$_SESSION["checkCode"]=$authnum_session;
//用session来做验证也不错;注册session,名称为authnum_session,
//其它页面只要包含了该图片
//即可以通过$_SESSION["checkCode"]来调用
//生成验证码图片,
//srand((double)microtime()*1000000);
mt_srand();
$im = imagecreate(100,40);//图片宽与高;
//主要用到黑白灰三种色;
$black = ImageColorAllocate($im, mt_rand(0,245),mt_rand(0,200),mt_rand(0,200));
$white = ImageColorAllocate($im, 255,255,255);
$gray = ImageColorAllocate($im, 200,200,200);
//将四位整数验证码绘入图片
imagefill($im,168,30,$gray);
//如不用干扰线,注释就行了;
$li = ImageColorAllocate($im, 220,220,220);
for ($i=0; $i<0; $i++)
{//加入3条干扰线;也可以不要;视情况而定,因为可能影响用户输入;
imageline($im,mt_rand(0,30),mt_rand(0,41),mt_rand(80,90),mt_rand(0,41),$li);
} //字符在图片的位置;
//imagestring($im, 5, 8, 2, $authnum_session, $white);
// Set the enviroment variable for GD
putenv('GDFONTPATH=' . realpath('.'));
// Name the font to be used (note the lack of the .ttf extension)
//$font = 'mvboli';
//$font = imageloadfont("mvboli.ttf");
//imagestring($im,$font, 8, 2, $authnum_session, $white);
//imagettftext ( $im,20.0, 0, 0, 30, $white,"FELIXTI.TTF", $authnum_session );
for ($i=0; $i<4; $i++)
{
imagettftext ( $im,20.0, mt_rand(0,40)-20, $i*25+3, 30, $white,"FELIXTI.TTF", substr($authnum_session,$i,1) );
}
for ($i=0; $i<90; $i++) //加入干扰象素
{
imagesetpixel($im, mt_rand()%100-2 , mt_rand()%40-2 , $gray);
}
ImagePNG($im);
ImageDestroy($im);
?>
同样这行代码需要一个字体文件,你可以把它改成你拷过来的的字体文件名
imagettftext ( $im,20.0, mt_rand(0,40)-20, $i*25+3, 30, $white,"FELIXTI.TTF", substr($authnum_session,$i,1) );
温馨提示:答案为网友推荐,仅供参考