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

Source: Internet
Author: User
Tags abs rand
Haven't written a post for a long time. have been busy with the new project. Recently, do the verification Code program, has always wanted to do a simple and generous, automatic identification more difficult. By collecting these times, it is found that the general practice has the following scenarios:
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:

Copy Code code as follows:

<?php
/**
* With text rotation, tilt, paste, Gazheng string interference line Verification code *
* @version 0.1
* @author Http://www.cnblogs.com/chengmo
* @copyright Cheng qq:8292669
*/
Class Utils_caption
{
var $Width = 60; Picture width
var $Height = 30; Picture High
var $Length = 4; Verify code digits
var $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 (); Verification Code
var $Image = ""; Drawing objects
var $FontColors =array (' #f36161 ', ' #6bc146 ', ' #5368bd '); Font color, red, green and blue
var $TPadden = 0.75;///character spacing, how many characters
var $Txbase = distance between 5;///x axes
var $Tybase =5;///y axis distance
var $TLine =true; 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 [rand (0, $charlen)]; Get 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); Get the Range
$arr []=array ($fontsize, $angle, $fontcolor, $char, $font, $bound); Get a rectangular box
$code. = $char;
}
$this->code= $arr; Verification Code
return $code;
}
Public Function Draw ()///drawing
{
if (Empty ($this->code)) $this->randrsi ();
$codes = $this->code; User authentication 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)///reprint characters
{
$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 Interference Line
Header ("Content-type:image/png");
Imagepng ($image);
Imagedestroy ($image);
}
/**
* Font rectangle width with font angle *
*
* @param int $font _size font size
* @param float $font _angle rotation angle
* @param string $font _file font file path
* @param string $text write characters
* @return Array returns the length and width of the height
*/
Private Function _calculatetextbox ($font _size, $font _angle, $font _file, $text) {
$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 ' => $max _y-$min _y,
' Box ' => $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;
}
}
}

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

Copy Code code as follows:

$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.
Click the download code

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.