当前位置: 首页 > news >正文

【转】验证码类

经典的php验证码类,随笔记一下,以后用的上的时候方便查找。

封装的captcha.class.php

 1 <?php 
 2 class captcha{
 3     private $width;//
 4     private $height;//
 5     private $codeNum;//数量
 6     private $code;//
 7     private $im;//
 8  
 9     function __construct($width=80, $height=20, $codeNum=4)
10     {
11         $this->width = $width;
12         $this->height = $height;
13         $this->codeNum = $codeNum;
14     }
15  
16     function showImg()
17     {
18         //创建图片
19         $this->createImg();
20         //设置干扰元素
21         $this->setDisturb();
22         //设置验证码
23         $this->setCaptcha();
24         //输出图片
25         $this->outputImg();
26     }
27  
28     function getCaptcha()
29     {
30         return $this->code;
31     }
32  
33     private function createImg()
34     {
35         $this->im = imagecreatetruecolor($this->width, $this->height);
36         $bgColor = imagecolorallocate($this->im, 0, 0, 0);
37         imagefill($this->im, 0, 0, $bgColor);
38     }
39  
40     private function setDisturb()
41     {
42         $area = ($this->width * $this->height) / 20;
43         $disturbNum = ($area > 250) ? 250 : $area;
44         //加入点干扰
45         for ($i = 0; $i < $disturbNum; $i++) {
46             $color = imagecolorallocate($this->im, rand(0, 255), rand(0, 255), rand(0, 255));
47             imagesetpixel($this->im, rand(1, $this->width - 2), rand(1, $this->height - 2), $color);
48         }
49         //加入弧线
50         for ($i = 0; $i <= 5; $i++) {
51             $color = imagecolorallocate($this->im, rand(128, 255), rand(125, 255), rand(100, 255));
52             imagearc($this->im, rand(0, $this->width), rand(0, $this->height), rand(30, 300), rand(20, 200), 50, 30, $color);
53         }
54     }
55  
56     private function createCode()
57     {
58         $str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ";
59  
60         for ($i = 0; $i < $this->codeNum; $i++) {
61             $this->code .= $str{rand(0, strlen($str) - 1)};
62         }
63     }
64  
65     private function setCaptcha()
66     {
67         $this->createCode();
68  
69         for ($i = 0; $i < $this->codeNum; $i++) {
70             $color = imagecolorallocate($this->im, rand(50, 250), rand(100, 250), rand(128, 250));
71             $size = rand(floor($this->height / 5), floor($this->height / 3));
72             $x = floor($this->width / $this->codeNum) * $i + 5;
73             $y = rand(0, $this->height - 20);
74             imagechar($this->im, $size, $x, $y, $this->code{$i}, $color);
75         }
76     }
77  
78     private function outputImg()
79     {
80         if (imagetypes() & IMG_JPG) {
81             header('Content-type:image/jpeg');
82             imagejpeg($this->im);
83         } elseif (imagetypes() & IMG_GIF) {
84             header('Content-type: image/gif');
85             imagegif($this->im);
86         } elseif (imagetype() & IMG_PNG) {
87             header('Content-type: image/png');
88             imagepng($this->im);
89         } else {
90             die("Don't support image type!");
91         }
92     }
93 }
94     
95 ?>

demo.php

1 <?php 
2     session_start();
3     require('captcha.class.php');
4     $captcha = new captcha(80,20,4);
5     $code = $captcha->getCaptcha();
6     $_SESSION['code'] = $code;
7     $captcha->showImg();
8 ?>

 

转载于:https://www.cnblogs.com/mashangfei/p/4884878.html

相关文章:

  • Lync Server 2010标准版前端服务器迁移之二:迁移用户及中央管理存储
  • 何谓集群
  • mysql 使用小结
  • 基于Qt有限状态机的一种实现方式和完善的人工智能方法
  • 【Android】高德地图 缩放级别及像素以及地图上的点转化成屏幕上的点
  • JAVA基础学习day26--正则表达式
  • css3中的伪元素
  • lua程序设计
  • yii2 model 给模型赋值(花了我将近30分钟)
  • CacheManager:–个通用缓存接口抽象类库
  • Map小结
  • Android中实现app版本更新
  • linux内核优化
  • 媒体类型(MIME类型)
  • AngularJS指令开发(1)——参数详解
  • python3.6+scrapy+mysql 爬虫实战
  • [js高手之路]搞清楚面向对象,必须要理解对象在创建过程中的内存表示
  • angular2开源库收集
  • AWS实战 - 利用IAM对S3做访问控制
  • C++11: atomic 头文件
  • canvas 绘制双线技巧
  • Java,console输出实时的转向GUI textbox
  • JavaScript DOM 10 - 滚动
  • JSONP原理
  • Laravel 中的一个后期静态绑定
  • Laravel5.4 Queues队列学习
  • Promise初体验
  • Python爬虫--- 1.3 BS4库的解析器
  • Vultr 教程目录
  • 扑朔迷离的属性和特性【彻底弄清】
  • 前端技术周刊 2018-12-10:前端自动化测试
  • 区块链共识机制优缺点对比都是什么
  • 区块链技术特点之去中心化特性
  • 日剧·日综资源集合(建议收藏)
  • 手机端车牌号码键盘的vue组件
  • 线上 python http server profile 实践
  • 想晋级高级工程师只知道表面是不够的!Git内部原理介绍
  • 如何在 Intellij IDEA 更高效地将应用部署到容器服务 Kubernetes ...
  • 支付宝花15年解决的这个问题,顶得上做出十个支付宝 ...
  • ​​​​​​​Installing ROS on the Raspberry Pi
  • ​MySQL主从复制一致性检测
  • # Pytorch 中可以直接调用的Loss Functions总结:
  • (01)ORB-SLAM2源码无死角解析-(66) BA优化(g2o)→闭环线程:Optimizer::GlobalBundleAdjustemnt→全局优化
  • (C语言)深入理解指针2之野指针与传值与传址与assert断言
  • (HAL库版)freeRTOS移植STMF103
  • (Matlab)使用竞争神经网络实现数据聚类
  • (Mirage系列之二)VMware Horizon Mirage的经典用户用例及真实案例分析
  • (附源码)node.js知识分享网站 毕业设计 202038
  • (附源码)springboot优课在线教学系统 毕业设计 081251
  • (附源码)计算机毕业设计ssm本地美食推荐平台
  • (免费分享)基于springboot,vue疗养中心管理系统
  • (终章)[图像识别]13.OpenCV案例 自定义训练集分类器物体检测
  • *2 echo、printf、mkdir命令的应用
  • ./和../以及/和~之间的区别
  • .NET MVC、 WebAPI、 WebService【ws】、NVVM、WCF、Remoting