Generation of intricate (inclined, sinusoidal interference lines, sticky, rotational inspection) card codes

Source: Internet
Author: User
This time to you to create a complex (inclined, sinusoidal interference line, sticky, rotational inspection) card code, generate intricate (tilt, sinusoidal interference line, sticky, rotation test) The notes of the Code, the following is the actual case, together to see.

Now many websites use the verification code is very easy to be automatically recognized by the software, this article introduced a PHP generation complex verification code added tilt, string interference line, paste, rotation and other effects

Common software is not easy to automatically identify the code of practice has the following 3 kinds:

1, font variant (generally through the algorithm, to distort)

2, the font paste (this is the QQ verification code for the representative, the Internet is still difficult to find, cracked QQ verification code)

3, interference line, noise point (this recognition is quite easy, it is easy to be recognized by the program Automation)

For the above mentioned, the 1th, 22 methods, in the identification time, is more difficult.

Specific implementation code:

<?php/** * with text rotation, tilt, paste, home plus string interference line verification code * */class Utils_caption{var $Width = 60;      Picture width var $Height = 30;      Picture high var $Length = 4;  Verify the number of code bits var $BgColor = "#FFFFFF"; The background color var $TFonts = Array ("Font.ttf"); var $TFontSize =array (17,20); Font size range var $TFontAngle =array ( -20,20);     Rotation angle var $Chars = "0123456789";       Verification Code Range (alphanumeric) var $Code = array ();       Verification code var $Image = ""; Graphical object var $FontColors =array (' #f36161 ', ' #6bc146 ', ' #5368bd '); Font color, red green blue var $TPadden = 0.75;///character spacing, how many characters var $Txbase = 5;///x Axis distance var $Tybase =5;///y axis distance var $TLine =true; Draw interference Line Public function randrsi ()//Generate code {$this->tfontangle=range ($this->tfontangle[0], $this->tfontangle [1]); $this->tfontsize=range ($this->tfontsize[0], $this->tfontsize[1]); $arr =array (); $Chars = $this->chars;$ tfontangle= $this->tfontangle; $TFontSize = $this->tfontsize; $FontColors = $this->fontcolors; $code = ""; $font =dirname (FILE). " /font/". $this->tfonts[0]; $charlen =strlen ($Chars)-1; $anglelen =count ($TFontAngle)-1;Angle range $fontsizelen=count ($TFontSize)-1; Angle range $fontcolorlen=count ($FontColors)-1; Angle range for ($i =0; $i < $this->length; $i + +)//get character with color {$char = $Chars [rand (0, $charlen)];///Get character $angle= $TFontAngle [Rand (0, $anglelen)]; Rotation angle $fontsize= $TFontSize [rand (0, $fontsizelen)]; Font size $fontcolor= $FontColors [rand (0, $fontcolorlen)]; Font size $bound= $this->_calculatetextbox ($fontsize, $angle, $font, $char); Get range $arr[]=array ($fontsize, $angle, $fontcolor, $char, $font, $bound); Get the Rectangle box $code.= $char;} $this->code= $arr; Verification code return $code;} Public Function Draw ()///Draw {if (empty ($this->code)) $this->randrsi () $codes = $this->code;//user Verification Code $wh=$ THIS-&GT;_GETIMAGEWH ($codes); $width = $WH [0]; $height = $WH [1]; Height $this->width= $width $this->height= $height $this->image = imagecreate ($width, $height); $image = $this- >Image; $back = $this->_getcolor2 ($this->_getcolor ($this->bgcolor)); Background color imagefilledrectangle ($image, 0, 0, $width, $height, $back); Fill background $tpadden= $this->tpadden; $basex= $this->txbase; $color =null;foreach ($codes as $v)//stooped character {$bound = $v [5]; $color = $this->_getcolor2 ($this->_ GetColor ($v [2]); Imagettftext ($image, $v [0], $v [1], $basex, $bound [' height '], $color, $v [4], $v [3]); $basex = $basex +$ bound[' width ']* $TPadden-$bound [' left '];///calculates the next margin} $this->tline? $this->_wirtesinline ($color, $basex): null ; Draw the interference line header ("Content-type:image/png"); Imagepng ($image); Imagedestroy ($image);} /** * Get font rectangle width by Font angle * * * @param int $font _size font size * @param float $font _angle Rotation angle * @param string $font _file font file path * @p Aram String $text Write character * @return Array returns the */private function _calculatetextbox ($font _size, $font _angle, $font _file, $te XT) {$box = Imagettfbbox ($font _size, $font _angle, $font _file, $text); $min _x = min (Array ($box [0], $box [2], $box [4], $box [6 ]), $max _x = max (Array ($box [0], $box [2], $box [4], $box [6]), $min _y = min (Array ($box [1], $box [3], $box [5], $box [7]); $max _y = max (Array ($box [1], $box [3], $box [5], $box [7]); return array (' left ' = = ($min _x >=-1)? -abs ($min _x + 1): ABS ($min _x + 2), ' Top ' + ABS ($min _y), ' width ' = $max _x-$min _x, ' height ' and $max _y-$min _y, ' B Ox ' = $box);} Private Function _getcolor ($color)//#ffffff {return Array (Hexdec ($color [1]. $color [2]), Hexdec ($color [3]. $color [4]), Hexdec ($color [5]. $color [6]);} Private Function _getcolor2 ($color)//#ffffff {return imagecolorallocate ($this->image, $color [0], $color [1], $color [2]);} Private Function _getimagewh ($data) {$TPadden = $this->tpadden; $w = $this->txbase; $h =0;foreach ($data as $v) {$w = $w + $v [5][' width ']* $TPadden-$v [5][' left ']; $h = $h > $v [5][' height ']? $h: $v [5][' height '];} Return Array (max ($w, $this->width), Max ($h, $this->height));} Draw the sinusoidal interference line private function _wirtesinline ($color, $w) {$img = $this->image; $h = $this->height; $h 1=rand ( -5,5); $h 2 = Rand ( -1,1); $w 2=rand (10,15); $h 3=rand (4,6); for ($i =-$w/2; $i < $w/2; $i = $i +0.1) {$y = $h/$h 3*sin ($i/$w 2) + $h/2+ $h 1; Imagesetpixel ($img, $i + $w/2, $y, $color), $h 2!=0?imagesetpixel ($img, $i + $w/2, $y + $h 2, $color): null;}}

Demo Code:

$rsi = new Utils_caption (), $rsi->tfontsize=array (15,17), $rsi->width=50; $rsi->height=25; $code = $rsi Randrsi (); Session_Start (); $_session["Checkcode"] = $code; $rsi->draw ();

Operating effect:

You can modify the corresponding values in the code to achieve your own complexity

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

PHP reserved Key value + Merge array

How to exclude duplicate values in a two-dimensional array

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.