php验证码乱码

这是dfl.php的内容:

<?php

function random($len=4){
$srcstr="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
mt_srand();
$str="";
for($i=0;$i<$len;$i++){
$str.=$srcstr[mt_rand(0,61)];
}
return $str;
}

$str=random(8);

function genimg($str,$width=150,$height=30){
$im=imagecreate($width,$height);

//背景色
$back=imagecolorallocate($im,0xFF,0xFF,0xFF);
//模糊点颜色
$pix=imagecolorallocate($im,0x68,0x80,0x38);
//字体色
$font=imagecolorallocate($im,0x68,0x80,0x38);

//绘模糊作用的点
mt_srand();
for($i=0;$i<1000;$i++){
imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$pix);
}

//写字,选择ComicSansMS字体
imagettftext($im,20,0,3,25,$font,"comic.ttf",$str);
//imagestring($im,5,3,3,$str,$font);

imagerectangle($im,0,0,$width-1,$height-1,$font);

header("Content-Type:image/png");
imagepng($im);

imagedestroy($im);
}

genimg($str,150,30);

?>

在index.php中用<img src="dfl.php" />
发现图片显示不出来,然后就单独运行dfl.php,发现全是乱码,实在找不到原因,请大家帮忙看看
我试过了,在widow\fonts下面找到了comic.ttf文件,并且把它放到了和dfl.php同一个文件夹下面了,还是乱码啊!不知道哪里搞错了

呵呵,这个程序没有问题

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) );
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-11-28
你这个dfl.php有错误输出吧,所以才看到的是乱码,肯定也不是全乱码,有一些还是看得清的,比如错误信息那些,估计是你这里有未定义的变量的原因,imagettftext($im,20,0,3,25,$font,"comic.ttf",$str); $font就没定义吧,应该是这样吧, imagettftext($im,20,0,3,25,$font="comic.ttf",$str);

再不行,可以留言或HI我,发个验证码类给你吧,[email protected]
第2个回答  2009-11-25
comic.ttf,没有找到这个字体,在网上搜一下这个字体,放到同级目录就好了
第3个回答  推荐于2016-07-19
<?php
session_start();
function random($len) {
$srcstr = "1a2s3d4f5g6hj8k9qwertyupzxcvbnm";
mt_srand();
$strs = "";
for ($i = 0; $i < $len; $i++) {
$strs .= $srcstr[mt_rand(0, 30)];
}
return $strs;
}

//随机生成的字符串
$str = random(4);

//验证码图片的宽度
$width = 50;

//验证码图片的高度
$height = 25;

//声明需要创建的图层的图片格式
@ header("Content-Type:image/png");

//创建一个图层
$im = imagecreate($width, $height);

//背景色
$back = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);

//模糊点颜色
$pix = imagecolorallocate($im, 187, 230, 247);

//字体色
$font = imagecolorallocate($im, 41, 163, 238);

//绘模糊作用的点
mt_srand();
for ($i = 0; $i < 1000; $i++) {
imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pix);
}

//输出字符
imagestring($im, 5, 7, 5, $str, $font);

//输出矩形
imagerectangle($im, 0, 0, $width -1, $height -1, $font);

//输出图片
imagepng($im);

imagedestroy($im);

$str = md5($str);

//选择 cookie
//SetCookie("verification", $str, time() + 7200, "/");

//选择 Session
$_SESSION["verification"] = $str;

验证码文件

文件调用

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>首页</title>
<link rel="stylesheet" type="text/css" href="css/main.css" />
<script type="text/javascript" src="jquery-1.9.1.min.js"></script>
<script type="text/javascript">
function changing(){
document.getElementById('checkpic').src="checkcode.php?"+Math.random();
}
</script>
</head>

<body>
<img id="checkpic" onclick="changing();" src='checkcode.php' />
</body>
</html>
第4个回答  2009-11-26
header('Content-type: image/jpeg');
做个声明看看