Elegant verification code

Source: Internet
Author: User
Elegant verification code
Import the font by yourself. you can modify the font as needed.
We recommend that you use bold fonts.

  1. Class Imagecode {
  2. Private $ width;
  3. Private $ height;
  4. Private $ counts;
  5. Private $ distrubcode;
  6. Private $ fonturl;
  7. Private $ fonturlbg;
  8. Private $ session;
  9. /**
  10. * Enter description here...
  11. *
  12. * @ Param unknown_type $ width
  13. * @ Param unknown_type $ height
  14. * @ Param unknown_type $ Count
  15. * @ Param unknown_type $ distrubcode random character range
  16. * @ Param unknown_type $ fonturl serious code body
  17. * @ Param unknown_type $ fonturlbg disturbing character font
  18. */
  19. Function _ construct ($ width = 120, $ height = 30, $ counts = 5, $ distrubcode = "1235467890 qwertyuipkjhgfdaszxcvbnm", $ fonturl = "msyhbd. ttf ", $ fonturlbg = 'F. ttf '){
  20. $ This-> width = $ width;
  21. $ This-> height = $ height;
  22. $ This-> counts = $ counts;
  23. $ This-> distrubcode = $ distrubcode;
  24. $ This-> fonturl = $ fonturl;
  25. $ This-> fonturlbg = $ fonturlbg? $ Fonturlbg: $ fonturl; // used when no interfering font is set
  26. $ This-> session = $ this-> sessioncode ();
  27. Session_start ();
  28. $ _ SESSION ['code'] = $ this-> session;
  29. }
  30. Function imageout (){
  31. $ Im = $ this-> createimagesource ();
  32. $ This-> setbackgroundcolor ($ im );
  33. $ This-> set_code ($ im );
  34. $ This-> setdistrubecode ($ im );
  35. ImageGIF ($ im );
  36. ImageDestroy ($ im );
  37. }
  38. Private function createimagesource (){
  39. Return imagecreate ($ this-> width, $ this-> height );
  40. }
  41. Private function setbackgroundcolor ($ im ){
  42. $ Bgcolor = ImageColorAllocate ($ im, rand (200,255), rand (200,255), rand (200,255); // ± 3? ° ?? ?
  43. Imagefill ($ im, 0, 0, $ bgcolor );
  44. }
  45. Private function setdistrubecode ($ im ){
  46. $ Count_h = $ this-> height;
  47. $ Cou = floor ($ count_h * 2 );
  48. For ($ I = 0; $ I <$ cou; $ I ++ ){
  49. $ X = rand (0, $ this-> width );
  50. $ Y = rand (0, $ this-> height );
  51. $ Jiaodu = rand (0,360 );
  52. $ Fontsize = rand (8, 15 );
  53. $ Fonturl = $ this-> fonturlbg;
  54. $ Originalcode = $ this-> distrubcode;
  55. $ Countdistrub = strlen ($ originalcode );
  56. $ Dscode = $ originalcode [rand (0, $ countdistrub-1)];
  57. $ Color = ImageColorAllocate ($ im, rand (40,140), rand (40,140), rand (40,140); // interfere with text color
  58. Imagettftext ($ im, $ fontsize, $ jiaodu, $ x, $ y, $ color, $ fonturl, $ dscode );
  59. }
  60. }
  61. Private function set_code ($ im ){
  62. $ Width = $ this-> width;
  63. $ Counts = $ this-> counts;
  64. $ Height = $ this-> height;
  65. $ Scode = $ this-> session;
  66. $ Y = floor ($ height/2) + floor ($ height/4 );
  67. $ Fontsize = rand (30, 35 );
  68. $ Fonturl = $ this-> fonturl;
  69. $ Counts = $ this-> counts;
  70. For ($ I = 0; $ I <$ counts; $ I ++ ){
  71. $ Char = $ scode [$ I];
  72. $ X = floor ($ width/$ counts) * $ I + 8;
  73. $ Jiaodu = rand (-20, 30 );
  74. $ Color = ImageColorAllocate ($ im, rand (0,200), rand (50,200), rand (100,240); // text color
  75. Imagettftext ($ im, $ fontsize, $ jiaodu, $ x, $ y, $ color, $ fonturl, $ char );
  76. }
  77. }
  78. Private function sessioncode (){
  79. $ Originalcode = $ this-> distrubcode;
  80. $ Countdistrub = strlen ($ originalcode );
  81. $ _ Dscode = "";
  82. $ Counts = $ this-> counts;
  83. For ($ j = 0; $ j <$ counts; $ j ++ ){
  84. $ Dscode = $ originalcode [rand (0, $ countdistrub-1)];
  85. $ _ Dscode. = $ dscode;
  86. }
  87. Return $ _ dscode;
  88. }
  89. }
  90. Header ("Content-type: image/GIF ");
  91. $ Imagecode = new Imagecode (160,50 );
  92. $ Imagecode-> imageout ();

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.