1, font variants (generally through the algorithm, to distort, more representative is: http://code.google.com/p/cool-php-captcha/
2, the font sticky paste (this inside with the QQ verification code for the representative, the current online or difficult to find, crack QQ verification code)
3, interference line, noise (this identification is quite easy, easy to be recognized by the program Automation)
For the above mentioned, the 1th, 22 methods, in the identification time, it is more difficult. Individuals prefer the second approach, which does not seem very laborious. And the distorted text, always feel strange. Haha, pure personal preference.
Implementation code:
;? php /** * with text rotation, tilt, paste, Gazheng string interference line Verification code * * @version 0.1 * @author Http://www.cnblogs.com/chengmo * @copyright Cheng Q q:8292669 * / class Utils_caption {var $Width = 60; //Picture width var $Height = 30; //Picture high var $Length = 4; //Verify code digitsvar $BgColor = " #FFFFFF ";
//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 "; //Authentication code range (alphanumeric) var $Code = array (); //Authentication code var $Image = ""; //Graphics Object var $FontColors =array (' #f36161 ', ' #6bc146 ', ' ' c11> #5368bd '); //Font color, red-green-blue var $TPadden = 0.75; ///Character spacing, how many characters var $Txbase = 5; ///x axis distance var $Tybase = 5; var $TLine =true on Both sides of ///y axis</span> ; ///Draw interference line public Function Randrsi () ///generate Authenticode{$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 characters and colors {$char = $Chars [r and (0, $charlen)]; ///Gets the 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); ///gets a range of $arr []=array ($fontsize, $angle, $fontcolor, $char, $font, $bound);
///Get rectangular frame $code. = $char; } $this->code= $arr;
//Verification code return $code;
The Public Function Draw () ///paint {if empty ($this->code)) $this->randrsi (); $codes = $this->code;
///user authentication code $WH = $this->_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) ///reprint 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 left margin } $this->tline? $this->_wirtesinline ($color, $basex): null;
///Draw interference line header (" content-type:image/png ");
Imagepng ($image);
Imagedestroy ($image); /** * The font rectangle WIDTH * * * * @param int $font _size font size * @param float $font _angle rotation angle * @param strin G $font _file font file path * @param string $text write character * @return array return long width/height * *Private Function _calculatetextbox ($font _size, $font _angle, $font _file, $text) {$box = Imagettfbbox ($font _size, $font _a
Ngle, $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 ' => ;
$max _y-$min _y, ' box ' => $box); Private Function _getcolor ($color) //#ffffff {return Array (Hexdec ($color [1]. $color [2]), Hex
Dec ($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 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; }
}
}
Off font:
Font.ttf, a simple bold file.
Description
First look at the operation of the effect bar, we do not busy copying run.
... ..
The main features are: rotation, and then paste, interference line is the line thickness can be changed, and then the sine wave shape can change.
More complex is: calculatetextbox This function, this is to get the width of the character after rotation height.
Demo
$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 ();
Well, that's all, the code has a lot of deficiencies. Welcome friends to put forward good suggestions.