验证码类
来源:互联网 发布:光纤销售数据 编辑:程序博客网 时间:2024/05/17 09:37
带你体验封装类的简便快捷
知识点:php基本语法
构造方法,析构方法,魔术方法
<?php$code=new code(); $code->outImage();class code{//验证码个数protected $number;protected $codeType;protected $width;protected $height;protected $image;protected $code;//验证码字符串public function __construct($number=4,$codeType=2,$width=100,$height=50){$this->number=$number;$this->codeType=$codeType;$this->width=$width;$this->height=$height;$this->code=$this->createCode();//生成验证码函数//echo $this->code;}public function __destruct(){imagedestory($this->image);//图像销毁应该写到析构方法中}public function __get($name){//魔术方法if($name=='code'){return $this->code;}return false;}protected function createCode(){//通过你的验证码类型给你生成不同的验证switch($this->codeType){case 0://纯数字$code=$this->getNumberCode();break;case 1://纯字母$code=$this->getCharCode();break;case 2://字母和数字$code=$this->getNumCharCode();break;default:die('不支持这种验证码类型');}return $code;}protected function getNumberCode(){$str=join('',range(0,9));return substr(str_shuffle($str),0,$this->number);}protected function getCharCode(){$str=join('',range('a','z'));$str=$str.strtoupper($str);return substr(str_shuffle($str),0,$this->number);}protected function getNumCharCode(){$numStr=join('',range(0,9));$str=join('',range('a','z'));$str=$numStr.$str.strtoupper($str);return substr(str_shuffle($str),0,$this->number);}protected function createImage(){$this->image=imagecreatetruecolor($this->width,$this->height);}protected function fillBack(){imagefill($this->image,0,0,$this->lightColor());}protected function lightColor(){return imagecolorallocate($this->image,mt_rand(130,255),mt_rand(130,255),mt_rand(130,255));}protected function darkColor(){return imagecolorallocate($this->image,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120));}protected function drawChar(){$width=ceil($this->width/$this->number);for($i=0;$i<$this->number;$i++){$x=mt_rand($i*$width+5,($i+1)*$width-5);$y=mt_rand(0,$this->height=15);imagechar($this->image,5,$x,$y,$this->code[$i],$this->darkColor());}}protected function drawDistub(){for($i=0;$i<150;$i++){$x=mt_rand(0,$this->width);$y=mt_rand(0,$this->height);imagesetpixel($this->image,$x,$y,$this->lightColor());}}protected function show(){header('Content-Type:image/png');imagepng($this->image);}public function outImage(){//创建画布,填充背景色,将验证码字符串划到画布中,添加干扰元素,输出并显示$this->createImage();$this->fillBack();$this->drawChar();$this->drawDistub();$this->show();}}?>
步骤:生成验证码,画出验证码(创建画布,填充背景色,将验证码字符串划到画布中,添加干扰元素,输出并显示)
阅读全文
0 0
- 验证码类,生成验证码
- java验证码类
- GIF验证码类
- php验证码类
- 验证码生成类
- 验证码类
- 验证码类
- 验证码类
- php验证码类
- 验证码类
- 测试验证码类
- 验证码类
- GIF验证码类
- PHP验证码类
- 验证码类
- php验证码类
- 验证码生成类
- php 验证码类
- C语言接口与实现【第二章】
- 2017ccpc网赛-1005-CaoHaha's staff
- JavaSE IO
- acm算法分类
- 怎样让函数返回数组
- 验证码类
- 除法器FPGA实现
- hpuoj【1298】杨辉三角2【数组】
- 文章标题
- cdq分治
- [转]vim——打开多个文件、同时显示多个文件、在文件之间切换
- TCP连接的建立与终止
- java获取键盘输入流
- Codeforces Round #330 (Div. 2)C. Warrior and Archer【博弈+逆向思维】