自用captcha验证码类

  1 <?php
  2     //header("Content-type: image/GIF");
  3     class Captcha{
  4         private $width;
  5         private $height;
  6         private $counts;//要生成的验证码个数
  7         private $distrubCode;//用于生成验证码的字符串
  8         private $fontUrl;//字体
  9 
 10         public function __construct($arr = array()){
 11             $this->width = isset($arr['width']) ? $arr['width'] : 120;
 12             $this->height = isset($arr['height']) ? $arr['height'] : 30;
 13             $this->counts = isset($arr['counts']) ? $arr['counts'] : 5;
 14             $this->distrubCode = isset($arr['distrubCode']) ? $arr['distrubCode'] : '1235467890qwertyuipkjhgfdaszxcvbnm';
 15             $this->fontUrl = isset($arr['fontUrl']) ? $arr['fontUrl'] : 'ITCBLKAD.TTF';
 16             //session_start();
 17             $this->getCaptchar();
 18         }
 19         //输出验证码
 20         public function getCaptchar(){
 21             //创建图片在内存
 22             $image = $this->createImageSource();
 23             //设置图片背景色
 24             $this->setImageBGColor($image);
 25             //设置验证码
 26             $this->setCode($image);
 27             //字符干扰码
 28             $this->setDistrubCode($image);
 29             //黑点干扰码
 30             //$this->setDistrubPixel($image);
 31             //干扰线
 32             //$this->setDistrubLine($image);
 33             //雪花干扰
 34             //$this->setDistrubString($image);
 35             //生成圆孤干扰
 36             //$this->setDistrubArc($image);
 37             //生成虚线干扰
 38             //$this->setDistrubImaginary($image);
 39             //输出图片
 40             imagegif($image);
 41             //从内存中销毁
 42             imagedestroy($image);
 43         }
 44         //创建画布
 45         private function createImageSource(){
 46             return imagecreatetruecolor($this->width,$this->height);
 47         }
 48         //设置画布的背景色
 49         private function setImageBGColor($image){
 50             //创建画布的背景色
 51             $bgcolor = imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
 52             //为画布填充背景色
 53             imagefill($image,0,0,$bgcolor);
 54         }
 55         //设置验证码中的干扰码
 56         private function setDistrubCode($image){
 57             $count_h = $this->height;
 58             //设置干扰码的数量
 59             $cou = floor($count_h/2);
 60             for($i = 0;$i<$cou;$i++){
 61                 //随机生成X,Y
 62                 $x = mt_rand(0,$this->width);
 63                 $y = mt_rand(0,$this->height);
 64                 //生成倾斜的角度
 65                 $angle = mt_rand(0,360);
 66                 //生成字体的大小
 67                 $fontSize = mt_rand(8,15);
 68                 //字体的样式
 69                 $fontUrl = $this->fontUrl;
 70                 //用于生成验证码的字符串
 71                 $distrubCode = $this->distrubCode;
 72                 //取得字符串的长度
 73                 $codeLenth = strlen($distrubCode);
 74                 //随机取出一个字符
 75                 $dscode = $distrubCode[mt_rand(0,$codeLenth-1)];
 76                 //创建的一个字体颜色
 77                 $fontColor = imagecolorallocate($image,mt_rand(40,140),mt_rand(40,140),mt_rand(40,140));
 78                 //把字体添加到画布上
 79                 imagettftext($image,$fontSize,$angle,$x,$y,$fontColor,$fontUrl,$dscode);
 80             }
 81         }
 82         //生成验证码
 83         private function setCode($image){
 84             $width = $this->width;
 85             $height = $this->height;
 86             $counts = $this->counts;
 87             $code = $this->sessionCode();
 88             //生成Y坐标
 89             $y = floor($height/2)+floor($height/4)+8;
 90             $fontSize = mt_rand(30,35);
 91             //$fontUrl = 'C:\Windows\Fonts\ALGER.TTF';
 92             $fontUrl = 'ALGER.TTF';
 93             //$fontUrl = $this->fontUrl;
 94             //添加验证码
 95             $counts=$this->counts;
 96             for($i=0;$i<$counts;$i++){
 97                 //取得字符
 98                 $char = $code[$i];
 99                 //生成X的坐标
100                 $x = floor($width/$counts-1)*$i+3;
101                 //倾斜角度
102                 $angle = mt_rand(-20,30);
103                 $fontColor = imagecolorallocate($image,mt_rand(0,50),mt_rand(50,100),mt_rand(50,140));
104                 //把字体添加到画布上
105                 imagettftext($image,$fontSize,$angle,$x,$y,$fontColor,$fontUrl,$char);
106             }
107         }
108         //生成要进行验证的字符串
109         public function sessionCode(){
110             $code = $this->distrubCode;
111             $len = strlen($code);
112             $_dscode = '';
113             $counts = $this->counts;
114             //取得要验证的字符串
115             for($i = 0;$i<$counts;$i++){
116                 $dscode = $code[mt_rand(0,$len-1)];
117                 $_dscode .= $dscode;
118             }
119             $_SESSION['captcha'] = $_dscode;
120             return $_dscode;
121         }
122         //生成黑点干扰码
123         private function setDistrubPixel($image){
124             $width = $this->width;
125             $height = $this->height;
126             //生成黑点
127             for($i=0;$i<$width*5;$i++){
128                 $pixelColor = imagecolorallocate($image,mt_rand(100,200),mt_rand(100,200),mt_rand(100,200));
129                 imagesetpixel($image,mt_rand(0,$width),mt_rand(0,$height),$pixelColor);
130             }
131         }
132         //生成干扰线
133         private function setDistrubLine($image){
134             $width = $this->width;
135             $height = $this->height;
136             //生成干扰线
137             for($i=0;$i<30;$i++){
138                 $lineColor = imagecolorallocate($image,mt_rand(100,200),mt_rand(100,200),mt_rand(100,200));
139             imageline($image,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$width),mt_rand(0,$height),$lineColor);
140             }
141         }
142         //生成雪花干扰
143         private function setDistrubString($image){
144             $width = $this->width;
145             $height = $this->height;
146             //生成随机雪花
147             for($i=0;$i<50;$i++){
148                 $stringColor = imagecolorallocate($image,mt_rand(100,200),mt_rand(100,200),mt_rand(100,200));
149                 imagestring($image,mt_rand(1,5),mt_rand(0,$width),mt_rand(0,$height),'*',$stringColor);
150             }
151         }
152         //生成圆弧干扰
153         private function setDistrubArc($image){
154             $width = $this->width;
155             $height = $this->height;
156             //生成随机圆弧
157             for($i=0;$i<30;$i++){
158                 $arcColor = imagecolorallocate($image,mt_rand(100,200),mt_rand(100,200),mt_rand(100,200));
159                 imagearc($image,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$width/2),mt_rand(0,$height/2),mt_rand(0,360),mt_rand(100,360),$arcColor);
160             }
161         }
162         //生成虚线干扰
163         private function setDistrubImaginary($image){
164             $width = $this->width;
165             $height = $this->height;
166             //生成虚线干扰线
167             for($i=0;$i<20;$i++){
168                 $black = imagecolorallocate($image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
169                 $gray = imagecolorallocate($image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
170                    $style = array ($black,$black,$black,$black,$black,$gray,$gray,$gray,$gray,$gray);
171                 imagesetstyle($image,$style);
172                 imageline($image,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$width),mt_rand(0,$height),IMG_COLOR_STYLED);
173             }
174         }
175         public static function checkCaptcha($char){
176             return (strtolower($char) === strtolower($_SESSION['captcha']));
177         }
178     }

 

