How PHP implements code for random numbers and letters verification codes

Source: Internet
Author: User
PHP Implementation of random numbers, letters of the Verification Code

Customizable to generate CAPTCHA text size, number, interference items, etc., you can also customize the font of text validation ...

Nonsense not much to say, directly on the code:

<?phpclass captcha{private $_fontfile= ";        Private $_size=36;        Private $_width=200;        Private $_height=100;        Private $_length=4;        Private $_image=null;        Private $_snow=0;        Private $_pixel=0;    Private $_line=0; Public function __construct ($config =array ()) {if (Is_array ($config) &&count ($config) >0) {if (ISS ET ($config [' fontfile ']) &&is_file ($config [' fontfile ']) &&is_readable ($config [' fontfile '])) {$            this->_fontfile= $config [' Fontfile '];            }else{return false; if (Isset ($config [' size ']) && $config [' size ']>0) {$this->_size= (int) $config [' Size ']            ; if (Isset ($config [' width ']) && $config [' width ']>0) {$this->_width= (int) $config [' Wid            Th ']; if (Isset ($config [' height ']) && $config [' height ']>0) {$this->_height= (int)$config [' height ']; if (Isset ($config [' length ']) && $config [' length ']>0) {$this->_length= (int) $config ['            Length ']; if (Isset ($config [' Snow ']) && $config [' Snow ']>0) {$this->_snow= (int) $config [' Snow ']            ; } if (Isset ($config [' Pixel ']) && $config [' Pixel ']>0) {$this->_pixel= (int) $config [' Pix            El ']; if (Isset ($config [' line ')] && $config [' line ']>0) {$this->_line= (int) $config [' line ']            ;            } $this->_image=imagecreatetruecolor ($this->_width, $this->_height);         return $this->_image;        } else{return false;        }} Public Function Getcaptcha () {$white =imagecolorallocate ($this->_image,255,255,255);        Imagefilledrectangle ($this->_image,0,0, $this->_width, $this->_height, $white); $str = $this->_generatestr ($this->_length);        if (false=== $str) {return false;        } $fontfile = $this->_fontfile;            for ($i =0; $i < $this->_length; $i + +) {$size = $this->_size;            $angle =mt_rand ( -30,30);            $x =ceil ($this->_width/$this->_length) * $i +mt_rand (5,10);            $y =ceil ($this->_height/1.5);            $color = $this->_getrandcolor ();            Intercept for Chinese characters//$text =mb_substr ($str, $i, 1, ' utf-8 ');            $text = $str {$i};        Imagettftext ($this->_image, $size, $angle, $x, $y, $color, $fontfile, $text);        } if ($this->_snow) {$this->_getsnow ();            }else{if ($this->_pixel) {$this->_getpixel ();            } if ($this->_line) {$this->_getline ();        }} header (' Content-type:image/png ');        Imagepng ($this->_image);        Imagedestroy ($this->_image);    Return Strtolower ($STR); } privatefunction _getsnow () {for ($i =1; $i <= $this->_snow; $i + +) {imagestring ($this->_image,mt_rand (1,5), MT        _rand (0, $this->_width), Mt_rand (0, $this->_height), ' * ', $this->_getrandcolor ()); }} Private Function _getpixel () {for ($i =1; $i <= $this->_pixel; $i + +) {Imagesetpixel ($this        _image,mt_rand (0, $this->_width), Mt_rand (0, $this->_height), $this->_getrandcolor ()); }} Private Function _getline () {for ($i =1; $i <= $this->_line; $i + +) {imageline ($this->_image , Mt_rand (0, $this->_width), Mt_rand (0, $this->_height), Mt_rand (0, $this->_width), Mt_rand (0, $this->_        Height), $this->_getrandcolor ());        }} Private Function _generatestr ($length =4) {if ($length <1 | | $length >30) {return false; } $chars =array (' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' K ', ' m ', ' n ', ' P ', ' x ', ' y ', ' z ', ' a ', ' B ', ' C ' , ' D ', ' E ', ' F ', ' G ', ' H ', ' K ', ' M ', ' N ', ' P ', ' X ', ' Y ', ' Z ', 1,2,3,4,5,6,7,8,9);        $str =join (' ', Array_rand (Array_flip ($chars), $length));    return $str; } Private Function _getrandcolor () {return imagecolorallocate ($this->_image,mt_rand (0,255), Mt_rand (0,255), Mt_    Rand (0,255)); }}?>

2, testcaptcha.php

<?phprequire_once ' classgd.class.php '; $config =array (' fontfile ' = ' Fonts/simfang.ttf ',   //Introducing font files//' snow ') =>50, ' Pixel ' =>100, ' line ' =>10    ), $captcha =new captcha ($config); $captcha->getcaptcha ();? >

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.