For custom verification code classes!
<?php
/*
* To-Change the license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
Class image{
Private $img;
Public $width = 85;
Public $height = 25;
Public $code;
Public $code _len = 4;
Public $code _str = "329832983DSDSKDSLKQWEWQ2LKFDSFSDJFDSFDSJWLKFJ93290KFDSKJFDSOIDSLK";
Public $BG _color = ' #DCDCDC ';
Public $font _size = 16;
Public $font = ' Font.ttf ';
Public $font _color = ' #000000 ';
Create verification code hungry character creation
Public Function Create_code () {
$code = ";
for ($i =0; $i < $this->code_len; $i +) {
$code. = $this->code_str[mt_rand (0, strlen ($this->code_str)-1)];
}
return $this->code = $code;
}
Output image
Public Function GetImage () {
$w = $this->width;
$h = $this->height;
$BG _color = $this->bg_color;
$img = Imagecreatetruecolor ($w, $h);
$BG _color = imagecolorallocate ($img,
Hexdec (substr ($BG _color)), Hexdec (substr ($BG _color, 3,2)), Hexdec (substr ($BG _color, 5,2));
Imagefill ($img, 0, 0, $bg _color);
$this->img = $img;
$this->create_font ();
$this->create_pix ();
$this->show_code ();
}
Write Verification Code
Public Function Create_font () {
$this->create_code ();
$color = $this->font_color;
$font _color = imagecolorallocate ($this->img, Hexdec (substr ($color, up)), Hexdec (substr ($color, 3,2)), Hexdec ( substr ($color, 5,2)));
$x = $this->width/$this->code_len;
for ($i =0; $i < $this->code_len; $i +) {
$txt _color = imagecolorallocate ($this->img, Mt_rand (0,100), Mt_rand (0, N), Mt_rand (0, 200));
Imagettftext ($this->img, $this->font_size, Mt_rand ( -30,), $x * $i +mt_rand (3, 6), Mt_rand ($this->height/1.2 , $this->height), $txt _color, $this->font, $this->code[$i]);
Imagestring ($this->img, $this->font_size, $x * $i +mt_rand (3, 6), Mt_rand (0, $this->HEIGHT/4), $this- code[$i], $font _color);
}
$this->font_color = $font _color;
}
Draw interference Lines
Public Function Create_pix () {
$pix _color= $this->font_color;
for ($i =0; $i <100; $i + +) {
Imagesetpixel ($this->img, Mt_rand (0, $this->width), Mt_rand (0, $this->height), $pix _color);
}
for ($j =0; $j <4; $j + +) {
Imagesetthickness ($this->img, Mt_rand (1, 2));
Imageline ($this->img, Mt_rand (0, $this->width), Mt_rand (0, $this->height), Mt_rand (0, $this->width), Mt_ Rand (0, $this->height), $pix _color);
}
}
Get Verification Code
Public Function GetCode () {
Return Strtoupper ($this->code);
}
Output Verification Code
Private Function Show_code () {
Header ("Content-type:image/png");
Imagepng ($this->img);
Imagedestroy ($this->img);
}
}
Let's look at one more example of the code:
Generate the picture type verification code, the verification code contains the number and the capital letter, the session holds the MD5 encrypted verification code
<?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);
}
}
Example three:
<?php
Class Validationcode {
Private $width;
Private $height;
Private $codeNum;
Private $image; Image Resources
Private $disturbColorNum;
Private $checkCode;
function __construct ($width =80, $height =20, $codeNum =4) {
$this->width= $width;
$this->height= $height;
$this->codenum= $codeNum;
$this->checkcode= $this->createcheckcode ();
$number =floor ($width * $height/15);
if ($number > 240-$codeNum) {
$this->disturbcolornum= 240-$codeNum;
}else{
$this->disturbcolornum= $number;
}
}
Output an image to the browser by accessing the method
function ShowImage ($fontFace = "") {
First step: Create an image background
$this->createimage ();
Step two: Set the interference element
$this->setdisturbcolor ();
Step three: Randomly draw text into an image
$this->outputtext ($fontFace);
Fourth Step: Output image
$this->outputimage ();
}
Get a randomly created captcha string by calling this method
function Getcheckcode () {
return $this->checkcode;
}
Private Function CreateImage () {
Create an image resource
$this->image=imagecreatetruecolor ($this->width, $this->height);
Random background color
$backColor =imagecolorallocate ($this->image, rand (225, 255), Rand (225,255), rand (225, 255));
Fills the background with color
Imagefill ($this->image, 0, 0, $backColor);
Set Border color
$border =imagecolorallocate ($this->image, 0, 0, 0);
Draw a rectangular border
Imagerectangle ($this->image, 0, 0, $this->width-1, $this->height-1, $border);
}
Private Function Setdisturbcolor () {
for ($i =0; $i < $this->disturbcolornum; $i +) {
$color =imagecolorallocate ($this->image, rand (0, 255), rand (0, 255), rand (0, 255));
Imagesetpixel ($this->image, rand (1, $this->width-2), rand (1, $this->height-2), $color);
}
for ($i =0; $i <10; $i + +) {
$color =imagecolorallocate ($this->image, rand (255), Rand (255), rand (200, 255));
Imagearc ($this->image, rand ( -10, $this->width), rand ( -10, $this->height), rand (+), rand (20, 200), 55, 44, $color);
}
}
PHP-authored image verification code class file sharing method