验证码图片类的编写
2019-12-22 18:17
1221 查看
1.配置文件config.php
<?php /** * 验证码类型 codeType int 0:纯数字 1:纯字符串 2:数字和字符串混合 * 验证码长度 length int * 图片宽度 width int * 图片高度 height int */ return [ 'codeType' => 2, 'length' => 5, 'width' => 400, 'height' => 200, ];config.php
2.生成验证码类
<?php //定义验证码类 class Code{ private $length; //验证码长度 private $codeType; //类型 private $code; //验证码 private $width; //宽度 private $height; //高度 private $img; //图片资源 public function __construct() { //引入配置文件 $this->config = require_once './config.php'; $this->length = $this->config['length']; $this->codeType = $this->config['codeType']; $this->width = $this->config['width']; $this->height = $this->config['height']; $this->createCode(); } protected function createCode() { switch($this->codeType){ case 0: $this->code = $this->getNumberCode(); break; case 1: $this->code = $this->getCharCode(); break; case 2: $this->code = $this->getNumCharCode(); break; default: die('验证码类型错误,请重新输入!'); break; } } public function getCode() { return $this->code; } private function getNumberCode() { $number = join('',range(0,9)); return $this->setCode($number); } private function getCharCode() { $str = join('',range('a','z')); $str .= strtoUpper($str); return $this->setCode($str); } private function getNumCharCode() { $number = join('',range(0,9)); $str = join('',range('a','z')); $code = strtoUpper($str).$str.$number; return $this->setCode($code); } private function setCode($string) { return substr(str_shuffle($string),0,$this->length); } //输出图像 public function getImg() { //新建画布 $this->createImg(); //画布填充背景色 $this->fillBackground(); //将验证码写入画布 $this->fillCode(); //加入干扰点 $this->setDistubPoint(); //设置干扰线 $this->setDisEarc(); //显示图像 $this->showImg(); } protected function createImg() { $this->img = imagecreatetruecolor($this->width,$this->height); } protected function fillBackground() { imagefill($this->img,0,0,$this->lightColor()); } private function lightColor() { return imagecolorallocate($this->img,mt_rand(130,255),mt_rand(130,255),mt_rand(130,255)); } private function darkColor() { return imagecolorallocate($this->img,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120)); } protected function fillCode() { $width = ceil($this->width/$this->length); $height = $this->height/2; for($i=0;$i<$this->length;$i++){ $x = mt_rand($i*$width+10,($i+1)*$width-10); $y = mt_rand($height-10,$height+10); imagechar($this->img,5,$x,$y,$this->code[$i],$this->darkColor()); } } //设置干扰点 protected function setDistubPoint() { for($i=0;$i<1000;$i++){ $x = mt_rand(0,$this->width); $y = mt_rand(0,$this->height); imagesetpixel($this->img,$x,$y,$this->darkColor()); } } //设置干扰线段 protected function setDisEarc() { for($i=0;$i<2;$i++){ imagearc ( $this->img , rand(0,$this->width) , rand(0,$this->height) , rand($this->width,$this->width*2) , rand($this->height,$this->height*2) , rand(0,100) , rand(280,270) , $this->darkColor() ); } } protected function showImg() { header('Content-type:image/png'); imagepng($this->img); } } $code = new Code(); $code ->getImg();ValidateCode.php
相关文章推荐
- 在VB上轻松编写读取图片验证码的网络程序(转)
- Python编写一个验证码图片数据标注GUI程序附源码
- 验证码图片的编写与定位
- Java-生成验证码图片(自定义内容,尺寸,路径)
- Web开发中的验证码图片的生成-基于Java的实现
- 验证码的编写
- python简易爬虫编写--图片获取
- C# 产生验证码 利用背景图片绘制
- 基于html中canvas标签的验证码图片生成方法
- 获取指定地址的 JPG 图片,验证码
- Lazy Load 是一个用 JavaScript 编写的 jQuery 插件. 它可以延迟加载长页面中的图片
- 一漂亮的PHP图片验证码实例
- C#编写滤镜 图片色调取反效果(Invert)
- Atom 编写 Markdown 一键上传图片,使用7牛云图床
- JavaScript编写带旋转+线条干扰的验证码脚本实例
- Thinkphp 3.2 验证码图片显示错误解决方法
- Android 利用webservice 获取验证码图片
- Servlet 生成验证码图片
- 生成图片形式的数字字母组合验证码
- 使用ASP生成任意长度彩色BMP验证码图片