A very useful verification code tool

Source: Internet
Author: User
A very useful verification code tool
<无详细内容>

  1. /**
  2. * Verification code
  3. */
  4. Class Base_Tool_Verify {
  5. /**
  6. * Verification code generation method
  7. */
  8. Public static function createCode (){
  9. // Generate a verification code Image
  10. // All numbers
  11. $ Str = "D, B, 2, 3, 4, 5, 6, 8, 9, A, B, C, D, E, F, G, H, W, J, K, M, m, N, K, P, Q, R, S, T, U, V, W, X, Y, Z "; // you can add or delete the characters to be displayed.
  12. $ List = explode (",", $ str );
  13. $ Cmax = count ($ list)-1;
  14. $ VerifyCode = '';
  15. For ($ I = 0; $ I <4; $ I ++ ){
  16. $ Randnum = mt_rand (0, $ cmax );
  17. $ VerifyCode. = $ list [$ randnum]; // extract the characters and combine them into the verification code characters.
  18. }
  19. $ _ SESSION ['code'] = $ verifyCode; // put the characters in the SESSION
  20. $ Im = imagecreate (); // generate an image
  21. $ Black = imagecolorallocate ($ im, 0, 0, 0); // set the color of the bar and the following three items
  22. $ White = imagecolorallocate ($ im, 244,249,244 );
  23. $ Gray = imagecolorallocate ($ im, rand (200,255), rand (200,255), rand (200,255 ));
  24. $ Red = imagecolorallocate ($ im, rand (200,255), rand (200,255), rand (200,255 ));
  25. $ Rand_color = imagecolorallocate ($ im, rand (0,255), rand (0,255), rand (0,255 ));
  26. $ Rand_color2 = imagecolorallocate ($ im, rand (0,255), rand (0,255), rand (0,255 ));
  27. Imagefill ($ im, $ white); // fill the color of the image
  28. $ Pix = imagecolorallocate ($ im, rand (0,255), rand (0,255), rand (0,255 ));
  29. Mt_srand ();
  30. For ($ I = 0; I I <100; $ I ++)
  31. {
  32. Imagesetpixel ($ im, mt_rand (0,180), mt_rand (), $ pix );
  33. }
  34. Imageline ($ im, 0, rand (0, 28), 80, rand (0, 28), $ rand_color );
  35. Imageline ($ im, 0, rand (0, 28), 80, rand (0, 28), $ rand_color2 );
  36. // Print the verification code into the image
  37. $ Mycolor = imagecolorallocate ($ im, 0, 78,152 );
  38. $ Path = API_PATH.DS. 'base'. DS. 'tool ';
  39. Putenv ('gdfontpath = '. $ path );
  40. $ Font = 'simhei. ttf ';
  41. $ Arrstr = str_split ($ verifyCode );
  42. Imagettftext ($ im, 20, rand (0, 50), 10, rand (20, 30), $ mycolor, $ font, $ arrstr [0]);
  43. Imagettftext ($ im, 15, rand (0, 50), 20, rand (20, 30), $ mycolor, $ font, $ arrstr [1]);
  44. Imagettftext ($ im, 15, rand (0, 50), 30, rand (20, 30), $ mycolor, $ font, $ arrstr [2]);
  45. Imagettftext ($ im, 15, rand (0, 50), 40, rand (20, 30), $ mycolor, $ font, $ arrstr [3]);
  46. $ Font2 = imagecolorallocate ($ im, 41,163,238 );
  47. Imagerectangle ($ im, 0, 0, 1, 1, $ font2 );
  48. Imagepng ($ im );
  49. Imagedestroy ($ im );
  50. }
  51. }

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.