首页 | 新闻 | 交流 | 问答吧 | 文档 | 手册 | 下载 | 博客

 
收藏此问题 发表新评论
PHP验证码
<?php
session_start();
$authnum=random(6);//验证码字符.
$_SESSION['yan']=$authnum;
//生成验证码图片
Header("Content-type: image/PNG");
$im = imagecreate(55,18); //imagecreate() 新建图像,大小为 x_size 和 y_size 的空白图像。
$red = ImageColorAllocate($im, 153,51,0); //设置背景颜色
$white = ImageColorAllocate($im, 255,204,0);//设置文字颜色
$gray = ImageColorAllocate($im, 102,102,0); //设置杂点颜色



/*
int imagecolorallocate ( resource image, int red, int green, int blue )
imagecolorallocate() 为图像分配颜色,代表了由给定的 RGB 成分组成的颜色。image 参数是 imagecreatetruecolor() 函数的返回值。red,green 和 blue 分别是所需要的颜色的红,绿,蓝成分。这些参数是 0 到 255 的整数或者十六进制的 0x00 到 0xFF。imagecolorallocate() 必须被调用以创建每一种用在 image 所代表的图像中的颜色。
第一次对 imagecolorallocate() 的调用会填充背景色。
*/

imagefill($im,55,18,$red);
/*imagefill() 在 image 图像的坐标 x,y(图像左上角为 0, 0)处用 color 颜色执行区域填充(即与 x, y 点颜色相同且相邻的点都会被填充)。
将四位整数验证码绘入图片
位置交错*/
for ($i = 0; $i < strlen($authnum); $i++)
{
//        $i%2 == 0?$top = -1top = 3;

        imagestring($im, 6, 13*$i+4, 1, substr($authnum,$i,1), $white);
        //int imagestring ( resource image, int font, int x, int y, string s, int col)
        //imagestring() 用 col 颜色将字符串 s 画到 image 所代表的图像的 x,y 座标处(图像的左上角为 0, 0)。如果 font 是 1,2,3,4 或 5,则使用内置字体。
}
for($i=0;$i<100;$i++)   //加入干扰象素
{
        imagesetpixel($im, rand()%55 , rand()%18 , $gray);
        //int imagesetpixel ( resource image, int x, int y, int color)
        //imagesetpixel() 在 image 图像中用 color 颜色在 x, y 坐标(图像左上角为 0, 0)上画一个点。
}
ImagePNG($im); //以 PNG 格式将图像输出到浏览器或文件
ImageDestroy($im);//销毁一图像

//产生随机数函数
function random($length) {
        $hash = '';
        $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
       
        $max = strlen($chars) - 1;

        for($i = 0; $i < $length; $i++) {
                $hash .= $chars[mt_rand(0, $max)];
        }
        return $hash;

}
?>

谁还可以更详细的解释下这段代码。。
昵称: xiazaib  时间: 2008-08-18 13:47:00
好象没法更详细了,你哪不明白,问哪吧
昵称: unspace  时间: 2008-08-25 17:07:00
人家是炫的,
昵称: guog  时间: 2008-08-25 17:29:00
if for 变量的声明
都写上吧,不然看不懂.
昵称: 游戏人间  时间: 2008-08-25 21:22:00
生成一个随机码并存入session,然后用这个随机码产生相应的图片,并加入了干扰象素
昵称: 飞豹游侠  时间: 2008-08-26 13:55:00
发表评论
昵称:
内容:
验证: 点击刷新验证码。 看不清?