转载于:https://www.cnblogs.com/he9527/p/5864563.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Captcha验证码是一种用于验证用户身份的技术。它通常以图像形式出现,要求用户在输入框中输入由图像中显示的文字或数字。这种验证码的目的是区分机器和人用户,以防止自动化程序或恶意软件的攻击。 在您提供的引用内容中,描述了一种实现常规输入验证码的方式,其中右边显示验证码图片,用户可以点击刷新验证码,左边的输入框用于用户输入验证码。这种方式是一种常见的验证码实现方式。 提到了使用深度学习来识别captcha验证码的项目实践。该项目使用Keras库构建了一个深度卷积神经网络,以识别captcha验证码。这种方法利用神经网络的模式识别能力,通过训练来学习captcha验证码的特征,并能够在实际应用中自动识别验证码。 提到了登录验证中常见的一种方式,即输入式验证码。这种验证码要求用户在输入框中手动输入由图像中显示的文字或数字,以验证用户身份。 综上所述,captcha验证码是一种用于验证用户身份的技术,通常以图像形式出现,并要求用户在输入框中输入由图像中显示的文字或数字。常规的输入验证码方式是在界面中显示验证码图片,用户点击刷新验证码,然后在相应输入框中输入验证码。深度学习可以用于识别captcha验证码,通过训练神经网络来自动识别验证码。输入式验证码是登录验证中常见的一种方式,要求用户手动输入验证码以完成验证。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值