生成验证码文件为testyzm.php
<?php
session_start();
header("Content-type: image/jpeg");
$width=120;
$height=30;
$im = imagecreatetruecolor($width,$height);
$bgcolor = imagecolorallocate($im,0,128,255);
imagefill($im,0,0,$bgcolor);
$str = "2345689abcdefghkmnopqrstuvwxyzABCDEFGHKMNOPQRSTUVWXYZ";
$code = '';
for ($i = 0; $i<4; $i++) {
$code .= $str[rand(0,strlen($str)-1)];
}
for($i=0;$i<4;$i++){
$angle_inclination=rand(-30,30);
$x_size = 26*$i+10;
$font_size = mt_rand(20,25);
$font=array('fonts/b.otf','fonts/e.otf','fonts/f.otf','fonts/g.otf','fonts/h.ttf','fonts/i.otf','fonts/j.ttf','fonts/k.otf','fonts/l.ttf','fonts/m.otf','fonts/n.ttf','fonts/o.otf','fonts/q.otf','fonts/r.otf',);
$font_color = imagecolorallocate($im, 255, 255, 255);
imagettftext($im,$font_size ,$angle_inclination,$x_size,23, $font_color, $font[rand(0,count($font)-1)], $code[$i]);
}
$_SESSION['checkcode'] = $code;
imagejpeg($im);
?>
改变大小可直接设定$width和$height的大小即可,
$font=array('fonts/b.otf',...)中的fonts/b.otf是位于fonts文件夹命名为b的字体,该字体已上传与百度网盘,用的时候随时下载。
在登陆或注册时调用的代码为
<img width="118px" height="25px" src="testyzm.php" name=yzm onClick="this.src='testyzm.php?id='+(new Date().getTime());" alt="点击刷新" />