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 are a few that are not easy to distinguish. Characters, such as letters "i,l,o,q" and numbers "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, and 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 Randomly select 4 characters from the random factor 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 the background color of the verification code.
//生成背景 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); }
Among them mt_rand(157, 255), the purpose 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 on the image For text, the main consideration is the position of the text on the image and the color of each text.
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, Choose a darker color value, and choose a lighter color value when drawing snowflakes. The purpose 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
Test code:
<?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. Apply
##
<label> <img src="../config/code.php" onclick="javascript:this.src='../config/code.php?tm='+Math.random();" /> </label>
<?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
Generate pictures with PHPVerification codeFunction details
php implements websiteVerification codeFunction
php encapsulated Verification codeClass detailed explanation
The above is the detailed content of PHP verification code class ValidateCode analysis. For more information, please follow other related articles on the PHP Chinese website!