Home >Backend Development >PHP Tutorial >PHP verification code class ValidateCode
This article mainly analyzes the PHP verification code class ValidateCode in detail, which has certain reference value. Interested friends can refer to it
PHP parsing verification code class
1. Start
I saw the ValidateCode written in PHP on the Internet to generate a verification code class. It felt good, so I used it to analyze and learn.
2. Class diagram
3. Verification code class part code
3.1 Define variables
//随机因子 private $charset = 'abcdefghjkmnprstuvwxyzABCDEFGJKMNPRSTUVWXYZ23456789'; private $code; private $codeLen = 4; private $width = 130; private $heigh = 50; private $img;//图像 private $font;//字体 private $fontsize = 20;
$charset is a random factor. Here, several characters that are difficult to distinguish are removed, such as the letters "i,l,o,q" ", the number "0,1". If necessary, you can add some Chinese or other characters or calculations, etc.
$codeLen indicates the length of the verification code, usually 4 digits.
3.2 Constructor, set the verification code font, generate a true color image img
public function __construct() { $this->font = ROOT_PATH.'/font/Chowderhead.ttf'; $this->img = imagecreatetruecolor($this->width, $this->heigh); }
##3.3 From random factors Randomly select 4 characters as the $code verification code.
//生成随机码 private function createCode() { $_len = strlen($this->charset) - 1; for ($i = 0; $i < $this->codeLen; $i++) { $this->code .= $this->charset[mt_rand(0, $_len)]; } }
3.4 Generate verification code background color.
//生成背景 private function createBg() { $color = imagecolorallocate($this->img, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255)); imagefilledrectangle($this->img, 0, $this->heigh, $this->width, 0, $color); }The purpose of mt_rand(157, 255) is to randomly select a lighter color.
3.5 Generate text on the image.
//生成文字 private function createFont() { $_x = $this->width / $this->codeLen; $_y = $this->heigh / 2; for ($i = 0; $i < $this->codeLen; $i++) { $color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156)); imagettftext($this->img, $this->fontsize, mt_rand(-30, 30), $_x * $i + mt_rand(3, 5), $_y + mt_rand(2, 4), $color, $this->font, $this->code[$i]); } }Generate verification code text on the image, mainly considering the position of the text on the image and each A text color. Control the x-axis position of the nth text = (image width/verification code length) * (n-1) random offset number; where n = {d1....n}
Control the y-axis position of the nth text = image height/2 random offset number;mt_rand(0, 156) randomly selects the text color, 0-156 aims to select a darker color . mt_rand(-30, 30) Random text rotation.
3.6 Generate lines and snowflakes on the image
//生成线条,雪花 private function createLine() { for ($i = 0; $i < 15; $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->heigh), mt_rand(0, $this->width), mt_rand(0, $this->heigh), $color); } for ($i = 0; $i < 150; $i++) { $color = imagecolorallocate($this->img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255)); imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->heigh), '#', $color); } }When drawing lines, take a darker color value, and when drawing snowflakes The purpose of choosing a lighter color value is to not affect the human eye's recognition of the verification code as much as possible, and to interfere with the automatic recognition code mechanism.
3.7 Generate verification code images externally for external calls.
//对外生成 public function doImg() { $this->createBg(); //1.创建验证码背景 $this->createCode(); //2.生成随机码 $this->createLine(); //3.生成线条和雪花 $this->createFont(); //4.生成文字 $this->outPut(); //5.输出验证码图像 }
3.8 Complete code:
img, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255)); imagefilledrectangle($this->img, 0, $this->heigh, $this->width, 0, $color); } //生成文字 private function createFont() { $_x = $this->width / $this->codeLen; $_y = $this->heigh / 2; for ($i = 0; $i < $this->codeLen; $i++) { $color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156)); imagettftext($this->img, $this->fontsize, mt_rand(-30, 30), $_x * $i + mt_rand(3, 5), $_y + mt_rand(2, 4), $color, $this->font, $this->code[$i]); } } //生成线条,雪花 private function createLine() { for ($i = 0; $i < 15; $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->heigh), mt_rand(0, $this->width), mt_rand(0, $this->heigh), $color); } for ($i = 0; $i < 150; $i++) { $color = imagecolorallocate($this->img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255)); imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->heigh), '#', $color); } } //输出图像 private function outPut() { header('Content-Type: image/png'); imagepng($this->img); imagedestroy($this->img); } //对外生成 public function doImg() { $this->createBg(); //1.创建验证码背景 $this->createCode(); //2.生成随机码 $this->createLine(); //3.生成线条和雪花 $this->createFont(); //4.生成文字 $this->outPut(); //5.输出验证码图像 } //获取验证码 public function getCode() { return strtolower($this->code); } }
4. Test
<?php /** * Created by PhpStorm. * User: andy * Date: 16-12-22 * Time: 下午1:20 */ define('ROOT_PATH', dirname(__FILE__)); require_once ROOT_PATH.'/includes/ValidateCode.class.php'; $_vc=new ValidateCode(); echo $_vc->doImg();Generate verification code: ##5. Application
<label> <img src="../config/code.php" onclick="javascript:this.src='../config/code.php?tm='+Math.random();" /> </label>The onclick code above is to click on the verification code image, which can automatically refresh the verification code.
code.php:
<?php /** * Created by PhpStorm. * User: andy * Date: 16-12-22 * Time: 下午3:43 */ require substr(dirname(__FILE__),0,-7).'/init.inc.php'; $_vc=new ValidateCode(); echo $_vc->doImg(); $_SESSION['ValidateCode']=$_vc->getCode();6. Summary
During the independent testing process, no problems were found; but when applied to the project At the time, I first discovered that the verification code image could not be generated. I searched online and some said it was in the outPut() function,
In the header('Content-Type: image/png'); line of code A line of ob_clean() code was added earlier to solve the verification code problem. Although this method is simple, it may cause other problems with buffered data because the db_clean() function discards the contents of the output buffer.
Related recommendations:
The above is the detailed content of PHP verification code class ValidateCode. For more information, please follow other related articles on the PHP Chinese website!