PHP之生成验证码

来源:互联网 发布:php java 编辑:程序博客网 时间:2024/06/09 18:46

今天写一个生成验证码的程序,就是用了刚学的GD2图形库,下面我就为大家分享我的步骤:

首先我们要有一个明确的计划:

1.GD库的知识创建一个基于真彩的画布imagecreatetruecolor(int x_size,int y_size);分配一个颜色imagecolorallocate(resource image,int red,int green,int blue);//红绿蓝区域填充imagefill(resource image,int x,int y,int color);矩形框imagerectangle(resource image,int x1,int y1,int x2,int y2,int color);画一个像素点imagesetpixel(resource image,int x,int y,int color);画一条线段imageline(resource image,int x1,int y1,int x2,int y2,int color);绘制文本内容imagettftext(resource image,float size,float angle,int x,int y,int color,string content);以png格式将图像输出到浏览器或文件imagepng(resource image[,string filename]);销毁一个图像imagedestroy(resource image);int rand([int min,int max]);//产生一个随机数strlen();//获取字符串的长度header();//设置相应头信息2.实现步骤1.创建一个画布2.开始绘画3.输出图像4.销毁图片(释放内存)3.具体实现步骤整个验证码的实现步骤1.先复制一个字体文件2.定义一个函数(用于随机获取验证码内容)3.开始绘画,绘画验证码

我们的字体:C:/window/fonts,然后选择你喜欢的字体

直接上干货,代码注释的比较详细:php

<?php//header("content-type:text/html;charset=utf-8");header("content-type:image/png");//绘制验证码$number=4;//验证码的长度$str=getCode($number,0);//获取验证码$height=30;//验证码的高度$width=$number*20;//1创建画布$im=imagecreate($width,$height);//图像元//定义几个颜色,输出不同颜色的验证码$color[]=imagecolorallocate($im,111,0,55);$color[]=imagecolorallocate($im,0,77,0);$color[]=imagecolorallocate($im,0,0,160);$color[]=imagecolorallocate($im,221,111,0);$color[]=imagecolorallocate($im,220,0,0);//背景颜色(这就是颜色)$bg=imagecolorallocate($im,200,200,200);//2开始绘画imagefill($im,0,0,$bg);//添加一个边框imagerectangle($im,0,0,$width-1,$height-1,$color[rand(0,4)]);//随机添加一些干扰点for($j=0;$j<200;$j++){$c=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));imagesetpixel($im,rand(0,$width),rand(0,$height),$c);}//随机添加干扰线for($i=0;$i<5;$i++){$c=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));imageline($im,rand(0,$width),rand(0,$height),rand(0,$width),rand(0,$height),$c);}//绘制验证码for($i=0;$i<$number;$i++){imagettftext($im,10,20,8+(18*$i),24,$color[rand(0,4)],"msyh.ttf",$str[$i]);}//3输出图像//设置响应头信息,图像相应imagepng($im);//4销毁图像资源imagedestroy($im);/**定义一个随机生成验证码内容的函数@ param $m=4 验证码的个数@ param $type=0 验证码的类型(纯数字) 1(数字加小写字母) 2(数字加大小写字母)**/function getCode($m=4,$type=0){//数字+小写字母+大写字母$str="0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";$t=array(9,35,strlen($str)-1);//随机生成验证码内容$c="";for($i=0;$i<$m;$i++){$c.=$str[rand(0,$t[$type])];}return $c;//echo strlen($str);}//echo getCode(6,1);?>
HTML:

<?php//验证码的使用header("content-type:text/html;charset=utf-8");?><!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>欢迎来到登录界面</title></head><body><h2 align="center">验证码的使用</h2><br/><br/><br/><br/><br/><br/><br/><br/><form action="" method="post"><table align="center" ><caption>用户登录</caption><tr><td><em>用户名:</em></td><td><input type="text" name="username"></td><td></td></tr><tr><td><em>密码:</em></td><td><input type="password" name="password"></td><td></td></tr><tr><td><em>验证码:</em></td><td><input type="text" name="yzm"></td><td><img src="demo.php" onclick="this.src='demo.php?id='+Math.random()"/></td></tr><tr><td colspan="3"><input type="submit" name="sub" value="登录"></td></tr></table></form></body></html>
结果如下:

在这里,不同的浏览器可能会得到不同的效果,注意观察


原创粉丝点击