PHP常用类------生成验证码类Code
直接附上代码吧!很简单的代码,写一遍基本就会了,主要明白用GD库画图的几个步骤:
1.先创建画布,不然画在哪儿?(imagecreatetruecolor)
2.根据要画的数据,选定颜色 (imagecolorallocate)
3.颜色有了,该考虑画啥了,画圆形,矩形,还是扇形,你喜欢就OK!
4.用什么画(系统中提供很多函数,比如imagerectangle,imagearc)
5.画完之后,有三个选择,最简单的,就是什么也不做;另外两个选择就是显示或保存,可以同时做(imagepng,imagegif)
6.图用完之后,就该释放资源了,有始有终嘛(imagedestroy)
代码如下:
<?php
/**
* 该类实例化的时候需要3个参数
* $width;//验证码的宽,默认值为80px
* $height;//验证码的高,默认值为20px
* $num;//验证码字符的个数,默认值为4
*/
class Code{
private $width;//验证码的宽
private $height;//验证码的高
private $num;//验证码字符的个数
private $code;//验证码的字符串
private $img;//验证码source
function __construct($width=80,$height=20,$num=4){
$this->width=$width;
$this->height=$height;
$this->num=$num;
$this->code=$this->create_code();
}
private function create_canvas(){//创建画布
$this->img=imagecreatetruecolor($this->width,$this->height);
$background_color=imagecolorallocate($this->img,0xFF,0xFF,0xFF);
imagefill($this->img,0,0,$background_color);
$border_color=imagecolorallocate($this->img,0xAA,0xAA,0xAA);
imagerectangle($this->img,0,0,$this->width-1,$this->height-1,$border_color);
}
private function create_code(){//生成验证码的字符串
$src="3456789qwertyupkjhgfdsazxcvbnmQWERTYUPLKJHGFDSAZXCVBNM";
$code="";
for($i=0;$i<$this->num;$i++){
$index=mt_rand(0,strlen($src)-1);
$code.=$src[$index];
}
return $code;
}
private function paint_char(){//将生成的字符串画在画布上
for($i=0;$i<$this->num;$i++){
$char_color=imagecolorallocate($this->img,0xFF,0,0xFF);
$font_size=4;
$x=5+($this->width/$this->num)*$i;
$y=($this->height-imagefontheight($font_size))/2; imagechar($this->img,$font_size,$x,$y,$this->code[$i],$char_color);
}
}
private function add_disturbance(){//添加干扰标记
for($i=0;$i<20;$i++){
$color=imagecolorallocate($this->img,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($this->img,rand(1,$this->width-2),rand(1,$this->height-2),$color);
}
}
private function print_code() {//判断兼容哪种格式
if (imagetypes() & IMG_PNG) {
header("Content-type: image/png");
imagepng($this->img);
} elseif (imagetypes() & IMG_JPG) {
header("Content-type: image/jpeg");
imagejpeg($this->img);
} else {
die("No image support in this PHP server");
}
}
private function get_code(){//获取验证码字符串的值
return $this->code;
}
private function destroy_code(){//释放资源
imagedestroy($this->img);
}
function show_image_code(){//所有步骤的汇集,搞定所有验证码的工作
$this->create_canvas();
$this->paint_char();
$this->add_disturbance();
$this->print_code();
$this->destroy_code();
} } //测试
$code = new Code(80, 30, 4);
$code->show_image_code(); ?>
PHP常用类------生成验证码类Code的更多相关文章
- C#生成验证码类
using System;using System.Collections.Generic;using System.Drawing;using System.Drawing.Drawing2D;us ...
- .net 根据匿名类生成实体类,根据datatable生成实体类,根据sql生成实体类
在开发中可能会遇到这几种情况 1.EF或LINQ查询出来的匿名对象在其它地方调用不方便,又懒的手动建实体类 2.通过datatable反射实体需要先建一个类 ,头痛 3.通过SQL语句返回的实体也需要 ...
- pdo文字水印类,验证码类,缩略图类,logo类
文字水印类 image.class.php <?php /** * webrx.cn qq:7031633 * @author webrx * @copyright copyright (c) ...
- Common工具类的验证码类的使用(未实现验证)
验证码接收 using System; using System.Collections.Generic; using System.Linq; using System.Web; using CZB ...
- Asp.net mvc生成验证码
1.生成验证码类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...
- J2EE如何生成验证码图片和点击刷新验证码
验证码图片生成步骤 创建BufferedImage对象. 获取BufferedImage的画笔,即调用getGraphics()方法获取Graphics对象. 调用Graphics对象的setColo ...
- Java生成验证码_转
为了防止用户恶意,或者使用软件外挂提交一些内容,就得用验证码来阻止,虽然这个会影响用户体验,但为了避免一些问题很多网站都使用了验证码;今天下午参考文档弄了一个验证码,这里分享一下;这是一个web工程, ...
- Laravel 下生成验证码的类
<?php namespace App\Tool\Validate; //验证码类 class ValidateCode { private $charset = 'abcdefghkmnprs ...
- JavaWeb学习总结(七):通过Servlet生成验证码及其应用 (BufferedImage类)
一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:
随机推荐
- C#从入门到精通视频教程(2009年最新)- 视频列表
http://www.jz97.net/index.php/playlist/view/182/page/2 C#从入门到精通视频教程(2009年最新)- 视频列表
- Django admin 的模仿流程
- 从源码的角度分析List与Set的区别
很多时候我们在讨论List与Set的异同点时都在说: 1.List.Set都实现了Collection接口 2.List是有序的,可以存储重复的元素,允许存入null 3.Set是无序的,不允许存储重 ...
- .Net修改网站项目调试时的虚拟目录(未验证)
有些项目需要在IIS发布的时候,将网站发布到虚拟目录,为了保持调试和发布的路径同一,一般会修改VS调试的虚拟目录 一.Web应用程序 Web应用程序的修改方式非常简单,在解决方案资源管理器->项 ...
- 树莓派3b安装Apache2+PHP+MySQL+phpyadmin
树莓派型号:3B+ 系统环境:2017-04-10-raspbian-jessie 先更新一下源 BASIC sudo apt-get update 安装Apache2 BASIC sudo apt- ...
- oracle 索引扫描类型的分类与构造
1. INDEX RANGE SCAN--请记住这个INDEX RANGE SCAN扫描方式drop table t purge;create table t as select * from dba ...
- <<linux device driver,third edition>> Chapter 4:Debugging Techniques
Debugging by Printing printk lets you classify messages accoring to their severity by associating di ...
- Winform 基础一 panel
一 居上.居中.居下 二 添加子控件 三 适应不同分辨率 四 内容超出,显示滚动条 一 .居上.居中.居下 二.添加子页面 Form7 childFrm = new Form7(); childFrm ...
- HiKey软硬件开发环境及其调试
HiKey是一款搭载华为海思麒麟620芯片,符合Linaro 96Boards标准的SBC开发板.它采用8核64位Cortex-A53处理器,主频高达1.2GHz. HiKey作为AOSP支持的一款产 ...
- 一个简单的javascript节流器实现
节流器 javascript的节流器主要用于延缓某些动作的执行,比如ajax请求,如果input框注册了input事件,那么当用户输入时就会持续的触发这个事件,如果回调函数中持续的通过ajax调用后台 ...