Add dynamic verification code

Source: Internet
Author: User
Provides a variety of official and user-released code examples. For code reference, you are welcome to exchange and learn to add dynamic verification codes. The beating verification codes enhance your visual experience.
First open the official Thinkphp Image file, which is under ThinkPHP \ Extend \ Library \ ORG \ Util by default.
Open and add the following lines (that is, the method for generating the image verification code), and add a method for generating the dynamic verification code. /**
* Generate a dynamic image Verification Code
* @ Static
* @ Access public
* @ Param string $ length digits
* @ Param string $ mode type
* @ Param string $ width
* @ Param string $ height
* @ Return string
*/
Static function buildActiveImageVerify ($ length = 4, $ mode = 1, $ width = 60, $ height = 22, $ verifyName = 'verify '){
Import ('org. Util. string ');
Import ('org. Util. GIFEncoder ');
$ Randval = String: randString ($ length, $ mode );
Session ($ verifyName, md5 ($ randval ));
$ Width = ($ length * 10 + 10)> $ width? $ Length * 10 + 10: $ width;
// Generate a 32-frame GIF Animation
For ($ I = 0; $ I <32; $ I ++ ){
Ob_start ();
$ Image = imagecreate ($ width, $ height );
Imagecolorallocate ($ image, 0, 0 );
// Set the text color array
$ ColorList [] = ImageColorAllocate ($ image, 210 );
$ ColorList [] = ImageColorAllocate ($ image, 0, 64, 0 );
$ ColorList [] = ImageColorAllocate ($ image, 0, 64 );
$ ColorList [] = ImageColorAllocate ($ image, 0,128,128 );
$ ColorList [] = ImageColorAllocate ($ image, 27,52, 47 );
$ ColorList [] = ImageColorAllocate ($ image, 102 );
$ ColorList [] = ImageColorAllocate ($ image, 145 );
$ ColorList [] = ImageColorAllocate ($ image, 113 );
$ ColorList [] = ImageColorAllocate ($ image, 0, 51, 51 );
$ ColorList [] = ImageColorAllocate ($ image, 158,180, 35 );
$ ColorList [] = ImageColorAllocate ($ image, 59,59, 59 );
$ ColorList [] = ImageColorAllocate ($ image, 0, 0 );
$ ColorList [] = ImageColorAllocate ($ image, 1,128,180 );
$ ColorList [] = ImageColorAllocate ($ image, 0,153, 51 );
$ ColorList [] = ImageColorAllocate ($ images, 60,131, 1 );
$ ColorList [] = ImageColorAllocate ($ image, 0, 0 );
$ Fontcolor = ImageColorAllocate ($ image, 0, 0 );
$ Gray = ImageColorAllocate ($ image, 245,245,245 );
$ Color = imagecolorallocate ($ image, 255,255,255 );
$ Color2 = imagecolorallocate ($ image, 255, 0, 0 );
Imagefill ($ image, 0, 0, $ gray );
$ Space = 15; // Character Spacing
If ($ I> 0) {// shield the first frame
$ Top = 0;
For ($ k = 0; $ k <$ length; $ k ++ ){
$ ColorRandom = mt_rand (0, sizeof ($ colorList)-1 );
$ Float_top = rand (0, 4 );
$ Float_left = rand (0, 3 );
Imagestring ($ image, 6, $ space * $ k, $ top + $ float_top, substr ($ randval, $ k, 1), $ colorList [$ colorRandom]);
}
}
For ($ k = 0; $ k <20; $ k ++ ){
$ ColorRandom = mt_rand (0, sizeof ($ colorList)-1 );
Imagesetpixel ($ image, rand () % 70, rand () % 15, $ colorList [$ colorRandom]);

}
// Add interference lines
For ($ k = 0; $ k <3; $ k ++ ){
$ ColorRandom = mt_rand (0, sizeof ($ colorList)-1 );
$ Todrawline = 1;
If ($ todrawline ){
Imageline ($ image, mt_rand (0, $ width), mt_rand (0, $ height), mt_rand (0, $ width), mt_rand (0, $ height ), $ colorList [$ colorRandom]);
} Else {
$ W = mt_rand (0, $ width );
$ H = mt_rand (0, $ width );
Imagearc ($ image, $ width-floor ($ w/2), floor ($ h/2), $ w, $ h, rand (90,180), rand (180,270 ), $ colorList [$ colorRandom]);
}
}
Imagegif ($ image );
Imagedestroy ($ image );
$ Imagedata [] = ob_get_contents ();
Ob_clean ();
++ $ I;
}
$ Gif = new GIFEncoder ($ imagedata );
Header ('content-type: image/gif ');
Echo $ gif-> GetAnimation ();
}
Then, put the GIFEncoder class file into the same-level directory. /**
* GIFEncoder class
**/
Class GIFEncoder {
Var $ GIF = "GIF89a";/* GIF header 6 bytes */
Var $ VER = "GIFEncoder V2.06";/* Encoder version */
Var $ BUF = Array ();
Var $ LOP = 0;
Var $ DIS = 2;
Var $ COL =-1;
Var $ IMG =-1;
Var $ ERR = Array (
'Err00' => "Does not supported function for only one image! ",
'Err01' => "Source is not a GIF image! ",
'Err02' => "Unintelligible flag ",
'Err03' => "cocould not make animation from animated GIF source ",
);
Function GIFEncoder ($ GIF_src, $ GIF_dly = 100, $ GIF_lop = 0, $ GIF_dis = 0, $ GIF_red = 0, $ GIF_grn = 0, $ GIF_blu = 0, $ GIF_mod = 'bin '){
If (! Is_array ($ GIF_src )&&! Is_array ($ GIF_tim )){
Printf ("% s: % s", $ this-> VER, $ this-> ERR ['err00']);
Exit (0 );
}
$ This-> LOP = ($ GIF_lop>-1 )? $ GIF_lop: 0;
$ This-> DIS = ($ GIF_dis>-1 )? ($ GIF_dis <3 )? $ GIF_dis: 3): 2;
$ This-> COL = ($ GIF_red>-1 & $ GIF_grn>-1 & $ GIF_blu>-1 )? ($ GIF_red | ($ GIF_grn <8) | ($ GIF_blu <16):-1;

For ($ I = 0, $ src_count = count ($ GIF_src); $ I <$ src_count; $ I ++ ){
If (strToLower ($ GIF_mod) = "url "){
$ This-> BUF [] = fread (fopen ($ GIF_src [$ I], "rb"), filesize ($ GIF_src [$ I]);
} Elseif (strToLower ($ GIF_mod) = "bin "){
$ This-> BUF [] = $ GIF_src [$ I];
} Else {
Printf ("% s: % s (% s )! ", $ This-> VER, $ this-> ERR ['err02'], $ GIF_mod );
Exit (0 );
}
If (substr ($ this-> BUF [$ I], 0, 6 )! = "GIF87a" & substr ($ this-> BUF [$ I], 0, 6 )! = "GIF89a "){
Printf ("% s: % d % s", $ this-> VER, $ I, $ this-> ERR ['err01 ']);
Exit (0 );
}
For ($ j = (13 + 3*(2 <(ord ($ this-> BUF [$ I] {10}) & 0x07 ))), $ k = TRUE; $ k; $ j ++ ){
Switch ($ this-> BUF [$ I] {$ j }){
Case "! ":
If (substr ($ this-> BUF [$ I], ($ j + 3), 8) = "NETSCAPE "){
Printf ("% s: % s (% s source )! ", $ This-> VER, $ this-> ERR ['err03'], ($ I + 1 ));
Exit (0 );
}
Break;
Case ";":
$ K = FALSE;
Break;
}
}
}
GIFEncoder: GIFAddHeader ();
For ($ I = 0, $ count_buf = count ($ this-> BUF); $ I <$ count_buf; $ I ++ ){
GIFEncoder: GIFAddFrames ($ I, $ GIF_dly [$ I]);
}
GIFEncoder: GIFAddFooter ();
}
Function GIFAddHeader (){
$ Cmap = 0;
If (ord ($ this-> BUF [0] {10}) & 0x80 ){
$ Cmap = 3*(2 <(ord ($ this-> BUF [0] {10}) & 0x07 ));
$ This-> GIF. = substr ($ this-> BUF [0], 6, 7 );
$ This-> GIF. = substr ($ this-> BUF [0], 13, $ cmap );
$ This-> GIF. = "! \ 377 \ 13NETSCAPE2. 0 \ 3 \ 1 ". GIFEncoder: GIFWord ($ this-> LOP)." \ 0 ";
}
}
Function GIFAddFrames ($ I, $ d ){
$ Locals_str = 13 + 3*(2 <(ord ($ this-> BUF [$ I] {10}) & 0x07 ));
$ Locals_end = strlen ($ this-> BUF [$ I])-$ Locals_str-1;
$ Locals_tmp = substr ($ this-> BUF [$ I], $ Locals_str, $ Locals_end );
$ Global_len = 2 <(ord ($ this-> BUF [0] {10}) & 0x07 );
$ Locals_len = 2 <(ord ($ this-> BUF [$ I] {10}) & 0x07 );
$ Global_rgb = substr ($ this-> BUF [0], 13, 3*(2 <(ord ($ this-> BUF [0] {10 }) & 0x07 )));
$ Locals_rgb = substr ($ this-> BUF [$ I], 13, 3*(2 <(ord ($ this-> BUF [$ I] {10 }) & 0x07 )));
$ Locals_ext = "! \ XF9 \ x04 ". chr ($ this-> DIS <2) + 0 ). chr ($ d> 0) & 0xFF ). chr ($ d> 8) & 0xFF ). "\ x0 \ x0 ";
If ($ this-> COL>-1 & ord ($ this-> BUF [$ I] {10}) & 0x80 ){
For ($ j = 0; $ j <(2 <(ord ($ this-> BUF [$ I] {10}) & 0x07 )); $ j ++ ){
If (ord ($ Locals_rgb {3 * $ j + 0}) ==( $ this-> COL> 0) & 0xFF & ord ($ Locals_rgb {3 * $ j + 1}) ==( $ this-> COL> 8) & 0xFF & ord ($ Locals_rgb {3 * $ j + 2}) ==( $ this-> COL> 16) & 0xFF ){
$ Locals_ext = "! \ XF9 \ x04 ". chr ($ this-> DIS <2) + 1 ). chr ($ d> 0) & 0xFF ). chr ($ d> 8) & 0xFF ). chr ($ j ). "\ x0 ";
Break;
}
}
}
Switch ($ Locals_tmp {0 }){
Case "! ":
$ Locals_img = substr ($ Locals_tmp, 8, 10 );
$ Locals_tmp = substr ($ Locals_tmp, 18, strlen ($ Locals_tmp)-18 );
Break;
Case ",":
$ Locals_img = substr ($ Locals_tmp, 0, 10 );
$ Locals_tmp = substr ($ Locals_tmp, 10, strlen ($ Locals_tmp)-10 );
Break;
}
If (ord ($ this-> BUF [$ I] {10}) & 0x80 & $ this-> IMG>-1 ){
If ($ Global_len = $ Locals_len ){
If (GIFEncoder: GIFBlockCompare ($ Global_rgb, $ Locals_rgb, $ Global_len )){
$ This-> GIF. = ($ Locals_ext. $ Locals_img. $ Locals_tmp );
} Else {
$ Byte = ord ($ Locals_img {9 });
$ Byte | = 0x80;
$ Byte & = 0xF8;
$ Byte | = (ord ($ this-> BUF [0] {10}) & 0x07 );
$ Locals_img {9} = chr ($ byte );
$ This-> GIF. = ($ Locals_ext. $ Locals_img. $ Locals_rgb. $ Locals_tmp );
}
} Else {
$ Byte = ord ($ Locals_img {9 });
$ Byte | = 0x80;
$ Byte & = 0xF8;
$ Byte | = (ord ($ this-> BUF [$ I] {10}) & 0x07 );
$ Locals_img {9} = chr ($ byte );
$ This-> GIF. = ($ Locals_ext. $ Locals_img. $ Locals_rgb. $ Locals_tmp );
}
} Else {
$ This-> GIF. = ($ Locals_ext. $ Locals_img. $ Locals_tmp );
}
$ This-> IMG = 1;
}
Function GIFAddFooter (){
$ This-> GIF. = ";";
}
Function GIFBlockCompare ($ GlobalBlock, $ LocalBlock, $ Len ){
For ($ I = 0; $ I <$ Len; $ I ++ ){
If ($ GlobalBlock {3 * $ I + 0 }! = $ LocalBlock {3 * $ I + 0} | $ GlobalBlock {3 * $ I + 1 }! = $ LocalBlock {3 * $ I + 1} | $ GlobalBlock {3 * $ I + 2 }! = $ LocalBlock {3 * $ I + 2 }){
Return (0 );
}
}
Return (1 );
}
Function GIFWord ($ int ){
Return (chr ($ int & 0xFF). chr ($ int> 8) & 0xFF ));
}
Function GetAnimation (){
Return ($ this-> GIF );
}
}
The usage is basically the same as the original method, but do not pass the type parameter, because the default dynamic format must be gif, and the image width cannot be less than 60. /**
* Generate a verification code
* @ Access public
* @ Return void
*/
Public function verify (){
Import ("ORG. Util. Image ");
Image: buildActiveImageVerify (4, 1 );
}

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.