gd库画图流程:
1,创建画布
2,创建各种颜料
3,绘画
4,保存成图片
5,销毁
html页面
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>验证</title>
<script type="text/javascript">
function update(th){
th.src='001.php?_s='+Math.random();
}
</script>
</head>
<body>
<form action='' method=''>
验证码<input type='text' name='验证码'/><img src='001.php' onclick='update(this)'/><br/>
<input type='submit' value='提交'/>
</form>
</body>
</html>
001.php生成验证码
<?php
// print_r(gd_info());//判断gd2库是否开启
$rs=imagecreatetruecolor(50,30);//创建画布资源
$gray=imagecolorallocate($rs, 100, 100, 100);
$fontcolor=imagecolorallocate($rs, mt_rand(150,255), mt_rand(150,255), mt_rand(150,255));
$linecolor1=imagecolorallocate($rs, mt_rand(100,200), mt_rand(100,200), mt_rand(100,200));
$linecolor2=imagecolorallocate($rs, mt_rand(100,200), mt_rand(100,200), mt_rand(100,200));
$linecolor3=imagecolorallocate($rs, mt_rand(100,200), mt_rand(100,200), mt_rand(100,200));
//填充颜色
imagefill($rs, 0, 0, $gray);
//随机字符串
$str=substr(str_shuffle('ABCDEFGHJKMNOPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz23456789'),0,4);
//书写字符串
imagestring($rs, 5, 8, 5, $str, $fontcolor);
//画干扰线
imageline($rs,0,mt_rand(0,30),50,mt_rand(0,30),$linecolor1);
imageline($rs,0,mt_rand(0,30),50,mt_rand(0,30),$linecolor2);
imageline($rs,0,mt_rand(0,30),50,mt_rand(0,30),$linecolor3);
//输出
header('content-type:image/png');
imagepng($rs);
//销毁
imagedestroy($rs);
?>