手机版

PHP制作图形验证码代码分享

时间:2021-09-09 来源:互联网 编辑:宝哥软件园 浏览:

效果:

myvcode.class.php:封装创建验证码的类

?PHP/* *文件: mvcode。上课。PHP *验证码类,类名Vcode */类Vcode { private $ width/*验证码宽度*/private $ height;/*验证码高度*/private $ CodeNum;/*验证码字符个数*/private $ CheckCode;/*验证码字符*/private $ image;/*验证码资源*/private $ PixNum;/*绘制干扰点的个数*/private $ LineNum;/*绘制干扰线的条数*//**构造方法实例化验证码对象,并初始化数据*@param int $width设置默认宽度* @ param int $高度设置默认高度*@param int $codeNum设置验证码中的字符个数*@param int $pixNum设置干扰点的个数*@param int $lineNum设置干扰线的数量*/function _ _ construct($ width=80,$height=40,$codeNum=4,$pixNum=40,$ lineNum=5){ $ this-width=$ width;$ this-height=$ height;$ this-codeNum=$ codeNum;$ this-PixNum=$ PixNum;$ this-LineNum=$ LineNum;}/*内部私有方法,创建图像资源*/私有函数getCreateImage(){ $ this-image=imagecreatetrue color($ this-width,$ this-height);$ white=image color allocate($ this-image,0xff,0xff,0x ff);imagefill($this-image,0,0,$ white);$ black=image color allocate($ this-image,0,0,0);imagerectangle($this-image,0,0,$this-width-1,$this-height-1,$ black);}/*内部私有方法,绘制字符,去掉o0Llz和012 */私人函数createCheckCode(){ $ code=' 3456789 abcdefghijkmnpqrstuvwxyabcdefghijkmnpqrstuvwxy ';$ this-CheckCode=' ';对于($ I=0;$ I $ this-CodeNum;$i ){$char=$code{rand(0,strlen($ code)-1)};$this-checkCode .=$ char $ font color=image color allocate($ this-image,rand(0,128),rand(0,128),rand(0,128));$fontSize=rand(3,5);$x=rand(0,$ this-width-imagefontwidth($ font size));$y=rand(0,$ this-height-image font height($ font size));imagechar($this-image,$fontSize,$x,$y,$char,$ font color);}}/*内部私有方法设置干扰元素*/私有函数setDisturbColor(){/*绘制干扰点*/for($ I=0;$ I $这个-PixNum;$ I){ $ color=imagecolor allocate($ this-image,rand(0,255),rand(0,255),rand(0,255));imagesetpixel($this-image,rand(1,$this-width-2),rand(1,$this-height-2),$ color);}/*绘制干扰线*/for($ I=0;$ I $ this-LineNum;$ I){ $ color=imagecolor allocate($ this-image,rand(0,255),rand(0,255),rand(0,255));imageline($this-image,rand(1,$this-width/2),rand(1,$this-height/2),rand(this-width/2,$ this-width2),rand(this-height/2,$ this-height2),$ color);}}/*开启会议保存利用回声输出图像*/function _ _ toString(){ $ _ SESSION[' code ']=strtoupper($ this-CheckCode);$ this-getCreateImage();$ this-CreateCheckCode();$ this-setdisturbColor();$ this-OutPutimg();}/*内部私有方法输出图像*/private function OutPutimg(){ header(' content-type : image/png ');image png($ this-image);}/*析构方法,释放对象*/function _ _ destroy(){ image destroy($ this-image);}}?

imgcode.php输出图像

?PHP session _ start();require _ once(' myv代码。上课。PHP’);echo new Vcode();test.html:同过img标签引用

img src='http:imgcode.php '可以加一个a标签,用射流研究…实现换一张效果:

/*局部刷新换验证码*/函数更改代码(){ var img code=document。getelementbyid(' code ');var变更=文档。getelementbyid(' change ');change.onclick=function(){/*必须加后面的参数才能刷新*/imgcode.src='code.php?TM的数学。random();}}

密码和变化分别是img和a的编号

版权声明:PHP制作图形验证码代码分享是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。