Thinkphp 3.2.3 comes with a code class in/thinkphp/library/think/verify.class.php, and the font file is located in/thinkphp/library/think/verify/
Can be modified within the Verify.class.php file, or you can write a class that inherits its own code. If you write an inherited class separately, you can reuse the properties and methods of the parent class, but be aware that some of the properties and methods in the parent class are private, and you can modify these private properties and methods for protection (protected), if you do not want to modify the method that the framework comes with. You can also define these properties and methods in subclasses.
The test controller is located in/application/home/controller/testverifycontroller.class.php
The attempt to test is located in/application/home/view/user/verify.html
The custom subclass is located in/applicaion/home/common/verifyprocess.class.php
VerifyProcess.class.php:
<?phpnamespace home\common;use Think\verify;class Verifyprocess extends Verify {private $_image = NULL; Verification code Picture Instance Private $_color = NULL; CAPTCHA Font Color public Function entryprocess ($id = ") {//Picture width (px) $this->imagew | | $this->imagew = $this->leng th* $this->fontsize*1.5 + $this->length* $this->fontsize/2; Picture height (px) $this->imageh | | $this->imageh = $this->fontsize * 2.5; Create an image $this->imagew x $this->imageh $this->_image = imagecreate ($this->imagew, $this->imageh); Set Background imagecolorallocate ($this->_image, $this->bg[0], $this->bg[1], $this->bg[2]); CAPTCHA font random color $this->_color = imagecolorallocate ($this->_image, Mt_rand (1,150), Mt_rand (1,150), Mt_rand (1,150)); The captcha uses a random font $ttfPath = $_server[' Document_root '). ' /thinkphp/library/think/verify/'. ($this->usezh? ' Zhttfs ': ' TTFs '). ‘/‘; if (Empty ($this->fontttf)) {$dir = Dir ($ttfPath); $ttfs = ARray (); while (false!== ($file = $dir->read ())) {if ($file [0]! = '. ' && substr ($file,-4) = = '. Ttf ') { $ttfs [] = $file; }} $dir->close (); $this->fontttf = $ttfs [Array_rand ($TTFS)]; } $this->fontttf = $ttfPath. $this->fontttf; if ($this->useimgbg) {$this->_background (); } if ($this->usenoise) {//Plot dot $this->_writenoise (); } if ($this->usecurve) {//Draw interference line $this->_writecurve (); }//Draw verification Code $codeNX = 0; The left margin//verification code for the nth character of the captcha is a simple operation $a = Mt_rand (1,9); $b = Mt_rand (1,9); $operate _array = Array (' + ', '-', ' * '); $key = Mt_rand (0, Count ($operate _array)-1); if ($operate _array[$key] = = ' + ') {//addition $code = $a. ' + '. $b. ' = '; $result = intval ($a + $b); } elseif ($operate _array[$key] = = '-') {//Subtraction $code = max ($a, $b). ' -'. Min ($a, $b). ' = '; $result = Intval (ABS ($a-$b)); } else {//Multiplication $code = $a. ' * '. $b. ' = '; $result = intval ($a * $b); } $this->length = 4; for ($i = 0; $i < $this->length; $i + +) {$codeNX + = Mt_rand ($this->fontsize*1.2, $this->fontsize*1.6); Imagettftext ($this->_image, $this->fontsize, Mt_rand ( -40, +), $codeNX, $this->fontsize*1.6, $this->_col Or, $this->fontttf, $code [$i]); }//Save verification Code $key = $this->authcode ($this->sekey); $result = $this->authcode ($result); $secode = Array (); $secode [' verify_code '] = $result; Save the check code to the session $secode [' verify_time '] = now_time; Verification code creation time session ($key. $id, $secode); Header (' Cache-control:private, max-age=0, No-store, No-cache, Must-revalidate '); Header (' cache-control:post-check=0, pre-check=0 ', false); Header (' Pragma:no-cache '); Header ("Content-type:image/png"); Output Image Imagepng ($this->_image); Imagedestroy ($this->_image);} /** * Draw the dots * to write letters or numbers of different colors on the picture */PRivate function _writenoise () {$codeSet = ' 2345678abcdefhijkmnpqrstuvwxyz '; for ($i = 0; $i < $i + +) {//DOT color $noiseColor = imagecolorallocate ($this->_image, Mt_rand (150,225 ), Mt_rand (150,225), Mt_rand (150,225)); for ($j = 0; $j < 5; $j + +) {//Plot point imagestring ($this->_image, 5, Mt_rand ( -10, $this->im Agew), Mt_rand ( -10, $this->imageh), $codeSet [Mt_rand (0)], $noiseColor); }}}/** * draw a random sine function curve consisting of two lines as the interfering line (you can change to a more handsome curve function) * * High school math formula I forgot Nirvana, write it out * sine type function analytic: Y=asin (ωx+φ +b * The effect of each constant value on the function image: * A: Determines the peak (i.e. the multiple of the longitudinal stretch compression) * B: Indicates the position of the waveform on the Y axis or the longitudinal moving distance (upper plus minus) *φ: determines the waveform and x-axis position relationship or horizontal Move distance (left plus right minus) *ω: Decision period (min positive period t=2π/∣ω∣) * */Private Function _writecurve () {$px = $py = 0; The front part of the curve $A = Mt_rand (1, $this->IMAGEH/2); Amplitude $b = Mt_rand (-$this->IMAGEH/4, $this->IMAGEH/4); Y-axis direction offset $f = Mt_rand (-$this->imageH/4, $this->IMAGEH/4); X-axis direction Offset $T = Mt_rand ($this->imageh, $this->imagew*2); Cycle $w = (M_PI)/$T; $px 1 = 0; Curve horizontal start position $px 2 = Mt_rand ($this->imagew/2, $this->imagew * 0.8); Curve horizontal end position for ($px = $px 1; $px <= $px 2; $px = $px + 1) {if ($w!=0) {$py = $A * Sin ($w * $px + $f) + $b + $this->imageh/2; y = Asin (ωx+φ) + b $i = (int) ($this->FONTSIZE/5); while ($i > 0) {imagesetpixel ($this->_image, $px + $i, $py + $i, $this->_color); This (while) loop is much better than Imagettftext and imagestring to draw (without this while loop) at one time with the font size $i-; }}}//curve after section $A = Mt_rand (1, $this->IMAGEH/2); Amplitude $f = Mt_rand (-$this->IMAGEH/4, $this->IMAGEH/4); X-axis direction Offset $T = Mt_rand ($this->imageh, $this->imagew*2); Cycle $w = (M_PI)/$T; $b = $py-$A * sin ($w * $px + $f)-$this->imageh/2; $PX 1 = $px 2; $PX2 = $this->imagew; for ($px = $px 1; $px <= $px 2; $px = $px + 1) {if ($w!=0) {$py = $A * Sin ($w * $px + $f) + $b + $this->imag EH/2; y = Asin (ωx+φ) + b $i = (int) ($this->FONTSIZE/5); while ($i > 0) {imagesetpixel ($this->_image, $px + $i, $py + $i, $this->_color); $i--; }}}}/* Cryptographic Verification Code */Private Function Authcode ($str) {$key = substr (MD5 ($this->sekey), 5, 8); $STR = substr (MD5 ($STR), 8, 10); return MD5 ($key. $str); /** * Draw Background image * Note: If the captcha output picture is larger, it will occupy more system resources */Private Function _background () {$path = DirName (__file__). ' /verify/bgs/'; $dir = Dir ($path); $BGS = Array (); while (false!== ($file = $dir->read ())) {if ($file [0]! = '. ' && substr ($file,-4) = = '. jpg ') { $BGS [] = $path. $file; }} $dir->close (); $GB = $bgs [Array_rand ($BGS)]; List ($width, $height) = @getImageSize ($GB); Resample $bgImage = @imagecreatefromjpeg ($GB); @imagecopyresampled ($this->_image, $bgImage, 0, 0, 0, 0, $this->imagew, $this->imageh, $width, $height); @imagedestroy ($bgImage); } }
TestVerifyController.class.php:
<?phpnamespace home\controller;use think\controller;use Home\common\verifyprocess;class TestVerifyController Extends Controller {//Interface Public Function index () {$this->display (' user/verify ');} Verify Public Function check_verify () {$verify = new verifyprocess (); if (! $verify->check ($_post[' verify ')) { $this->error (' captcha error ');} } Displays the Captcha public function Verify () {$verify = new verifyprocess (); $verify->entryprocess ();} }
Verify.html:
<! DOCTYPE html>
Effect:
thinkphp 3.2.3 Plus minus multiplication verification code class