手机版

PHP动态随机生成验证码类代码

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

下面是效果图,这个效果图是没有开启干扰码的效果图

下面是类代码复制代码代码如下:PHP/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *文件:图像代码//DONE:生成动态验证码类//DATE ' 2010-3-31//作者: www .5dkx。5D网站开心博客* * * * * * * * * * * * * * * *类ImageCode { private $ width//验证码图片宽度二等兵$身高;//验证码图片高度private $ codeNum//验证码字符个数private $ checkCode//验证码字符私人$图像;//验证码画布/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *功能:构造函数//Done:成员属性初始化//5D开心博客* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * function _ _ construct($ width=60,$height=20,$ codeNum=4){ $ this-width=$ width; $ this-height=$ height;$ this-codeNum=$ codeNum;$ this-CheckCode=$ this-CreateCheckCode();}函数show image(){ $ this-getcreateImage();$ this-OutputText();$ this-setdisturbColor();$ this-outputImage();}函数GetCheckCode(){ return $ this-check code;}私有函数getCreateImage(){ $ this-image=imagecreatetrue color($ this-width,$ this-height);$ back=image color allocate($ this-image,255,255,255);$ border=image color allocate($ this-image,255,255,255);imagefiledrectangle($ this-image,0,0,$this-width-1,$this-height-1,$ border);//使用纯白色填充矩形框,这里用的话后面干扰码失效/*如果想用干扰码的话使用下面的*///imagerectangle($this-image,0,0,$this-width-1,$this-height-1,$ border);}私有函数createCheckCode(){ for($ I=0;$ I $ this-CodeNum;$i ) { $number=rand(0,2);switch($ number){ case 0: $ rand _ number=rand(48,57);打破;//数字案例1:美元rand_number=兰特(65,90);打破;//大写字母案例2:美元rand_number=rand(97,122);打破;//小写字母} $asc=sprintf('%c ',$ rand _ number);$asc_number=$asc_number .$ asc}返回$ asc _ number}私有函数setDisturbColor()//干扰吗设置{ for($ I=0;$ I=100 $ I){/$ color=image color allocate($ this-image,rand(0,255),rand(0,255),rand(0,255));$ color=image color allocate($ this-image,255,255,255);imagesetpixel($this-image,rand(1,$this-width-2),rand(1,$this-height-2),$ color);}//$ color=image color allocate($ this-image,0,0,0);//imagesetpixel($this-image,rand(1,$this-width-2),rand(1,$this-height-2),$ color);}私有函数outputText() { //随机颜色、随机摆放、随机字符串向图像输出对于($ I=0;$ I=$ this-CodeNum;$ I){ $ BG _ color=image color allocate($ this-image,rand(0,255),rand(0,128),rand(0,255));$ x=地板($ this-width/$ this-codenum)* $ I ^ 3;$y=rand(0,$ this-height-15);imagechar($this-image,5,$x,$y,$this-checkCode[$i],$ BG _ color);} }私有函数outputImage(){ if(imagetype())IMG _ GIF){ header(' Content _ type : image/GIF ');imagegif($ this-image);} else if(imagetype())IMG _ JPG){ header(' Content-type : image/JPEG ');imagejpeg($this-image ' ',0.5);} else if(imagetype())IMG _ PNG){ header(' Content-type : image/PNG ');图像JPEG($ this-image);} else if(imagetype())IMG _ WBMP){ header(' Content-type : image/vnd。WAP。wbmp’);图像JPEG($ this-image);} else { die('PHP不支持图像创建');} } function _ _ destroy(){ image destroy($ this-image);} } /*显示*//* * * * * * * * * * * * * * * * * * * * * * * * * session _ start();$image=新的ImageCode(60,20,4);$ image-显示图像();$ _ SESSION[' ImageCode ']=$ image-GetCheckCode();*******************************************************************/?

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