ThinkPHP 3.2.3实现加减乘除图片验证码_第1页
ThinkPHP 3.2.3实现加减乘除图片验证码_第2页
ThinkPHP 3.2.3实现加减乘除图片验证码_第3页
ThinkPHP 3.2.3实现加减乘除图片验证码_第4页
ThinkPHP 3.2.3实现加减乘除图片验证码_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第ThinkPHP3.2.3实现加减乘除图片验证码ThinkPHP3.2.3自带的验证码类位于/ThinkPHP/Library/Think/Verify.class.php,字体文件位于/ThinkPHP/Library/Think/Verify/

可以在Verify.class.php文件内进行修改,也可以单独写一个类继承自带的验证码类。如果单独写一个继承的类,可以重用父类的属性和方法,但是要注意的是父类中有一些属性和方法是私有(private)的,可以修改这些私有的属性和方法为保护(protected)的,如果不希望修改框架自带的方法的话,也可以在子类中再定义这些属性和方法。

测试的控制器位于/Application/Home/Controller/TestVerifyController.class.php

测试的试图位于/Application/Home/View/User/verify.html

自定义的子类位于/Applicaion/Home/Common/VerifyProcess.class.php

VerifyProcess.class.php:

namespaceHome\Common;

useThink\Verify;

classVerifyProcessextendsVerify{

private$_image=NULL;//验证码图片实例

private$_color=NULL;//验证码字体颜色

publicfunctionentryProcess($id=''){

//图片宽(px)

$this-imageW||$this-imageW=$this-length*$this-fontSize*1.5+

$this-length*$this-fontSize/2;

//图片高(px)

$this-imageH||$this-imageH=$this-fontSize*2.5;

//建立一幅$this-imageWx$this-imageH的图像

$this-_image=imagecreate($this-imageW,$this-imageH);

//设置背景

imagecolorallocate($this-_image,$this-bg[0],$this-bg[1],$this-bg[2]);

//验证码字体随机颜色

$this-_color=imagecolorallocate($this-_image,mt_rand(1,150),

mt_rand(1,150),mt_rand(1,150));

//验证码使用随机字体

$ttfPath=$_SERVER['DOCUMENT_ROOT'].'/ThinkPHP/Library/Think/Verify/'.

($this-useZh'zhttfs':'ttfs').'/';

if(empty($this-fontttf)){

$dir=dir($ttfPath);

$ttfs=array();

while(false!==($file=$dir-read())){

if($file[0]!='.'substr($file,-4)=='.ttf'){

$ttfs[]=$file;

$dir-close();

$this-fontttf=$ttfs[array_rand($ttfs)];

$this-fontttf=$ttfPath.$this-fontttf;

if($this-useImgBg){

$this-_background();

if($this-useNoise){

//绘杂点

$this-_writeNoise();

if($this-useCurve){

//绘干扰线

$this-_writeCurve();

//绘验证码

$codeNX=0;//验证码第N个字符的左边距

//验证码为简单运算

$a=mt_rand(1,9);

$b=mt_rand(1,9);

$operate_array=array('+','-','*');

$key=mt_rand(0,count($operate_array)-1);

if($operate_array[$key]=='+'){//加法

$code=$a.'+'.$b.'=';

$result=intval($a+$b);

}elseif($operate_array[$key]=='-'){//减法

$code=max($a,$b).'-'.min($a,$b).'=';

$result=intval(abs($a-$b));

}else{//乘法

$code=$a.'*'.$b.'=';

$result=intval($a*$b);

$this-length=4;

for($i=0;$i$this-length;$i++){

$codeNX+=mt_rand($this-fontSize*1.2,$this-fontSize*1.6);

imagettftext($this-_image,$this-fontSize,mt_rand(-40,40),

$codeNX,$this-fontSize*1.6,$this-_color,$this-fontttf,$code[$i]);

//保存验证码

$key=$this-authcode($this-seKey);

$result=$this-authcode($result);

$secode=array();

$secode['verify_code']=$result;//把校验码保存到session

$secode['verify_time']=NOW_TIME;//验证码创建时间

session($key.$id,$secode);

header('Cache-Control:private,max-age=0,no-store,no-cache,must-revalidate');

header('Cache-Control:post-check=0,pre-check=0',false);

header('Pragma:no-cache');

header("content-type:image/png");

//输出图像

imagepng($this-_image);

imagedestroy($this-_image);

*画杂点

*往图片上写不同颜色的字母或数字

privatefunction_writeNoise(){

$codeSet='2345678abcdefhijkmnpqrstuvwxyz';

for($i=0;$i$i++){

//杂点颜色

$noiseColor=imagecolorallocate($this-_image,mt_rand(150,225),

mt_rand(150,225),mt_rand(150,225));

for($j=0;$j$j++){

//绘杂点

imagestring($this-_image,5,mt_rand(-10,$this-imageW),

mt_rand(-10,$this-imageH),$codeSet[mt_rand(0,29)],$noiseColor);

*画一条由两条连在一起构成的随机正弦函数曲线作干扰线(你可以改成更帅的曲线函数)

*高中的数学公式咋都忘了涅,写出来

*正弦型函数解析式:y=Asin(ωx+φ)+b

*各常数值对函数图像的影响:

*A:决定峰值(即纵向拉伸压缩的倍数)

*b:表示波形在Y轴的位置关系或纵向移动距离(上加下减)

*φ:决定波形与X轴位置关系或横向移动距离(左加右减)

*ω:决定周期(最小正周期T=2π/∣ω∣)

privatefunction_writeCurve(){

$px=$py=0;

//曲线前部分

$A=mt_rand(1,$this-imageH/2);//振幅

$b=mt_rand(-$this-imageH/4,$this-imageH/4);//Y轴方向偏移量

$f=mt_rand(-$this-imageH/4,$this-imageH/4);//X轴方向偏移量

$T=mt_rand($this-imageH,$this-imageW*2);//周期

$w=(2*M_PI)/$T;

$px1=0;//曲线横坐标起始位置

$px2=mt_rand($this-imageW/2,$this-imageW*0.8);//曲线横坐标结束位置

for($px=$px1;$px=$px2;$px=$px+1){

if($w!=0){

$py=$A*sin($w*$px+$f)+$b+$this-imageH/2;

//y=Asin(ωx+φ)+b

$i=(int)($this-fontSize/5);

while($i0){

imagesetpixel($this-_image,$px+$i,$py+$i,$this-_color);

//这里(while)循环画像素点比imagettftext和imagestring用字体大小一次画出

(不用这while循环)性能要好很多

$i--;

//曲线后部分

$A=mt_rand(1,$this-imageH/2);//振幅

$f=mt_rand(-$this-imageH/4,$this-imageH/4);//X轴方向偏移量

$T=mt_rand($this-imageH,$this-imageW*2);//周期

$w=(2*M_PI)/$T;

$b=$py-$A*sin($w*$px+$f)-$this-imageH/2;

$px1=$px2;

$px2=$this-imageW;

for($px=$px1;$px=$px2;$px=$px+1){

if($w!=0){

$py=$A*sin($w*$px+$f)+$b+$this-imageH/2;

//y=Asin(ωx+φ)+b

$i=(int)($this-fontSize/5);

while($i0){

imagesetpixel($this-_image,$px+$i,$py+$i,$this-_color);

$i--;

/*加密验证码*/

privatefunctionauthcode($str){

$key=substr(md5($this-seKey),5,8);

$str=substr(md5($str),8,10);

returnmd5($key.$str);

*绘制背景图片

*注:如果验证码输出图片比较大,将占用比较多的系统资源

privatefunction_background(){

$path=dirname(__FILE__).'/Verify/bgs/';

$dir=dir($path);

$bgs=array();

while(false!==($file=$dir-read())){

if($file[0]!='.'substr($file,-4)=='.jpg'){

$bgs[]=$path.$file;

$dir-close();

$gb=$bgs[array_rand($bgs)];

list($width,$height)=@getimagesize($gb);

//Resample

$bgImage=@imagecreatefromjpeg($gb);

@imagecopyresampled($this-_image,$bgImage,0,0,0,0,$this-imageW,

$this-imageH,$width,$height);

@imagedestroy($bgImage);

}

TestVerifyController.class.php:

namespaceHome\Controller;

useThink\Controller;

useHome\Common\VerifyProcess;

classTestVerifyControllerextendsController{

//界面

publicfunctionindex(){

$this-display('User/verify');

//验证

publicfunctioncheck_verify(){

$verify=newVerifyProcess();

if(!$verify-check($_POST['verify'])){

$this-error('验证码错误');

//显示验证码

publicfunctionverify(){

$verify=newVerifyProcess();

$verify-entryProcess();

}

verify.html:

!DOCTYPEhtml

htmllang="en"

head

metacharset="UTF-8"

titleDocument/title

scriptsrc="/js/jquery/1.9.1/jquery-1.9.1.min.js"/script

/head

body

formaction="{:U('Home/TestVerify/check_verify','','')}"metho

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论