代码如下:
<?php
$im=imagecreate(45,18);//创建一个45*18的图像
$gray=imagecolorallocate($im,200,200,200);//创建用在image所代表的图像中颜色
for($i=0;$i<4;$i++){
$size=mt_rand(3,6);//字体大小
$x=(5+$i*10);//x轴坐标
$y=mt_rand(1,3);//y轴坐标
$mun=mt_rand(0,9);//随机数
imagestring($im,$size,$x,$y,$mun,imagecolorallocate($im,rand(0,130),rand(0,130),rand(0,130)));//将颜色为随机的字符写入图像中
}
for($j=0;$j<200;$j++){
$randcolor=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($im,rand()%70,rand()%30,$randcolor);//在image图像中用color颜色在x,y(图像左上角为0,0)坐标上画一个点
}
imagepng($im);//显示图片
imagedestroy($im); //销毁因image所产生的内存
?>
效果图:

本文介绍了一种使用PHP生成简单图形验证码的方法。通过创建指定尺寸的图像并填充随机点及数字,实现基本的验证码功能。具体步骤包括设置图像大小、定义随机颜色、绘制随机数字和背景杂点。
3400

被折叠的 条评论
为什么被折叠?



