- /**
- * Universal Verification Code Class img.php
- * Version: V0.1
- * bbs.it-home.org 2013/3/1
- */
- Class Validatecode {
- Private $charset = "abcdefghizklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; Random factor
- Private $code; Verification Code Text
- Private $codelen = 4; Verification code displays several text
- Private $width = 130; Verification Code width
- Private $height = 50; Verification Code Height
- Private $img; Validation code resource handle
- Private $font; The specified font
- Private $fontsize = 20; The specified font size
- Private $fontcolor; Font Color random
- Construct a class to write fonts
- Public Function __construct () {
- $this->font=root_path. ' /font/elephant.ttf ';
- }
- Create 4 random Codes
- Private Function Createcode () {
- $_leng=strlen ($this->charset);
- for ($i =1; $i <= $this->codelen; $i +) {
- $this->code.= $this->charset[mt_rand (0,$_leng)];
- }
- return $this->code;
- }
- Create a background
- Private Function Createbg () {
- Create a canvas for a resource jubing
- $this->img=imagecreatetruecolor ($this->width, $this->height);
- Background color
- $color =imagecolorallocate ($this->img,mt_rand (157,255), Mt_rand (157,255), Mt_rand (157,255));
- Draw a rectangle
- Imagefilledrectangle ($this->img,0, $this->height, $this->width,0, $color);
- }
- Creating fonts
- Private Function CreateFont () {
- $_x= ($this->width/$this->codelen); Font length
- for ($i =0; $i < $this->codelen; $i +) {
- Text color
- $color =imagecolorallocate ($this->img,mt_rand (0,156), Mt_rand (0,156), Mt_rand (0,156));
- Resource handle font size tilt font length font height font Color font specific text
- Imagettftext ($this->img, $this->fontsize,mt_rand ( -30,30), $_x* $i +mt_rand (1,5), $this->height/1.4, $color, $this->font, $this->code[$i]);
- }
- }
- Random lines
- Private Function Createline () {
- Random lines
- for ($i =0; $i <6; $i + +) {
- $color = Imagecolorallocate ($this->img,mt_rand (0,156), Mt_rand (0,156), Mt_rand (0,156));
- Imageline ($this->img,mt_rand (0, $this->width), Mt_rand (0, $this->height), Mt_rand (0, $this->width), Mt_ Rand (0, $this->height), $color);
- }
- Random Snowflakes
- for ($i =0; $i <45; $i + +) {
- $color = Imagecolorallocate ($this->img,mt_rand (220,255), Mt_rand (220,255), Mt_rand (220,255));
- Imagestring ($this->img,mt_rand (1,5), Mt_rand (0, $this->width), Mt_rand (0, $this->height), ' * ', $color);
- }
- }
- Output background
- Private Function OutPut () {
- Generate headers
- Header (' contenttype:img/png ');
- Output picture
- Imagepng ($this->img);
- Destroying result sets
- Imagedestroy ($this->img);
- }
- External output
- Public Function doimg () {
- Load background
- $this->CREATEBG ();
- Loading files
- $this->createcode ();
- Loading lines
- $this->createline ();
- Loading fonts
- $this->createfont ();
- Load background
- $this->output ();
- }
- Get Verification Code
- Public Function GetCode () {
- Return Strtolower ($this->code);
- }
- }
- ?>
Copy CodeInvocation Example: index.php for you to recommend a few articles about PHP verification code: PHP Random Verification code PHP generated random verification Code (graphics) with a snowflake background verification code PHP write a verification code PHP to generate a code of dynamic picture Verification code |