[php]
/**
* Picture verification code class
* Generate image type verification code. The verification code contains numbers and capital letters. The md5 encrypted verification code is stored in the session
*
* How to use:
* $captcha = new Catpcha();
* $captcha->buildAndExportImage();
*
*Author: luojing
*Creation time: 2013-3-27 11:42:12 am
*/
class Captcha {
Private $width;//Width
Private $height; //Height
Private $codeNum;//Number of verification code characters
Private $image;//Verification code image resource
Private $sessionKey;//The name saved in the session
Private $captcha;//Verification code string
const charWidth = 10;//Single character width, changes according to the output character size
/**
* Create a verification code class and initialize related parameters
* @param $width image width
* @param $height Image height
* @param $codeNum The number of characters in the verification code
* @param $sessionKey The name saved in session
*/
Function __construct($width = 50, $height = 20, $codeNum = 4, $sessionKey = 'captcha') {
$this->width = $width;
$this->height = $height;
$this->codeNum = $codeNum;
$this->sessionKey = $sessionKey;
//Guarantee minimum height and width
If($height < 20) {
$this->height = 20;
}
If($width < ($codeNum * self::charWidth + 10)) {//Reserve 5 pixel gaps on the left and right
$this->width = $codeNum * self::charWidth + 10;
}
}
/**
* Construct and output the verification code image
*/
Public function buildAndExportImage() {
$this->createImage();
$this->setDisturb();
$this->setCaptcha();
$this->exportImage();
}
/**
* Construct the image and set the background color
*/
Private function createImage() {
//Create image
$this->image = imagecreatetruecolor($this->width, $this->height);
//Create background color
$bg = imagecolorallocate($this->image, mt_rand(220, 255), mt_rand(220, 255), mt_rand(220, 255));
//Fill background color
imagefilledrectangle($this->image, 0, 0, $this->width - 1, $this->height - 1, $bg);
}
/**
*Set interference elements
*/
Private function setDisturb() {
//Set interference points
for($i = 0; $i < 150; $i++) {
$color = imagecolorallocate($this->image, mt_rand(150, 200), mt_rand(150, 200), mt_rand(150, 200));
imagesetpixel($this->image, mt_rand(5, $this->width - 10), mt_rand(5, $this->height - 3), $color);
}
//设置干扰线
for($i = 0; $i < 10; $i++) {
$color = imagecolorallocate($this->image, mt_rand(150, 220), mt_rand(150, 220), mt_rand(150, 220));
imagearc($this->image, mt_rand(-10, $this->width), mt_rand(-10, $this->height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $color);
}
//创建边框色
$border = imagecolorallocate($this->image, mt_rand(0, 50), mt_rand(0, 50), mt_rand(0, 50));
//画边框
imagerectangle($this->image, 0, 0, $this->width - 1, $this->height - 1, $border);
}
/**
* Generate and draw verification code
*/
private function setCaptcha() {
$str = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ';
//生成验证码字符
for($i = 0; $i < $this->codeNum; $i++) {
$this->captcha .= $str{mt_rand(0, strlen($str) - 1)};
}
//绘制验证码
for($i = 0; $i < strlen($this->captcha); $i++) {
$color = imagecolorallocate($this->image, mt_rand(0, 200), mt_rand(0, 200), mt_rand(0, 200));
$x = floor(($this->width - 10)/$this->codeNum);
$x = $x*$i + floor(($x-self::charWidth)/2) + 5;
$y = mt_rand(2, $this->height - 20);
imagechar($this->image, 5, $x, $y, $this->captcha{$i}, $color);
}
}
/*
* 输出图像,验证码保存到session中
*/
private function exportImage() {
if(imagetypes() & IMG_GIF){
header('Content-type:image/gif');
imagegif($this->image);
} else if(imagetypes() & IMG_PNG){
header('Content-type:image/png');
imagepng($this->iamge);
} else if(imagetypes() & IMG_JPEG) {
header('Content-type:image/jpeg');
imagepng($this->iamge);
} else {
imagedestroy($this->image);
die("Don't support image type!");
}
//将验证码信息保存到session中,md5加密
if(!isset($_SESSION)){
session_start();
}
$_SESSION[$this->sessionKey] = md5($this->captcha);
imagedestroy($this->image);
}
function __destruct() {
unset($this->width, $this->height, $this->codeNum,$this->captcha);
}
}
/**
* Picture verification code class
* Generate image type verification code. The verification code contains numbers and capital letters. The md5 encrypted verification code is stored in the session
*
* How to use:
* $captcha = new Catpcha();
* $captcha->buildAndExportImage();
*
*Author: luojing
*Creation time: 2013-3-27 11:42:12 am
*/
class Captcha {
private $width;//Width
private $height; //Height
private $codeNum;//Number of verification code characters
private $image;//Verification code image resource
private $sessionKey;//The name saved in the session
private $captcha;//Verification code string
const charWidth = 10;//The width of a single character changes according to the output character size
/**
* Create a verification code class and initialize related parameters
* @param $width image width
* @param $height Image height
* @param $codeNum The number of characters in the verification code
* @param $sessionKey The name saved in session
*/
function __construct($width = 50, $height = 20, $codeNum = 4, $sessionKey = 'captcha') {
$this->width = $width;
$this->height = $height;
$this->codeNum = $codeNum;
$this->sessionKey = $sessionKey;
//Guarantee minimum height and width
if($height < 20) {
$this->height = 20;
}
if($width < ($codeNum * self::charWidth + 10)) {//Reserve 5 pixel gaps on the left and right
$this->width = $codeNum * self::charWidth + 10;
}
}
/**
* Construct and output verification code image
*/
public function buildAndExportImage() {
$this->createImage();
$this->setDisturb();
$this->setCaptcha();
$this->exportImage();
}
/**
* Construct the image and set the background color
*/
private function createImage() {
//Create image
$this->image = imagecreatetruecolor($this->width, $this->height);
//Create background color
$bg = imagecolorallocate($this->image, mt_rand(220, 255), mt_rand(220, 255), mt_rand(220, 255));
//Fill background color
imagefilledrectangle($this->image, 0, 0, $this->width - 1, $this->height - 1, $bg);
}
/**
* Set disturbing elements
*/
private function setDisturb() {
//Set interference point
for($i = 0; $i < 150; $i++) {
$color = imagecolorallocate($this->image, mt_rand(150, 200), mt_rand(150, 200), mt_rand(150, 200));
imagesetpixel($this->image, mt_rand(5, $this->width - 10), mt_rand(5, $this->height - 3), $color);
}
//Set interference line
for($i = 0; $i < 10; $i++) {
$color = imagecolorallocate($this->image, mt_rand(150, 220), mt_rand(150, 220), mt_rand(150, 220));
imagearc($this->image, mt_rand(-10, $this->width), mt_rand(-10, $this->height), mt_rand(30, 300), mt_rand(20, 200), 55 , 44, $color);
}
//Create border color
$border = imagecolorallocate($this->image, mt_rand(0, 50), mt_rand(0, 50), mt_rand(0, 50));
//Draw a border
imagerectangle($this->image, 0, 0, $this->width - 1, $this->height - 1, $border);
}
/**
* Generate and draw verification code
*/
private function setCaptcha() {
$str = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ';
//Generate verification code characters
for($i = 0; $i < $this->codeNum; $i++) {
$this->captcha .= $str{mt_rand(0, strlen($str) - 1)};
}
//Draw verification code
for($i = 0; $i < strlen($this->captcha); $i++) {
$color = imagecolorallocate($this->image, mt_rand(0, 200), mt_rand(0, 200), mt_rand(0, 200));
$x = floor(($this->width - 10)/$this->codeNum);
$x = $x*$i + floor(($x-self::charWidth)/2) + 5;
$y = mt_rand(2, $this->height - 20);
imagechar($this->image, 5, $x, $y, $this->captcha{$i}, $color);
}
}
/*
* 输出图像,验证码保存到session中
*/
private function exportImage() {
if(imagetypes() & IMG_GIF){
header('Content-type:image/gif');
imagegif($this->image);
} else if(imagetypes() & IMG_PNG){
header('Content-type:image/png');
imagepng($this->iamge);
} else if(imagetypes() & IMG_JPEG) {
header('Content-type:image/jpeg');
imagepng($this->iamge);
} else {
imagedestroy($this->image);
die("Don't support image type!");
}
//将验证码信息保存到session中,md5加密
if(!isset($_SESSION)){
session_start();
}
$_SESSION[$this->sessionKey] = md5($this->captcha);
imagedestroy($this->image);
}
function __destruct() {
unset($this->width, $this->height, $this->codeNum,$this->captcha);
}
}