The example in this article describes the validation Code tool class for PHP encapsulation. Share to everyone for your reference, specific as follows:
<?php//Validation Code tool class captcha{//property private $width;
Private $height;
Private $fontsize;
Private $pixes;
Private $lines;
Private $str _len;
/* Construct method * @param1 Array $arr = Array (), the associated array of the initialization property/Public function __construct ($arr = Array ()) { Initialize $this->width = isset ($arr [' width '])?
$arr [' width ']: $GLOBALS [' config '] [' captcha '] [' width ']; $this->height = isset ($arr [' height '])?
$arr [' Height ']: $GLOBALS [' config '] [' captcha '] [' height ']; $this->fontsize = isset ($arr [' fontsize '])?
$arr [' FontSize ']: $GLOBALS [' config '] [' captcha '] [' fontsize ']; $this->pixes = isset ($arr [' pixes '])?
$arr [' pixes ']: $GLOBALS [' config '] [' captcha '] [' pixes ']; $this->lines = isset ($arr [' lines '])?
$arr [' Lines ']: $GLOBALS [' config '] [' captcha '] [' lines ']; $this->str_len = isset ($arr [' Str_len '])?
$arr [' Str_len ']: $GLOBALS [' config '] [' captcha '] [' str_len ']; * * * Generate CAPTCHA Picture/Public functionGenerate () {//make canvas $img = Imagecreatetruecolor ($this->width, $this->height);
Given the background color $BG _color = imagecolorallocate ($img, Mt_rand (200,255), Mt_rand (200,255), Mt_rand (200,255));
Imagefill ($img, 0,0, $BG _color);
Making interference line $this->getlines ($img);
Increase the jamming point $this->getpixels ($img);
Add Captcha $captcha = $this->getcaptcha ();
Text color $str _color = imagecolorallocate ($img, Mt_rand (0,100), Mt_rand (0,100), Mt_rand (0,100));
Write text//The starting position where the calculated text should appear $start _x = ceil ($this->width/2)-25;
$start _y = ceil ($this->height/2)-8; if (imagestring ($img, $this->fontsize, $start _x, $start _y, $captcha, $str _color)) {//success: Output Verification Code header (' Content
-type:image/png ');
Imagepng ($IMG);
}else{//Failed return false; * * Get Authenticode Random String * @return string $captcha, random captcha text/Private Function Getcaptcha () {// Gets a random string $str = Implode (", Array_merge (Range (' A ', ' Z '), Range (' A ', ' Z '), Range (1,9)); Randomly taking $captcha = '; Save the random string for ($i = 0, $len = strlen ($str), $i < $this->str_len; $i + +) {//each random character $captcha. = $s Tr[mt_rand (0, $len-1)].
' ';
//Save data to session $_session[' captcha ' = Str_replace (', ', $captcha);
Returns the return value $captcha; * * * Add interference point * @param1 resource $img/Private Function Getpixels ($img) {//Add interference point for ($i = 0 $i < $this->pixes; $i + +) {//Assign color $pixel _color = imagecolorallocate ($img, Mt_rand (100,150), Mt_rand
(100,150), Mt_rand (100,150));
Draw Point Imagesetpixel ($img, Mt_rand (0, $this->width), Mt_rand (0, $this->height), $pixel _color); * * Increase the Interference line * @param1 resource $img to increase the image resource of the interference line/Private function Getlines ($img) {//Increase Add interference line for ($i = 0; $i < $this->lines; $i + +) {//Assign color $line _color = imagecolorallocate ($img, Mt_Rand (150,200), Mt_rand (150,200), Mt_rand (150,200)); Draw Line Imageline ($img, Mt_rand (0, $this->width), Mt_rand (0, $this->height), Mt_rand (0, $this->width), Mt_rand (0
, $this->height), $line _color); }/* Verify code * @param1 string $captcha, user-submitted authentication code * @return BOOL, successful return true, failure return false * * * Publ IC static function Checkcaptcha ($captcha) {//Authenticode case-insensitive return (Strtolower ($captcha) = = Strtolower ($_session['
Captcha ']));
}
}
For more information on PHP related content readers can view the site topics: "PHP graphics and pictures Operating skills summary", "Basic PHP Grammar Introductory Course", "PHP Operations and Operator Usage Summary", "PHP object-oriented Program Design Introductory Course", "PHP Network Programming Skills Summary", "PHP array (Array) Operations tips Encyclopedia, "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP Common database Operation tips Summary"
I hope this article will help you with the PHP program design.