PHP Verification Code (tilt, sinusoidal interference line, sticky, rotate)

Source: Internet
Author: User
Tags abs foreach min rand

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-&gt;tfontangle=range ($this-&gt;tfontangle[0], $this-&gt;tfontangle[1]);

		$this-&gt;tfontsize=range ($this-&gt;tfontsize[0], $this-&gt;tfontsize[1]);
		$arr =array ();
		$Chars = $this-&gt;chars;
		$TFontAngle = $this-&gt;tfontangle;
		$TFontSize = $this-&gt;tfontsize;
		$FontColors = $this-&gt;fontcolors;
		$code = "";

		$font =dirname (__file__). " /font/ ". $this-&gt;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 &lt; $this-&gt;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-&gt;_calculatetextbox ($fontsize, $angle, $font, $char);    ///gets a range of  $arr []=array ($fontsize, $angle, $fontcolor, $char, $font, $bound);
		 ///Get rectangular frame  $code. = $char;  } $this-&gt;code= $arr;
	 //Verification code  return $code;
		The Public Function Draw ()  ///paint  {if empty ($this-&gt;code)) $this-&gt;randrsi ();  $codes = $this-&gt;code;

		 ///user authentication code  $WH = $this-&gt;_getimagewh ($codes);
		$width = $WH [0];  $height = $WH [1];
		 ///height  $this-&gt;width= $width;

		$this-&gt;height= $height;
		$this-&gt;image = imagecreate ($width, $height);

		$image = $this-&gt;image;  $back = $this-&gt;_getcolor2 ($this-&gt;_getcolor ($this-&gt;bgcolor));   ///background color  imagefilledrectangle ($image, 0, 0, $width, $height, $back);  ///Fill Background$TPadden = $this-&gt;tpadden;
		$basex = $this-&gt;txbase;
		$color =null;
			foreach ($codes as $v)  ///reprint character  {$bound = $v [5];
			$color = $this-&gt;_getcolor2 ($this-&gt;_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-&gt;tline? $this-&gt;_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 " =&gt; ($min _x &gt;=-1)?-abs ($min _x + 1): ABS ($min _x + 2), '  top   ' =&gt; abs ($min _y), '  width  ' =&gt; $max _x-$min _x, '  height  ' =&gt ;
	$max _y-$min _y, '  box  ' =&gt; $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-&gt;image, $color [0], $color [1], $color [2]);
		Private Function _getimagewh ($data) {$TPadden = $this-&gt;tpadden;
		$w = $this-&gt;txbase;
		$h = 0;
			foreach ($data as $v) {$w = $w + $v [5]['  width  ']* $TPadden-$v [5]['   left  '];
		$h = $h &gt; $v [5]['  height  ']? $h: $v [5]['  height  '];
	Return Array (max ($w, $this-&gt;width), Max ($h, $this-&gt;height));

		 //Draw sinusoidal interference line  Private Function _wirtesinline ($color, $w) {$img = $this-&gt;image;
		$h = $this-&gt;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 &lt; $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.

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.