ci验证码类库
2015-11-24 14:49
447 查看
由于CI自带的验证码会自动生成文件到本地,这样太占空间
<?php
class Captcha
{
private $width;
private $height;
private $codeNum;
private $code;
private $im;
function __construct($width=80, $height=30, $codeNum=4){
$this->width = $width;
$this->height = $height;
$this->codeNum = $codeNum;
}
function showImg(){
//创建图片
$this->createImg();
//设置干扰元素
$this->setDisturb();
//设置验证码
$this->setCaptcha();
//输出图片
$this->outputImg();
}
function getCaptcha(){
$this->createCode();
return $this->code;
}
private function createImg(){
$this->im = imagecreatetruecolor($this->width, $this->height);
$bgColor = imagecolorallocate($this->im, 0, 0, 0);
imagefill($this->im, 0, 0, $bgColor);
}
private function setDisturb(){
$area = ($this->width * $this->height) / 20;
$disturbNum = ($area > 250) ? 250 : $area;
//加入点干扰
for ($i = 0; $i < $disturbNum; $i++) {
$color = imagecolorallocate($this->im, rand(0, 255), rand(0, 255), rand(0, 255));
imagesetpixel($this->im, rand(1, $this->width - 2), rand(1, $this->height - 2), $color);
}
//加入弧线
for ($i = 0; $i <= 5; $i++) {
$color = imagecolorallocate($this->im, rand(128, 255), rand(125, 255), rand(100, 255));
imagearc($this->im, rand(0, $this->width), rand(0, $this->height), rand(30, 300), rand(20, 200), 50, 30, $color);
}
}
private function createCode(){
$str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ";
for ($i = 0; $i < $this->codeNum; $i++) {
$this->code .= $str{rand(0, strlen($str) - 1)};
}
}
private function setCaptcha(){
for ($i = 0; $i < $this->codeNum; $i++) {
$color = imagecolorallocate($this->im, rand(50, 250), rand(100, 250), rand(128, 250));
$size = rand(floor($this->height / 5), floor($this->height / 3));
$x = floor($this->width / $this->codeNum) * $i + 5;
$y = rand(0, $this->height - 20);
imagechar($this->im, $size, $x, $y, $this->code{$i}, $color);
}
}
private function outputImg(){
if (imagetypes() & IMG_JPG) {
header('Content-type:image/jpeg');
imagejpeg($this->im);
} elseif (imagetypes() & IMG_GIF) {
header('Content-type: image/gif');
imagegif($this->im);
} elseif (imagetype() & IMG_PNG) {
header('Content-type: image/png');
imagepng($this->im);
} else {
die("Don't support image type!");
}
}
}
控制器调用:
public function get_code(){
$this->load->library('captcha');
$code = $this->captcha->getCaptcha();
$this->session->set_userdata('code', $code);
$this->captcha->showImg();
}
http://www.ifixedbug.com/posts/codeigniter-captcha-library
<?php
class Captcha
{
private $width;
private $height;
private $codeNum;
private $code;
private $im;
function __construct($width=80, $height=30, $codeNum=4){
$this->width = $width;
$this->height = $height;
$this->codeNum = $codeNum;
}
function showImg(){
//创建图片
$this->createImg();
//设置干扰元素
$this->setDisturb();
//设置验证码
$this->setCaptcha();
//输出图片
$this->outputImg();
}
function getCaptcha(){
$this->createCode();
return $this->code;
}
private function createImg(){
$this->im = imagecreatetruecolor($this->width, $this->height);
$bgColor = imagecolorallocate($this->im, 0, 0, 0);
imagefill($this->im, 0, 0, $bgColor);
}
private function setDisturb(){
$area = ($this->width * $this->height) / 20;
$disturbNum = ($area > 250) ? 250 : $area;
//加入点干扰
for ($i = 0; $i < $disturbNum; $i++) {
$color = imagecolorallocate($this->im, rand(0, 255), rand(0, 255), rand(0, 255));
imagesetpixel($this->im, rand(1, $this->width - 2), rand(1, $this->height - 2), $color);
}
//加入弧线
for ($i = 0; $i <= 5; $i++) {
$color = imagecolorallocate($this->im, rand(128, 255), rand(125, 255), rand(100, 255));
imagearc($this->im, rand(0, $this->width), rand(0, $this->height), rand(30, 300), rand(20, 200), 50, 30, $color);
}
}
private function createCode(){
$str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ";
for ($i = 0; $i < $this->codeNum; $i++) {
$this->code .= $str{rand(0, strlen($str) - 1)};
}
}
private function setCaptcha(){
for ($i = 0; $i < $this->codeNum; $i++) {
$color = imagecolorallocate($this->im, rand(50, 250), rand(100, 250), rand(128, 250));
$size = rand(floor($this->height / 5), floor($this->height / 3));
$x = floor($this->width / $this->codeNum) * $i + 5;
$y = rand(0, $this->height - 20);
imagechar($this->im, $size, $x, $y, $this->code{$i}, $color);
}
}
private function outputImg(){
if (imagetypes() & IMG_JPG) {
header('Content-type:image/jpeg');
imagejpeg($this->im);
} elseif (imagetypes() & IMG_GIF) {
header('Content-type: image/gif');
imagegif($this->im);
} elseif (imagetype() & IMG_PNG) {
header('Content-type: image/png');
imagepng($this->im);
} else {
die("Don't support image type!");
}
}
}
控制器调用:
public function get_code(){
$this->load->library('captcha');
$code = $this->captcha->getCaptcha();
$this->session->set_userdata('code', $code);
$this->captcha->showImg();
}
http://www.ifixedbug.com/posts/codeigniter-captcha-library
相关文章推荐
- 数据链路层—简介
- #Android#Eclipse Logcat无法打印信息处理方法
- 字符串匹配的Boyer-Moore算法
- jquery easyui的datagrid在初始化的时候会请求两次URL?
- 去掉navBar的线
- 《JavaScript高级程序设计》chapter 1: javascript 简介
- 需求文档可测试化
- tuple
- 深入了解require.js特性
- 在线桌面版本API,中文api
- tmux使用大全
- iOS开源程序下载网站
- Centos6.7 ELK日志系统部署
- CCBPM 常用API接口说明
- Win7+花生壳6.0+tomcat打做自己的web服务器(搭建自己的网站)(参考)
- Linux编程——makefile编写技巧大全
- spark 单机模式
- int -> String
- 该怎么实现iMindMap固定功能的巧妙运用
- 如何在一个<td>里放2个以上的<form>而不换行