|
楼主 |
发表于 2011-3-28 14:50:58
|
显示全部楼层
回复 4# Hex
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* 驗證碼生成
*author:haile
*/
class Verify {
//验证码位数
private $vCheckCodeNum = 4;
//产生的验证码
private $vCheckCode = '';
//验证码的图片
private $vCheckImage = '';
//干扰像素
private $vDisturbColor = '';
//验证码的图片宽度
private $vCheckImageWidth = '80';
//验证码的图片宽度
private $vCheckImageHeight = '20';
/**
*
* @brief 产生验证码
*
*/
private function createCheckCode() {
$this->vCheckCode = strtolower(substr(md5(rand()),0,$this->vCheckCodeNum));
return $this->vCheckCode;
}
/**
*
* @brief 产生验证码图片
*
*/
private function createVerifyImage() {
$this->vCheckImage = @imagecreate ($this->vCheckImageWidth,$this->vCheckImageHeight);
imagecolorallocate ($this->vCheckImage, 255, 255, 255);
return $this->vCheckImage;
}
/**
*
* @brief 设置图片的干扰像素
*
*/
private function setDisturbColor() {
for ($i=0;$i<=128;$i++) {
$this->vDisturbColor = imagecolorallocate ($this->vCheckImage,255, 255, 255);
imagesetpixel($this->vCheckImage,rand(2,128),rand(2,38),$this->vDisturbColor);
}
}
/**
*
* @brief 设置验证码图片的大小
*
* @param $width 宽
*
* @param $height 高
*
*/
public function setCheckImageWH($width,$height) {
if($width==''||$height=='')return false;
$this->vCheckImageWidth = $width;
$this->vCheckImageHeight = $height;
return true;
}
/**
*
* @brief 在验证码图片上逐个画上验证码
*
*/
private function writeCheckCodeToImage() {
for ($i=0;$i<$this->vCheckCodeNum;$i++) {
$bg_color = imagecolorallocate ($this->vCheckImage, rand(0,255), rand(0,128), rand(0,255));
$x = floor($this->vCheckImageWidth/$this->vCheckCodeNum)*$i+3;
$y = rand(0,$this->vCheckImageHeight-15);
imagechar ($this->vCheckImage, 5, $x, $y, $this->vCheckCode[$i], $bg_color);
}
}
/**
*
* @brief 输出验证码图片
*
*/
public function outCheckImage() {
$this ->createCheckCode();
$this ->createVerifyImage();
$this ->setDisturbColor();
$this ->writeCheckCodeToImage();
$this ->writeCheckCodeToSession();
header('Content-Type:image/jpeg');
imagejpeg($this->vCheckImage);
}
public function writeCheckCodeToSession()
{
$ci =& get_instance();
$ci->session->set_userdata('verifycode', $this->vCheckCode);//本想在控制器才写入session 但返回不了code 只好在这里 在输出图片之前写入session
}
public function code()
{
return $this->vCheckCode;//这个原是想 在控制器应用到session的 但没有效果 控制器里面得到是null,我只好增加 function writeCheckCodeToSession 在这个类里面
}
}
?> |
|