php;">
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,$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),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 == tru
E) {
$this->setNoisePix(
);
}
if ($this->showNoiseLine == tru
E) {
$this->setNoiseLine(
);
}
if ($this->showBorder == tru
E) {
$this->borderColor = $this->getColor($this->borderColor
);
imageRectangle($this->image,$this->width - 1,$this->height - 1,$this->borderColor
);
}
$this->CI->session->set_userdata('auth_code',$cod
E);
ob_clean(
);
header("Content-type: image/jpeg"
);
imagejpeg($this->imag
E);
imagedestroy($this->imag
E);
}
/
**
* 显示验证码的JS调用
*
*/
function showScript()
{
//显示验证码
echo "var img_src = '/imgauthcode/show/?';\n";
echo "document.writeln('
CI->session->userdata('auth_code') && $auth_cod
E) ? ($this->CI->session->userdata('auth_code') === $auth_cod
E) :
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,255),255)
);
imageSetPixel($this->image,$this->width),$this->height),$randColor
);
}
}
function setNoiseLine()
{
for($i = 0; $i < $this->noiseNumLine; $i ++)
{
$randColor = imageColorAllocate($this->image,255)
);
imageline($this->image,rand(1,$randColor
);
}
}
}
OK. 一切结束,终于正常运行了。