CodeIgniter框架验证码类库文件与用法示例
<?php
class Authcode
{
var $CI;
var $fontPath;//字体路径
var $image;
var $charLen = 4; //生成几位验证码
var $arrChr = array();//验证码字符
var $width = 83; //图片宽
var $height = 24; //图片高
var $bgcolor = "#ffffff"; //背景色
var $showNoisePix = true; //生成杂点
var $noiseNumPix = 80; //生成杂点数量
var $showNoiseLine = true; //生成杂线
var $noiseNumLine = 2; //生成杂线数量
var $showBorder = true; //边框,当杂点、线一起作用的时候,边框容易受干扰
var $borderColor = "#000000";
function Authcode()
{
$this->CI = & get_instance();
$this->fontPath = realpath(dirname(__FILE__) . '/fonts/'); //字体文件
//$this->arrChr = array_merge(range(1, 9) , range('A', 'Z'));//数字字母验证码
//$this->arrChr = range('A', 'Z');//纯字母验证码
$this->arrChr = range(0, 9);//纯数字验证码
}
/**
* 显示验证码
*
*/
function show()
{
$this->image = imageCreate($this->width, $this->height);
$this->back = $this->getColor($this->bgcolor);
imageFilledRectangle($this->image, 0, 0, $this->width, $this->height, $this->back);
$size = $this->width / $this->charLen - 4;
if ($size > $this->height) {
$size = $this->height;
}
$left = ($this->width - $this->charLen * ($size + $size / 10)) / $size + 5;
$code = '';
for($i = 0; $i < $this->charLen; $i ++) {
$randKey = rand(0, count($this->arrChr) - 1);
$randText = $this->arrChr[$randKey];
$code .= $randText;
$textColor = imageColorAllocate($this->image, rand(0, 100), rand(0, 100), rand(0, 100));
$font = $this->fontPath . '/' . rand(1, 5) . ".ttf";
$randsize = rand($size - $size / 10, $size + $size / 10);
$location = $left + ($i * $size + $size / 10);
@imagettftext($this->image, $randsize, rand(- 18, 18), $location, rand($size - $size / 10, $size + $size / 10) + 2, $textColor, $font, $randText);
}
if ($this->showNoisePix == true) {
$this->setNoisePix();
}
if ($this->showNoiseLine == true) {
$this->setNoiseLine();
}
if ($this->showBorder == true) {
$this->borderColor = $this->getColor($this->borderColor);
imageRectangle($this->image, 0, 0, $this->width - 1, $this->height - 1, $this->borderColor);
}
$this->CI->session->set_userdata('auth_code', $code);
ob_clean();
header("Content-type: image/jpeg");
imagejpeg($this->image);
imagedestroy($this->image);
}
/**
* 显示验证码的JS调用
*
*/
function showScript()
{
//显示验证码
echo "var img_src = '/imgauthcode/show/?';
";
echo "document.writeln('');";
}
/**
* 检查验证码是否正确
*
* @param string $auth_code
* @return bool
*/
function check($auth_code = null)
{
return ($this->CI->session->userdata('auth_code') && $auth_code) ? ($this->CI->session->userdata('auth_code') === $auth_code) : false;
}
function getColor($color)
{
$color = eregi_replace("^#", "", $color);
$r = $color[0] . $color[1];
$r = hexdec($r);
$b = $color[2] . $color[3];
$b = hexdec($b);
$g = $color[4] . $color[5];
$g = hexdec($g);
$color = imagecolorallocate($this->image, $r, $b, $g);
return $color;
}
function setNoisePix()
{
for($i = 0; $i < $this->noiseNumPix; $i ++) {
$randColor = imageColorAllocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
imageSetPixel($this->image, rand(0, $this->width), rand(0, $this->height), $randColor);
}
}
function setNoiseLine()
{
for($i = 0; $i < $this->noiseNumLine; $i ++) {
$randColor = imageColorAllocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
imageline($this->image, rand(1, $this->width), rand(1, $this->height), rand(1, $this->width), rand(1, $this->height), $randColor);
}
}
}
- .NET Core系列之MemoryCache 初识
- 007手机一键Root(安机网一键Root) v3.0 官方最新版 一键ROOT您的Android手机
- 12306密码被盗了怎么办?12306密码外泄解决方法
- 12个字的qq网名
- 150M迷你型无线路由器怎么设置?
- 192.168.1.1打不开怎么办?路由器192.168.1.1打不开的原因以及解决办法
- 2011年电子报合订本 电子报 编辑部 中文 PDF版 [84M]
- 2015年1月15日小米新旗舰发布会现场图文直播
- 2016.3.1vivo Xplay5新品发布会现场视频直播 优酷直播
- 2016华为P9发布会视频直播地址 4月15日华为P9国行发布会直播
相关文章
- vscode有哪些插件好用? vscode部分插件的使用方法
- VerifyCodeServlet(一次性验证码)
- codeblocks安装及使用超详细图文教程
- VistaCodecs解码器 v6.6.5.0 (通杀一切音频视频 万能视频解码器)
- Universal Encoder Decoder(通用编码器解码器) v2.0 免费绿色版
- Win7Codecs 解码包 64位 v11.2.7 Final 中文安装版
- real解码器real decoderg v2014.07 最新免费版
- CodeIgniter框架验证码类库文件与用法示例
- ae粒子插件 trapcode particular v2.0 原版破解+中英双语版
- Canopus ProCoder 2(视频编码转换软件) v2.04.02 汉化安装版