Verification codes are used in many places. Here we provide a newly developed security verification code class that supports generating GIF image verification codes (with noise, interference lines, grids, random backgrounds, and random custom fonts, tilt, GIF animation ).
Verification codes are used in many places. Here we provide a newly developed security verification code class that supports generating GIF image verification codes (with noise, interference lines, grids, random backgrounds, and random custom fonts, tilt, GIF animation ).
First look
The path of the font and font files must be set in $ FontFilePath and $ FontFileName of the class. For example:
The Code is as follows:
Private static $ FontFilePath = "static/font/"; // relative location of the Code File
Private static $ FontFileName = array ("3.ttf"); // array (" 1.ttf", "2.ttf"," 3.ttf", "4.ttf"," 5.ttf", "6.ttf"," 7.ttf", "8.ttf "); //
The complete code is as follows:
The Code is as follows:
/**
(Verification code generation class, supports generating GIF image Verification Code (with noise, interference line, grid, random color background, random Custom font, tilt, GIF animation)
Server:
$ Mod = strtolower (isset ($ _ REQUEST ["mod"])? $ _ REQUEST ["mod"]: "");
If ($ mod = "code "){
Echo SecurityCode: Draw (4, 1,120, 30, 5, 10,100, "secode ");
Die ();
}
Call:
Verification:
$ ReqCode = strtolower (isset ($ _ REQUEST ["secode"])? $ _ REQUEST ["secode"]: ""); // REQUEST verification code
$ SessionCode = strtolower (isset ($ _ SESSION ["secode"])? $ _ SESSION ["secode"]: ""); // The verification code generated by the SESSION
If ($ reqCode! = $ SessionCode ){
Echo "security verification code error! ";
Die ();
}
*/
$ Mod = strtolower (isset ($ _ REQUEST ["mod"])? $ _ REQUEST ["mod"]: "");
If ($ mod = "code "){
Echo SecurityCode: Draw (4, 15,100, 27, 10, 2,100, "secode ");
Die ();
}
// Security verification code
Class SecurityCode {
Private static $ Debug = 0;
Private static $ Code = '';
Private static $ Chars = 'bcdefhkmnrstuvwxyabcdefghkmnprstuvwxy34568 ';
// Private static $ Chars = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz01234567890 ';
Private static $ TextGap = 20;
Private static $ TextMargin = 5;
Private static $ FontFilePath = "static/font/"; // relative location of the Code File
Private static $ FontFileName = array ("3.ttf"); // array (" 1.ttf", "2.ttf"," 3.ttf", "4.ttf"," 5.ttf", "6.ttf"," 7.ttf", "8.ttf "); //
Private static $ Img = 'gif 89a'; // GIF header 6 bytes
Private static $ BUF = Array ();
Private static $ LOP = 0;
Private static $ DIS = 2;
Private static $ COL =-1;
Private static $ IMG =-1;
/**
Generate GIF image Verification
@ Param int $ L verification code length
@ Param int $ F: Number of frames for generating a Gif Image
@ Param int $ W width
@ Param int $ H height
@ Param int $ number of MixCnt interference lines
@ Param int $ lineGap gridline Interval
@ Param int $ noisyCnt bath points
@ Param int $ sessionName Verification Code Session name
*/
Public static function Draw ($ L = 4, $ F = 1, $ W = 150, $ H = 30, $ MixCnt = 2, $ lineGap = 0, $ noisyCnt = 10, $ sessionName = "Code "){
Ob_start ();
Ob_clean ();
For ($ I = 0; $ I <$ L; $ I ++ ){
Self: $ Code. = SubStr (self: $ Chars, mt_rand (0, strlen (self: $ Chars)-1), 1 );
}
If (! Isset ($ _ SESSION ))
Session_start ();
$ _ SESSION [$ sessionName] = strtolower (self: $ Code );
$ BgRGB = array (rand (0,255), rand (0,255), rand (0,255 ));
// Generate a GIF animation with multiple frames
For ($ I = 0; $ I <$ F; $ I ++ ){
$ Img = ImageCreate ($ W, $ H );
// Background color
$ BgColor = imagecolorallocate ($ img, $ bgRGB [0], $ bgRGB [1], $ bgRGB [2]);
ImageColorTransparent ($ img, $ bgColor );
Unset ($ bgColor );
// Add Noise
$ MaxNoisy = rand (0, $ noisyCnt );
$ NoisyColor = imagecolorallocate ($ img, rand (0,255), rand (0,255), rand (0,255 ));
For ($ k = 0; $ k <= $ maxNoisy; $ k ++ ){
Imagesetpixel ($ img, rand (0, $ W), rand (0, $ H), $ noisyColor );
}
// Add a grid
If ($ lineGap> 0 ){
For ($ m = 0; $ m <($ W/$ lineGap); $ m ++) {// vertical line
Imageline ($ img, $ m * $ lineGap, 0, $ m * $ lineGap, $ H, $ noisyColor );
}
For ($ n = 0; $ n <($ H/$ lineGap); $ n ++) {// horizontal line
Imageline ($ img, 0, $ n * $ lineGap, $ W, $ n * $ lineGap, $ noisyColor );
}
}
Unset ($ noisyColor );
// Add interference lines
For ($ k = 0; $ k <$ MixCnt; $ k ++ ){
$ Wr = mt_rand (0, $ W );
$ Hr = mt_rand (0, $ W );
$ LineColor = imagecolorallocate ($ img, rand (0,255), rand (0,255), rand (0,255 ));
Imagearc ($ img, $ W-floor ($ wr/2), floor ($ hr/2), $ wr, $ hr, rand (90,180), rand (180,270 ), $ lineColor );
Unset ($ lineColor );
Unset ($ wr, $ hr );
}
// Ignore text in the first frame
If ($ I! = 0 | $ F <= 1 ){
// Text
$ ForeColor = imagecolorallocate ($ img, rand (0,255), rand (0,255), rand (0,255 ));
For ($ j = 0; $ j <$ L; $ j ++ ){
$ FontFile = self ::$ FontFilePath. self ::$ FontFileName [rand (0, count (self ::$ FontFileName)-1)];
If (! File_exists ($ fontFile ))
Imagestring ($ img, 4, self: $ TextMargin + $ j * self: $ TextGap, ($ H-rand ($ H/2, $ H), self:: $ Code [$ j], $ foreColor );
Else
ImageTTFtext ($ img, rand (15, 18), rand (-15, 15), self: $ TextMargin + $ j * self: $ TextGap, ($ H-rand (7, 10), $ foreColor, $ fontFile, self: $ Code [$ j]);
}
Unset ($ foreColor );
}
ImageGif ($ img );
Imagedestroy ($ img );
$ Imdata [] = ob_get_contents ();
OB_clean ();
}
Unset ($ W, $ H, $ B );
If (self ::$ Debug ){
Echo $ _ SESSION ['code'];
Echo'
', Var_Dump($Imdata), '
';
Die ();
}
Header ('content-type: image/gif ');
Return self: CreateGif ($ Imdata, 20 );
Unset ($ Imdata );
}
Private static function CreateGif ($ GIF_src, $ GIF_dly = 10, $ 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 )){
Throw New Exception ('error: '. _ LINE _.', Does not supported function for only one image !! ');
Die ();
}
Self: $ LOP = ($ GIF_lop>-1 )? $ GIF_lop: 0;
Self: $ DIS = ($ GIF_dis>-1 )? ($ GIF_dis <3 )? $ GIF_dis: 3): 2;
Self: $ 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 '){
Self: $ BUF [] = fread (fopen ($ GIF_src [$ I], 'rb'), filesize ($ GIF_src [$ I]);
} Elseif (strToLower ($ GIF_mod) = 'bin '){
Self: $ BUF [] = $ GIF_src [$ I];
} Else {
Throw New Exception ('error: '. _ LINE _.', Unintelligible flag ('. $ GIF_mod .')! ');
Die ();
}
If (! (Substr (self ::$ BUF [$ I], 0, 6) = 'gif 87a' Or Substr (self ::$ BUF [$ I], 0, 6) = 'gif 89a ')){
Throw New Exception ('error: '. _ LINE _.', Source '. $ I.' is not a GIF image! ');
Die ();
}
For ($ j = (13 + 3*(2 <(ord (self: $ BUF [$ I] {10}) & 0x07 ))), $ k = TRUE; $ k; $ j ++ ){
Switch (self ::$ BUF [$ I] {$ j }){
Case '! ':
If (substr (self: $ BUF [$ I], ($ j + 3), 8) = 'netscape '){
Throw New Exception ('error: '. _ LINE _.', cocould not make animation from animated GIF source ('. ($ I + 1 ).')! ');
Die ();
}
Break;
Case ';':
$ K = FALSE;
Break;
}
}
}
Self: AddHeader ();
For ($ I = 0, $ count_buf = count (self: $ BUF); $ I <$ count_buf; $ I ++ ){
Self: AddFrames ($ I, $ GIF_dly );
}
Self: $ Img. = ';';
Return (self: $ Img );
}
Private static function AddHeader (){
$ I = 0;
If (ord (self ::$ BUF [0] {10}) & 0x80 ){
$ I = 3*(2 <(ord (self: $ BUF [0] {10}) & 0x07 ));
Self: $ Img. = substr (self: $ BUF [0], 6, 7 );
Self: $ Img. = substr (self: $ BUF [0], 13, $ I );
Self: $ Img. = "! \ 377 \ 13NETSCAPE2. 0 \ 3 \ 1 ". chr (self: $ LOP & 0xFF ). chr (self ::$ LOP> 8) & 0xFF ). "\ 0 ";
}
Unset ($ I );
}
Private static function AddFrames ($ I, $ d ){
$ L_str = 13 + 3*(2 <(ord (self: $ BUF [$ I] {10}) & 0x07 ));
$ L_end = strlen (self: $ BUF [$ I])-$ L_str-1;
$ L_tmp = substr (self: $ BUF [$ I], $ L_str, $ L_end );
$ G_len = 2 <(ord (self: $ BUF [0] {10}) & 0x07 );
$ L_len = 2 <(ord (self: $ BUF [$ I] {10}) & 0x07 );
$ G_rgb = substr (self: $ BUF [0], 13, 3*(2 <(ord (self: $ BUF [0] {10 }) & 0x07 )));
$ L_rgb = substr (self: $ BUF [$ I], 13, 3*(2 <(ord (self: $ BUF [$ I] {10 }) & 0x07 )));
$ L_ext = "! \ XF9 \ x04 ". chr (self: $ DIS <2) + 0 ). chr ($ d> 0) & 0xFF ). chr ($ d> 8) & 0xFF ). "\ x0 \ x0 ";
If (self ::$ COL>-1 & ord (self ::$ BUF [$ I] {10}) & 0x80 ){
For ($ j = 0; $ j <(2 <(ord (self: $ BUF [$ I] {10}) & 0x07 )); $ j ++ ){
If (ord ($ L_rgb {3 * $ j + 0}) = (self: $ COL> 0) & 0xFF & ord ($ L_rgb {3 * $ j + 1}) = (self: $ COL> 8) & 0xFF & ord ($ L_rgb {3 * $ j + 2}) = (self: $ COL> 16) & 0xFF ){
$ L_ext = "! \ XF9 \ x04 ". chr (self: $ DIS <2) + 1 ). chr ($ d> 0) & 0xFF ). chr ($ d> 8) & 0xFF ). chr ($ j ). "\ x0 ";
Break;
}
}
}
Switch ($ L_tmp {0 }){
Case '! ':
$ L_img = substr ($ L_tmp, 8, 10 );
$ L_tmp = substr ($ L_tmp, 18, strlen ($ L_tmp)-18 );
Break;
Case ',':
$ L_img = substr ($ L_tmp, 0, 10 );
$ L_tmp = substr ($ L_tmp, 10, strlen ($ L_tmp)-10 );
Break;
}
If (ord (self ::$ BUF [$ I] {10}) & 0x80 & self ::$ IMG>-1 ){
If ($ G_len = $ L_len ){
If (self: Compare ($ G_rgb, $ L_rgb, $ G_len )){
Self: $ Img. = ($ L_ext. $ L_img. $ L_tmp );
} Else {
$ Byte = ord ($ L_img {9 });
$ Byte | = 0x80;
$ Byte & = 0xF8;
$ Byte | = (ord (self: $ BUF [0] {10}) & 0x07 );
$ L_img {9} = chr ($ byte );
Self: $ Img. = ($ L_ext. $ L_img. $ L_rgb. $ L_tmp );
}
} Else {
$ Byte = ord ($ L_img {9 });
$ Byte | = 0x80;
$ Byte & = 0xF8;
$ Byte | = (ord (self: $ BUF [$ I] {10}) & 0x07 );
$ L_img {9} = chr ($ byte );
Self: $ Img. = ($ L_ext. $ L_img. $ L_rgb. $ L_tmp );
}
} Else {
Self: $ Img. = ($ L_ext. $ L_img. $ L_tmp );
}
Self: $ IMG = 1;
}
Private static function Compare ($ G_Block, $ L_Block, $ Len ){
For ($ I = 0; $ I <$ Len; $ I ++ ){
If ($ G_Block {3 * $ I + 0 }! = $ L_Block {3 * $ I + 0} | $ G_Block {3 * $ I + 1 }! = $ L_Block {3 * $ I + 1} | $ G_Block {3 * $ I + 2 }! = $ L_Block {3 * $ I + 2 }){
Return (0 );
}
}
Return (1 );
}
}
The usage is in the comments at the beginning of the class.