A useful PHP verification code class example sharing_PHP tutorial
WBOYOriginal
2016-07-13 17:18:51928browse
Share a useful PHP verification code class, including calling examples. Note: If the specified font is not applicable, then use the imagestring() function. If you need to encounter the specified font, you must use the imagettftext() function. The location of the font is Windows/Fonts.
under the C drive
Refer to the method of generating verification code in PHP on the Internet, as well as the method of generating PHP image verification code and PHP Chinese verification code. Used relevant knowledge of PHP GD library.
1, the class that generates the verification code VerificationCode.class.php
Copy the code The code is as follows:
class VerificationCode{ private $charset="abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789"; //Random factor private $code; //Verification code private $codelen=4 ; //Verification code length private $width=110; //Width private $height=30; //Height private $img; //Image resource handle private $font; / /Specify font private $fontSize=25; //Font size private $fontColor; //Font color public function __construct(){ $this->font="CALIBRIZ.TTF “; ; $i < $this->codelen; $i++) { } //Generate background private function createBg(){ $this->img=imagecreatetruecolor($this->width,$this->height); $color = imagecolorallocate($this->img,mt_rand(157,255),mt_rand(157,255),mt_rand(157,255)); ;width,0,$color); 🎜> for ($i = 0; $i < $this->codelen; $i++) { fontColor=imagecolorallocate($this->img,mt_rand(0,156),mt_rand( 0,156),mt_rand(0,156)); this->height/1.4,$this->fontColor,$this->font,$this->code[$i]); // www.jbxue.com // imagestring($this ->img,5,$i*$x+mt_rand(1,5),5,$this->code[$i],$this->fontColor); 🎜> //Generate lines and snowflakes private function createDisturb(){ for ($i = 0; $i < 6; $i++) { color $color=imageallocate($this-> img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156)); ;width),mt_rand(0,$this->width),mt_rand(0,$this->width),$color); for ($i = 0; $i < 100; $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->height),'*',$color); } } //输出 private function outPut(){ header("Content-Type:image/png"); imagepng($this->img); imagedestroy($this->img); } public function showCode(){ $this->createBg(); $this->createCode(); $this->createDisturb(); $this->createFont(); $this->outPut(); } //获取验证码 public function getCode(){ return strtolower($this->code); } } ?>
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn