按照老師打的,可是沒有驗證碼的框,而且出現亂碼
<?php
require_once'string.func.php';
//通過GD庫做驗證碼
//創建畫布
$width=80;
$height=28;
$image=imagecreatetruecolor($width,$height);
$white=imagecolorallocate($image,255,255,255);
$black=imagecolorallocate($image,0,0,0);
//用填充矩形填充畫布
imagefilledrectangle($image,1,1,$width-x,$height-2,$white);
$type=1;
$length=4;
//賦值產生的驗證碼
$chars=buildRandomString($type,$length);
$_SESSION[$sess_name]=$chars;
$fontfiles= array ("simfang.ttf","simhei.ttf","simkai.ttf");
for ($i=0;$i<$length;$i++){
? ?//設置隨機大小
? ?$size=mt_rand(14,18);
? ?//隨機角度
? ?$angle=mt_rand(-15,15);
? ?$x=5+$i*$size;
? ?//隨機的Y坐標
? ?$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);
? ?imagettftext($image,$size,$angle,$x,$y,$color,$fontfile,$text);
}
header("content-type:image/gif");
imagegif($image);
imagedestroy($image);
?>
string.func.php
<?php
//產生隨機的字符串
function buildRandomString($type=1,$length=4){
//$type=1;
//$length=4;
if($type==1){
? ?$char=join("",range(0,9));
}
else if($type==2){
? ?$char=join("",array_merge(range("a","z"),range("A","Z")));
}
elseif($type==3){
? ? $char=join("",array_merge(range("a","z"),range("A","Z"),range(0,9)));
}
if($length>strlen($chars)){
? ?exit("字符串長度不夠");
}
$chars=str_shuffle($chars);//隨機打亂字符串
return substr($chars,0,$length);//截取
}
?>
2018-06-05
2018-03-25
$fontfiles = array()的字體換下,注意在后面清下緩存 ob_clean(); 亂碼看下編輯器字符是不是utf8