[PHP]
/**
* Image Verification Code class
* Generate image Type Verification code, verification code contains numbers and uppercase letters, the session is stored MD5 encrypted code
*
* How to use:
* $captcha = new Catpcha ();
* $captcha->buildandexportimage ();
*
* Author: luojing
* Created time: 2013-3-27 11:42:12
*/
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//session
private $captcha;//Verification Code string
Const CHARWIDTH = 10;//A single character width, depending on the output character size
/**
* Create verification code class, initialize related parameters
* @param $width Picture width
* @param $height Picture height
* @param $codeNum number of verification code characters
* @param the name saved in $sessionKey session
*/
function __construct ($width = $height, $codeNum = 4, $sessionKey = ' captcha ') {
$this->width = $width;
$this->height = $height;
$this->codenum = $codeNum;
$this->sessionkey = $sessionKey;
Guaranteed minimum height and width
if ($height < 20) {
$this->height = 20;
}
if ($width < ($codeNum * self::charwidth + 10)) {//left 5 pixels gap
$this->width = $codeNum * self::charwidth + 10;
}
}
/**
* Construct and output verification code picture
*/
Public Function Buildandexportimage () {
$this->createimage ();
$this->setdisturb ();
$this->setcaptcha ();
$this->exportimage ();
}
/**
* Construct the image, set the background
*/
Private Function CreateImage () {
Create an image
$this->image = Imagecreatetruecolor ($this->width, $this->height);
Create a background color
$BG = Imagecolorallocate ($this->image, Mt_rand (255), Mt_rand (up, 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 < $i + +) {
$color = Imagecolorallocate ($this->image, Mt_rand (Mt_rand), Mt_rand (150, 200));
Imagesetpixel ($this->image, Mt_rand (5, $this->width-10), Mt_rand (5, $this->height-3), $color);
}
Set up interference lines
for ($i = 0; $i < $i + +) {
$color = Imagecolorallocate ($this->image, Mt_rand (Mt_rand), Mt_rand (150, 220));
Imagearc ($this->image, Mt_rand ( -10, $this->width), Mt_rand ( -10, $this->height), Mt_rand (+), Mt_rand (20 , (), $color);
}
Create a border color
$border = Imagecolorallocate ($this->image, mt_rand (0, 0), Mt_rand (0, 50), and Mt_rand (,));
Draw Border
Imagerectangle ($this->image, 0, 0, $this->width-1, $this->height-1, $border);
}
/**
* Generate and draw verification code
*/
Private Function Setcaptcha () {
$str = ' 23456789ABCDEFGHJKLMNPQRSTUVWXYZ ';
Generate CAPTCHA Characters
for ($i = 0; $i < $this->codenum; $i + +) {
$this->captcha. = $str {mt_rand (0, strlen ($str)-1)};
}
Draw a verification code
for ($i = 0; $i < strlen ($this->captcha); $i + +) {
$color = Imagecolorallocate ($this->image, Mt_rand (0, 200), Mt_rand (0, $), Mt_rand (0,));
$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);
}
}
/*
* Output image, verification code saved to 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!");
}
Save verification Code information to session, MD5 encryption
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);
}
}
/**
* Image Verification Code class
* Generate image Type Verification code, verification code contains numbers and uppercase letters, the session is stored MD5 encrypted code
*
* How to use:
* $captcha = new Catpcha ();
* $captcha->buildandexportimage ();
*
* Author: luojing
* Created time: 2013-3-27 11:42:12
*/
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//session
private $captcha;//Verification Code string
Const CHARWIDTH = 10;//A single character width, depending on the output character size
/**
* Create verification code class, initialize related parameters
* @param $width Picture width
* @param $height Picture height
* @param $codeNum number of verification code characters
* @param the name saved in $sessionKey session
*/
function __construct ($width = $height, $codeNum = 4, $sessionKey = ' captcha ') {
$this->width = $width;
$this->height = $height;
$this->codenum = $codeNum;
$this->sessionkey = $sessionKey;
Guaranteed minimum height and width
if ($height < 20) {
$this->height = 20;
}
if ($width < ($codeNum * self::charwidth + 10)) {//left 5 pixels gap
$this->width = $codeNum * self::charwidth + 10;
}
}
/**
* Construct and output verification code picture
*/
Public Function Buildandexportimage () {
$this->createimage ();
$this->setdisturb ();
$this->setcaptcha ();
$this->exportimage ();
}
/**
* Construct the image, set the background
*/
Private Function CreateImage () {
Create an image
$this->image = Imagecreatetruecolor ($this->width, $this->height);
Create a background color
$BG = Imagecolorallocate ($this->image, Mt_rand (255), Mt_rand (up, 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 < $i + +) {
$color = Imagecolorallocate ($this->image, Mt_rand (Mt_rand), Mt_rand (150, 200));
Imagesetpixel ($this->image, Mt_rand (5, $this->width-10), Mt_rand (5, $this->height-3), $color);
}
Set up interference lines
for ($i = 0; $i < $i + +) {
$color = Imagecolorallocate ($this->image, Mt_rand (Mt_rand), Mt_rand (150, 220));
Imagearc ($this->image, Mt_rand ( -10, $this->width), Mt_rand ( -10, $this->height), Mt_rand (+), Mt_rand (20 , (), $color);
}
Create a border color
$border = Imagecolorallocate ($this->image, mt_rand (0, 0), Mt_rand (0, 50), and Mt_rand (,));
Draw Border
Imagerectangle ($this->image, 0, 0, $this->width-1, $this->height-1, $border);
}
/**
* Generate and draw verification code
*/
Private Function Setcaptcha () {
$str = ' 23456789ABCDEFGHJKLMNPQRSTUVWXYZ ';
Generate CAPTCHA Characters
for ($i = 0; $i < $this->codenum; $i + +) {
$this->captcha. = $str {mt_rand (0, strlen ($str)-1)};
}
Draw a verification code
for ($i = 0; $i < strlen ($this->captcha); $i + +) {
$color = Imagecolorallocate ($this->image, Mt_rand (0, 200), Mt_rand (0, $), Mt_rand (0,));
$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);
}
}
/*
* Output image, verification code saved to 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!");
}
Save verification Code information to session, MD5 encryption
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);
}
}
http://www.bkjia.com/PHPjc/477617.html www.bkjia.com true http://www.bkjia.com/PHPjc/477617.html techarticle [PHP]? PHP/** * Image Verification Code class * Generate image Type Verification code, verification code contains numbers and uppercase letters, the session is stored MD5 encrypted code * * Use: * $captcha = ...