获取验证码随机字符串@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 ...
随机推荐
- opencv3.2将中文输出到图片上
opencv自带的putText函数无法输出utf8类型的字符,因此无法将中文打印到图片上.用这篇文章的freetype可以实现中文输出,但是需要将字符解码转码比较麻烦,而Pillow的Image函数 ...
- less语法(一)变量与extend
摘要: 作为 CSS 的一种扩展,Less 不仅完全兼容 CSS 语法,而且连新增的特性也是使用 CSS 语法.这样的设计使得学习 Less 很轻松,而且你可以在任何时候回退到 CSS.less文件是 ...
- [原]unity3d刀光剑影(二)
本篇研究 暴走武侠 效果.直接贴代码 Shader "Cg shader with single texture" { Properties { _MainTex ("T ...
- 【scala】 scala 类 (五)
1.scala类 1.class 关键字 2.var 属性 默认生成getter/setter 方法 3.val 属性 默认生成getter 方法 4. 自定义getter /setter 方法 , ...
- 关于UIGestureRecognizerState
UIGestureRecognizerState的定义如下 typedef enum { UIGestureRecognizerStatePossible, UIGestureRecognizerSt ...
- 探究Visual Studio生成的.vs文件夹内部结构和作用
https://shiyousan.com/post/636441130259624698 在某个契机的引发下,对VS解决方案中自动生成的.vs文件夹产生了兴趣,以前总对这个文件夹不怎么上心,最近正好 ...
- VMware Playerでの仮想マシン起動エラー
Windows Updateすると.翌日VMware Playerの仮想マシン起動時に 「この仮想マシンを構成済み設定でパワーオンするのに十分な物理メモリがありません.」 のエラーとなることが時々あり ...
- MSF 内网渗透笔记
进入meterpreter模式 在meterpreter中输入shell即可进入CMD窗口接着即可执行CMD命令,例如打开RDP服务REG ADD HKLM\SYSTEM\CurrentControl ...
- C++ template —— 模板特化(五)
本篇讲解模板特化-------------------------------------------------------------------------------------------- ...
- 使用java连接数据库以后显示“ Establishing SSL connection without server's identity verification is not recommended”的警告如何解决
今天写了一段查询数据库的操作,如下 package MySQL; import java.sql.*; public class MySQL { //JDBC驱动名以及数据库URL static fi ...