获取验证码随机字符串@return string $captcha,随机验证码文字
<?php
//验证码工具类
class Captcha{
//属性
private $width;
private $height;
private $fontsize;
private $pixes;
private $lines;
private $str_len;
/*
* 构造方法
* @param1 array $arr = array(),初始化属性的关联数组
*/
public function __construct($arr = array()){
//初始化
$this->width = isset($arr['width']) ? $arr['width'] : $GLOBALS['config']['captcha']['width'];
$this->height = isset($arr['height']) ? $arr['height'] : $GLOBALS['config']['captcha']['height'];
$this->fontsize = isset($arr['fontsize']) ? $arr['fontsize'] : $GLOBALS['config']['captcha']['fontsize'];
$this->pixes = isset($arr['pixes']) ? $arr['pixes'] : $GLOBALS['config']['captcha']['pixes'];
$this->lines = isset($arr['lines']) ? $arr['lines'] : $GLOBALS['config']['captcha']['lines'];
$this->str_len = isset($arr['str_len']) ? $arr['str_len'] : $GLOBALS['config']['captcha']['str_len'];
}
/*
* 产生验证码图片
*/
public function generate(){
//制作画布
$img = imagecreatetruecolor($this->width,$this->height);
//给定背景色
$bg_color = imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagefill($img,0,0,$bg_color);
//制作干扰线
$this->getLines($img);
//增加干扰点
$this->getPixels($img);
//增加验证码文字
$captcha = $this->getCaptcha();
//文字颜色
$str_color = imagecolorallocate($img,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
//写入文字
//计算文字应该出现的起始位置
$start_x = ceil($this->width/2) - 25;
$start_y = ceil($this->height/2) - 2881064151;
if(imagestring($img,$this->fontsize,$start_x,$start_y,$captcha,$str_color)){
//成功:输出验证码
header('Content-type:image/png');
imagepng($img);
}else{
//失败
return false;
}
}
/*
* 获取验证码随机字符串
* @return string $captcha,随机验证码文字
*/
private function getCaptcha(){
//获取随机字符串
$str = implode('',array_merge(range('a','z'),range('A','Z'),range(1,9)));
//随机取
$captcha = ''; //保存随机字符串
for($i = 0,$len = strlen($str);$i < $this->str_len;$i++){
//每次随机取一个字符
$captcha .= $str[mt_rand(0,$len - 1)] . ' ';
}
//将数据保存到session
$_SESSION['captcha'] = str_replace(' ','',$captcha);
//返回值
return $captcha;
}
/*
* 增加干扰点
* @param1 resource $img
*/
private function getPixels($img){
//增加干扰点
for($i = 0;$i < $this->pixes;$i++){
//分配颜色
$pixel_color = imagecolorallocate($img,mt_rand(100,150),mt_rand(100,150),mt_rand(100,150));
//画点
imagesetpixel($img,mt_rand(0,$this->width),mt_rand(0,$this->height),$pixel_color);
}
}
/*
* 增加干扰线
* @param1 resource $img,要增加干扰线的图片资源
*/
private function getLines($img){
//增加干扰线
for($i = 0;$i < $this->lines;$i++){
//分配颜色
$line_color = imagecolorallocate($img,mt_rand(150,200),mt_rand(150,200),mt_rand(150,200));
//画线
imageline($img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$line_color);
}
}
/*
* 验证验证码
* @param1 string $captcha,用户提交的验证码
* @return bool,成功返回true,失败返回false
*/
public static function checkCaptcha($captcha){
//验证码不区分大小写
return (strtolower($captcha) === strtolower($_SESSION['captcha']));
}
}
获取验证码随机字符串@return string $captcha,随机验证码文字的更多相关文章
- PHP学习笔记:万能随机字符串生成函数(已经封装好)
做验证码用到的,然后就把这个函数封装起来,使用时候要设置2个参数: $str设置里要被采集的字符串,比如: $str='efasfgzsrhftjxjxjhsrth'; 则在函数里面生成的字符串就回从 ...
- 【代码笔记】iOS-产生随机字符串
一,代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, ...
- java 与日期转换相关的方法(java.util.date类型和java.sql.date类型互相转换)、随机字符串生成方法、UUID生产随机字符串
package com.oop.util; import java.text.*; import java.util.UUID; import org.junit.Test; /* * 与日期相关的工 ...
- shell 生成指定范围随机数与随机字符串 .
shell 生成指定范围随机数与随机字符串 分类: shell 2014-04-22 22:17 20902人阅读 评 ...
- random and password 在Linux下生成crypt加密密码的方法,shell 生成指定范围随机数与随机字符串
openssl rand -hex n (n is number of characters) LANG=c < /dev/urandom tr -dc _A-Z-a-z-0-9 | head ...
- php生成随机字符串和验证码的类
网上有很多的php随机数与验证码的代码与文章,真正适用的没有几个. 索性自己搞一个吧. 开始本节的php教程 吧,以下代码的实现,主要做到可以很好区分一个get_code(),另一个create_ch ...
- go golang 判断base64数据 获取随机字符串 截取字符串
go golang 判断base64数据 获取随机字符串 截取字符串 先少写点,占个坑,以后接着加. 1,获取指定长度随机字符串 func RandomDigits(length int) strin ...
- js随机生成一个数组中的随机字符串以及更新验证码
随机生成m,n范围的值得公式: Math.random()*(n-m)+m: 改正公式:Math.random()*(n+1-m)+m // 生成随机字符串function randomMixed(n ...
- php获取随机字符串的几种方法
方法一:shuffle函数(打乱数组)和mt_rand函数(生成随机数,比rand速度快四倍) /** * 获得随机字符串 * @param $len 需要的长度 * @param $special ...
随机推荐
- linux下主流的三种远程技术
远程登录操作对于租用服务器的用户来说都不陌生.特别是租用国外服务器的用户来说,更是家常便饭.通过远程登录操作,即使我们人在深圳,也能无差别的操作远在美国的服务器.而对于linux系统下的服务器,目前主 ...
- myeclipse安装jad反编译插件
有时候想深入底层看jar包封装的源代码,但是打不开.这就需要配置反编译插件: 1:准备原材料 jad.exe + net.sf.jadclipse_3.3.0.jar 下载目录: jad.exe : ...
- mysql数据库中,如何对json数据类型的值进行修改?通过json_set函数对json字段值进行修改?
需求描述: 今天在看mysql中存放json数据类型的问题,对于json数据进行修改的操作, 在此记录下. 操作过程: 1.创建包含json数据类型的表,插入基础数据 mysql> create ...
- MTK 隐藏通知栏
步骤: 源码/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarVie ...
- Spring中的类型转换与数据绑定(PropertyEditor、ConversionService、Data Binding、Formatter)
Spring早期使用PropertyEditor进行Object与String的转换.到Spring 3后,Spring提供了统一的ConversionService API和强类型的Converte ...
- iOS_UITextField 基本操作
基本操作 UITextField *userNameTextField = [[UITextField alloc] init]; userNameTextField.frame = CGRectMa ...
- OO模式-Singleton
讨论一: 既然仅仅有一个类?为什么非要用一个模式来定义?难道就不能用程序猿之间的约定又或者使用伟大的设计模式来完毕? 1)先来说说全局变量的优点,当定义一个全局变量时,不论什么一个函数或者一行代码都能 ...
- SpringMVC由浅入深day01_5注解的处理器映射器和适配器
5 注解的处理器映射器和适配器 在spring3.1之前使用org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandle ...
- DropDownListFor的种种纠结(禁止转载)
严重禁止转载,好多爬虫软件为了浏览到处抓东西,真缺德 具有键“CorpType”的 ViewData 项属于类型“System.Int64”,但它必须属于类型“IEnumerable<Selec ...
- 【Android】amr播放
http://download.csdn.net/download/r8hzgemq/4877495 http://www.cnblogs.com/fengzhblog/archive/2013/08 ...