Php image Chinese verification code

Source: Internet
Author: User

Php image Chinese verification code

<? Php
Session_start ();

$ Vi = new vCodeImage;
$ Vi-> SetImage );

Class vCodeImage {
Var $ mode; // 1: digit mode; 2: Letter mode; 3: DIGIT/letter mode; others: DIGIT/letter optimized mode
Var $ v_num; // Number of verification codes
Var $ img_w; // the image width of the verification code.
Var $ img_h; // verification code Image height
Var $ int_pixel_num; // Number of interference pixels
Var $ int_line_num; // Number of interfering lines
Var $ font_dir; // relative path of the font file
Var $ border; // image border
Var $ borderColor; // image border color

Function SetImage ($ made, $ v_num, $ img_w, $ img_h, $ int_pixel_num, $ int_line_num, $ font_dir = '.. /font ', $ border = true, $ borderColor = '2014, 255,200 '){
If (! Isset ($ _ SESSION ['vcode']) {
Session_register ('vcode ');
  }
$ _ SESSION ['vcode'] = "";
 
$ This-> mode = $ made;
$ This-> v_num = $ v_num;
$ This-> img_w = $ img_w;
$ This-> img_h = $ img_h;
$ This-> int_pixel_num = $ int_pixel_num;
$ This-> int_line_num = $ int_line_num;
$ This-> font_dir = $ font_dir;
$ This-> border = $ border;
$ This-> borderColor = $ borderColor;
$ This-> GenerateImage ();
 }

Function GetChar ($ mode ){
If ($ mode = "1 "){
$ Ychar = ";
  }
Else if ($ mode = "2 "){
$ Ychar = "A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, s, T, U, V, W, X, Y, Z ";
  }
Else if ($ mode = "3 "){
$ Ychar = ", A, B, C, D, E, F, G, H, I, J, K, L, M, n, O, P, Q, R, S, T, U, V, W, X, Y, Z ";
  }
Else
$ Ychar = "3, 4, 5, 6, 7, 8, 9, A, B, C, D, H, K, P, R, S, T, W, X, Y ";
Return $ ychar;
 }
 
Function RandColor ($ rs, $ re, $ gs, $ ge, $ bs, $ be ){
$ R = mt_rand ($ rs, $ re );
$ G = mt_rand ($ gs, $ ge );
$ B = mt_rand ($ bs, $ be );
Return array ($ r, $ g, $ B );
 }
 
Function GenerateImage (){
$ Im = imagecreate ($ this-> img_w, $ this-> img_h );

$ Black = imagecolorallocate ($ im, 0, 0 );
$ White = imagecolorallocate ($ im, 255,255,255 );
$ Bgcolor = imagecolorallocate ($ im, 250,250,250 );

Imagefill ($ im, 0, 0, $ bgcolor );

$ Fonts = ScanDir ($ this-> font_dir );
$ Fmax = count ($ fonts)-2;

$ Ychar = $ this-> GetChar ($ this-> mode );
$ List = explode (",", $ ychar );

$ X = mt_rand (2, $ this-> img_w/($ this-> v_num + 2 ));
$ Cmax = count ($ list)-1;

$ V_code = '';

For ($ I = 0; $ I <$ this-> v_num; $ I ++) // verification code
  {
$ Randnum = mt_rand (0, $ cmax );
$ This_char = $ list [$ randnum];
$ V_code. = $ this_char;
$ Size = mt_rand (intval ($ this-> img_w/5), intval ($ this-> img_w/4 ));
$ Angle = mt_rand (-20, 20 );
$ Y = mt_rand ($ size + 2), ($ this-> img_h-2 ));
If ($ this-> border)
$ Y = mt_rand ($ size + 3), ($ this-> img_h-3 ));
$ Rand_color = $ this-> RandColor (0,200, 0,100, 0,250 );
$ Randcolor = imagecolorallocate ($ im, $ rand_color [0], $ rand_color [1], $ rand_color [2]);
$ Fontrand = mt_rand (2, $ fmax );
$ Font = "$ this-> font_dir/". $ fonts [$ fontrand];
Imagettftext ($ im, $ size, $ angle, $ x, $ y, $ randcolor, $ font, $ this_char );
$ X = $ x + intval ($ this-> img_w/($ this-> v_num + 1 ));
  }

For ($ I = 0; $ I <$ this-> int_pixel_num; $ I ++) {// interfere with pixels
$ Rand_color = $ this-> RandColor (50,250, 0,250, 50,250 );
$ Rand_color_pixel = imagecolorallocate ($ im, $ rand_color [0], $ rand_color [1], $ rand_color [2]);
Imagesetpixel ($ im, mt_rand () % $ this-> img_w, mt_rand () % $ this-> img_h, $ rand_color_pixel );
  }

For ($ I = 0; $ I <$ this-> int_line_num; $ I ++) {// interference line
$ Rand_color = $ this-> RandColor (0,250, 0,250, 0,250 );
$ Rand_color_line = imagecolorallocate ($ im, $ rand_color [0], $ rand_color [1], $ rand_color [2]);
Imageline ($ im, mt_rand (0, intval ($ this-> img_w/3), mt_rand (0, $ this-> img_h ), mt_rand (intval ($ this-> img_w-($ this-> img_w/3), $ this-> img_w), mt_rand (0, $ this-> img_h ), $ rand_color_line );
  }

If ($ this-> border) // draw a border
  {
If (preg_match ("/^ \ d {1, 3}, \ d {1, 3}, \ d {1, 3} $/", $ this-> borderColor )){
$ BorderColor = explode (',', $ this-> borderColor );
   }
$ Border_color_line = imagecolorallocate ($ im, $ borderColor [0], $ borderColor [1], $ borderColor [2]);
Imageline ($ im, 0, 0, $ this-> img_w, 0, $ border_color_line); //
Imageline ($ im, 0, 0, 0, $ this-> img_h, $ border_color_line); // left vertical
Imageline ($ im, 0, $ this-> img_h-1, $ this-> img_w, $ this-> img_h-1, $ border_color_line); // lower horizontal
Imageline ($ im, $ this-> img_w-1, 0, $ this-> img_w-1, $ this-> img_h, $ border_color_line); // right portrait
  }

Imageantialias ($ im, true); // Anti-aliasing

$ Time = time ();
$ _ SESSION ['vcode'] = $ v_code. "|". $ time; // returns the negative value of the verification code and generation time to $ _ SESSION [vCode].

// Generate an image for the browser
If (function_exists ("imagegif ")){
Header ("Content-type: image/gif ");
Imagegif ($ im );
  }
Elseif (function_exists ("imagepng ")){
Header ("Content-type: image/png ");
Imagepng ($ im );
  }
Elseif (function_exists ("imagejpeg ")){
Header ("Content-type: image/jpeg ");
Imagejpeg ($ im, "", 80 );
  }
Elseif (function_exists ("imagewbmp ")){
Header ("Content-type: image/vnd. wap. wbmp ");
Imagewbmp ($ im );
  }
Else
Die ("No Image Support On This Server! ");
 
Imagedestroy ($ im );
 }
}
?>

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.