php实现随机数字、字母的验证码
php实现随机数字、字母的验证码
可自定义生成验证码文字的大小、数量、干扰项等等,也可以自定义验证文字的字体。。。
废话不多说,直接上代码:
- 1、classgd.class.php
- <?php
- Class Captcha{
- private $_fontfile='';
- private $_size=36;
- private $_width=200;
- private $_height=100;
- private $_length=4;
- private $_image=null;
- private $_snow=0;
- private $_pixel=0;
- private $_line=0;
- public function __construct($config=array()){
- if(is_array($config)&&count($config)>0){
- if(isset($config['fontfile'])&&is_file($config['fontfile'])&&is_readable($config['fontfile'])){
- $this->_fontfile=$config['fontfile'];
- }else{
- return false;
- }
- if(isset($config['size'])&&$config['size']>0){
- $this->_size=(int)$config['size'];
- }
- if(isset($config['width'])&&$config['width']>0){
- $this->_width=(int)$config['width'];
- }
- if(isset($config['height'])&&$config['height']>0){
- $this->_height=(int)$config['height'];
- }
- if(isset($config['length'])&&$config['length']>0){
- $this->_length=(int)$config['length'];
- }
- if(isset($config['snow'])&&$config['snow']>0){
- $this->_snow=(int)$config['snow'];
- }
- if(isset($config['pixel'])&&$config['pixel']>0){
- $this->_pixel=(int)$config['pixel'];
- }
- if(isset($config['line'])&&$config['line']>0){
- $this->_line=(int)$config['line'];
- }
- $this->_image=imagecreatetruecolor($this->_width,$this->_height);
- return $this->_image;
- }
- else{
- return false;
- }
- }
- public function getCaptcha(){
- $white=imagecolorallocate($this->_image,255,255,255);
- imagefilledrectangle($this->_image,0,0,$this->_width,$this->_height,$white);
- $str=$this->_generateStr($this->_length);
- if(false===$str){
- return false;
- }
- $fontfile=$this->_fontfile;
- for($i=0;$i<$this->_length;$i++){
- $size=$this->_size;
- $angle=mt_rand(-30,30);
- $x=ceil($this->_width/$this->_length)*$i+mt_rand(5,10);
- $y=ceil($this->_height/1.5);
- $color=$this->_getRandColor();
- //针对中文字符截取
- //$text=mb_substr($str,$i,1,'utf-8');
- $text=$str{$i};
- imagettftext($this->_image, $size, $angle, $x, $y, $color, $fontfile, $text);
- }
- if($this->_snow){
- $this->_getSnow();
- }else{
- if($this->_pixel){
- $this->_getPixel();
- }
- if($this->_line){
- $this->_getLine();
- }
- }
- header('content-type:image/png');
- imagepng($this->_image);
- imagedestroy($this->_image);
- return strtolower($str);
- }
- private function _getSnow(){
- for($i=1;$i<=$this->_snow;$i++){
- imagestring($this->_image,mt_rand(1,5),mt_rand(0,$this->_width),mt_rand(0,$this->_height),'*',$this->_getRandColor());
- }
- }
- private function _getPixel(){
- for($i=1;$i<=$this->_pixel;$i++){
- imagesetpixel($this->_image,mt_rand(0,$this->_width),mt_rand(0,$this->_height),$this->_getRandColor());
- }
- }
- private function _getLine(){
- for($i=1;$i<=$this->_line;$i++){
- imageline($this->_image,mt_rand(0,$this->_width),mt_rand(0,$this->_height),mt_rand(0,$this->_width),mt_rand(0,$this->_height),$this->_getRandColor());
- }
- }
- private function _generateStr($length=4){
- if($length<1 || $length>30){
- return false;
- }
- $chars=array(
- 'a','b','c','d','e','f','g','h','k','m','n','p','x','y','z',
- 'A','B','C','D','E','F','G','H','K','M','N','P','X','Y','Z',
- 1,2,3,4,5,6,7,8,9
- );
- $str=join('',array_rand(array_flip($chars),$length));
- return $str;
- }
- private function _getRandColor(){
- return imagecolorallocate($this->_image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
- }
- }
- ?>
2、testCaptcha.php
- <?php
- require_once 'classgd.class.php';
- $config=array(
- 'fontfile'=>'fonts/simfang.ttf', //引入字体文件
- //'snow'=>50,
- 'pixel'=>100,
- 'line'=>10
- );
- $captcha=new Captcha($config);
- $captcha->getCaptcha();
- ?>
php实现随机数字、字母的验证码的更多相关文章
- 随机发送n位数字+字母的验证码
''' 随机发送n位数字+字母的验证码 ''' import random def get_verified(length): code = '' for i in range(length): nu ...
- javascript原生 实现数字字母混合验证码
实现4位数 数字字母混合验证码(数字+大写字母+小写字母) ASCII 字符集中得到3个范围: 1. 48-57 表示数字0-9 2. 65-90 表示大写字母 3. 97-122 表示小写字母 范围 ...
- QTP生成随机数字+字母
以下函数实现随机生成17位数(包括字母和数字),仍有改进的空间,可根据具体要求适当修改 Dim targetstring '调用返回函数给变量.Function过程通过函数名返回一个值 targets ...
- C# 生成四位数字字母混合验证码
private static void Rand() { var arr = new List<string>(); ; i < ; i++) { arr.Add(i.ToStrin ...
- 用js做数字字母混合的随机四位验证码
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Servlet实现数字字母验证码图片(二)
Servlet实现数字字母验证码图片(二): 生成验证码图片主要用到了一个BufferedImage类,如下:
- js随机生成字母数字组合的字符串 随机动画数字
效果描述: 附件中只有一个index.html文件有效 其中包含css以及html两部分内容 纯js生成的几个随机数字 每次都不重复,点击按钮后再次切换 使用方法: 1.将css样式引入到你的网页中 ...
- Java随机生成定长纯数字或数字字母混合数
(转)Java随机生成定长纯数字或数字字母混合数 运行效果图: 具体实现代码
- 绘制字母和数字组合的验证码(原生php)
<?php $font = array('font/FZZQJW.TTF','font/STHUPO.TTF');//字体 $str = '689acdefhjkmnpqrtuvwxyACDEF ...
随机推荐
- Express开发性能优化
1.使用浏览器缓存 在app.js里添加 var CACHETIME = 60 * 1000 * 60 * 24 * 30; app.use(express.static(path.join(__di ...
- Firebird shadow
火鸟数据库的shadow,即实时镜像. 主库发生变化,shadow也跟随变化,防止任何意外造成主库损坏无法使用,当然shadow可以有多个. 1.创建shadow的准备:修改Firebird.conf ...
- Python__函数和代码复用
主要内容 函数的定义和使用 实例:七段数码管的绘制 代码复用与函数递归 PyInstall库的使用 实例:科赫雪花小包裹 函数的定义与使用 函数的理解与定义 函数的使用及调用过程 函数的参数传递 函数 ...
- 悟空模式-java-单例模式
[那座山,正当顶上,有一块仙石.其石有三丈六尺五寸高,有二丈四尺围圆.三丈六尺五寸高,按周天三百六十五度:二丈四尺围圆,按政历二十四气.上有九窍八孔,按九宫八卦.四面更无树木遮阴,左右倒有芝兰相衬.盖 ...
- Linux安装jdk,编写helloworld程序
今天学习了Linux安装jdk,做个笔记记录一下. 第一步,确定Linux是32位的还是64位的,然后到oracle官网上下载对应版本的jdk,一般下载.tar.gz文件.查看Linux的版本的命令是 ...
- ARM体系结构和汇编指令
第一节 可编程器件的编程原理 1. 可编程器件的特点 1 . CPU在固定频率的时钟控制下节奏运行 2 . CPU可以通过总线读取外部存储设备中的二进制指令集,然后解码执行 3 . 这些可以被CPU解 ...
- Mysql-安装指南
1.设置用户名密码 首次登录后修改密码如下: 如果密码设置太过简单会报以下错误 mysql修改密码Your password does not satisfy the current policy r ...
- Getting Started with Erlang
Getting Started with Erlang Erlang is a great language that lets you build highly concurrent applica ...
- AE中IHookHelper的用法 来自http://blog.sina.com.cn/s/blog_6faf711d0100xs1x.html
IHookHelper 主要在用在自定义类型于AE带的的ICommand或ITool等, 1.实例化IHookHelper 对象: IHookHelper m_hookHelper = new Hoo ...
- jQuery实现大图轮播
css样式: *{ margin: 0; padding: 0;}ul{ list-style:none;}.slideShow{ width: 620px; heigh ...