<?php
/*
系统验证码类, 可直接在模板标签中调用
提供了两套验证码(普通验证码和图片验证码)
*/
//defined('IN_SHOP') or die();
class Captcha {
protected $img; //验证码图片资源
protected $width; //图片宽度
protected $height; //图片高度
protected $img_jpg = array( //以图片为背景
1 =>'captcha_bg1.jpg',
2 =>'captcha_bg2.jpg',
3 =>'captcha_bg3.jpg',
4 =>'captcha_bg4.jpg',
5 =>'captcha_bg5.jpg',
);
protected $img_gif = array(
1 =>'captcha_bg1.jpg',
2 =>'captcha_bg2.jpg',
3 =>'captcha_bg3.jpg',
4 =>'captcha_bg4.jpg',
5 =>'captcha_bg5.jpg',
);
protected $path = 'tmp/captcha_img/'; //背景图片路径
protected $code_num = 5; //验证码个数
protected $distrub_num = 100; //干扰点个数
protected $code; //验证码
protected $type; //验证码类型
function __construct($type = '', $width = 145, $height = 20) {
$this->width = $width;
$this->height = $height;
$this->type = $type;
$this->code = $this->set_char();
$this->record_code();
//生成验证码
$this->captcha();
}
protected function captcha() {
//画图
$this->get_img();
//画干扰点
if ($this->type == '')
$this->get_point();
//形成验证码
$this->get_char();
//输出图片
$this->show_img();
}
//形成图像资源
protected function get_img() {
if ($this->type == '')
{
$this->img = imagecreatetruecolor($this->width, $this->height);
$bgcolor = imagecolorallocate($this->img, mt_rand(128, 255), mt_rand(128, 255), mt_rand(128, 255));
imagefill($this->img, 0, 0, $bgcolor);
}
elseif ($this->type == 'img')
{
if (!file_exists(ROOT_PATH . $this->path . $this->img_jpg[1]))
{
return false;
}
else
{
if ((function_exists('imagecreatefromjpeg') && (imagetypes() & IMG_JPEG)))
{
$theme = $this->img_jpg[mt_rand(1, count($this->img_jpg))];
$bgcolor = imagecreatefromjpeg(ROOT_PATH . $this->path . $theme);
}
else
{ $theme = $this->img_gif[mt_rand(1, count($this->img_jpg))];
$bgcolor = imagecreatefromgif(ROOT_PATH . $this->path . $theme);
}
}
$x = imagesx($bgcolor);
$y = imagesy($bgcolor);
$this->img = imagecreatetruecolor($x, $y);
(function_exists('imagecopyresampled')) ? imagecopyresampled($this->img, $bgcolor, 0, 0, 0, 0, $this->width, $this->height, $x, $y) : imagecopyresized($this->img, $bgcolor, 0, 0, 0, 0, $this->width, $this->height, $x, $y);
imagecolorallocate($this->img, 255, 255, 255);
imagedestroy($bgcolor);
}
}
//画干扰点
protected function get_point() {
for ($i = 0; $i < $this->distrub_num; $i++)
{
$bg_color = imagecolorallocate($this->img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagesetpixel($this->img, mt_rand(1, $this->width - 1), mt_rand(1, $this->height - 1), $bg_color);
}
}
//生成验证码字符串
protected function set_char() {
$char = '1234567890qwertyuiopasdfghjklmnbvcxzQWERTYUIOPLKJHGFDSAZXCVBNM';
$str = '';
for ($i = 0; $i < $this->code_num; $i++)
{
$str .= substr($char, rand(0, strlen($char) - 1), 1);
}
return $str;
}
//将验证码输出到图布
protected function get_char() {
$bgcolor = imagecolorallocate($this->img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
for ($i = 0; $i < $this->code_num; $i++)
{
imagechar($this->img, mt_rand(4, 6), ($this->width / $this->code_num) * $i + 7, mt_rand(0, ($this->height - 15)), substr($this->code, $i, 1), $bgcolor);
}
}
//输出图片
protected function show_img() {
header("Cache-Control: no-cache,no-store,must-revalidate");
header("pragma: no-cache");
if(imagetypes() & IMG_PNG)
{
header("Content-Type: image/png");
imagepng($this->img);
}
elseif (imagetypes() & IMG_GIF)
{
header("Content-Type: image/gif");
imagegif($this->img);
}
else
{
header("Content-Type: image/jpeg");
imagejpeg($this->img);
}
}
//记录验证码
protected function record_code() {
session_start();
$_SESSION['__CAPTCHA__'] = base64_encode($this->code);
}
function __destruct() {
imagedestroy($this->img);
}
}
?>