您的位置:首页 > 编程语言 > PHP开发

学习php最简单的验证码制作

2016-03-18 14:30 555 查看
笔者是一个即将出去实习的计算机学生,关于php纯属自己的兴趣爱好,自学了一些简单的。今天把自己的做的验证码发来让大家指教一番。虽是简单,却花了不少心思,代码如下:<?php
//定义图片格式
header("Content-type:image/png");

//定义画布大小,即验证码区域
$img=imagecreatetruecolor(80, 30);

//定义画笔颜色
$red1=imagecolorallocate($img, 0xff, 0x00, 0x00);
$green1=imagecolorallocate($img, 0x00, 0xff, 0x00);
$blue1=imagecolorallocate($img, 0x00, 0x00, 0xff);

//定义画布背景色
$bgcolor=imagecolorallocate($img, 0xff, 0xff, 0xff);

//将定义的颜色存入数组,以便随机换颜色
$col = array('0' =>$red1,'1'=>$green1,'2'=>$blue1 );

//填充画布背景色
imagefill($img, 0, 0, $bgcolor);

//添加验证码内容
for($i=0;$i<4;$i++)
{
$content .='';
$content=$content.rand(0,9);
}
imagestring($img, 40, 20, 10, $content,$col[rand(0,2)] );

//添加干扰因素
//添加干扰点
for($i=0;$i<50;$i++)
{
imagesetpixel($img, rand(0,80), rand(0,40), $col[rand(0,2)]);
}
//添加干扰线
for($j=0;$j<4;$j++)
{
//imageline函数的格式:imageline(image, x1, y1, x2, y2, color);
imageline($img, rand(0,20), rand(0,20), rand(0,80), rand(0,30), $col[rand(0,2)]);
}

//输出图像

imagepng($img);

//释放图像资源
imagedestroy($img);
?>


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php 验证码 计算机