PHP verification code recognition

Source: Internet
Author: User

 

Our most common Verification Code

One or four digits, a random string of one digit, and the original verification code. The verification function is almost zero.
2. Random image verification code. The characters in the image are relatively regular, some may be added with some random interferon, and some may be random character colors. The verification effect is better than the previous one. People who do not have the knowledge of basic graphics and Images cannot break through!
3. Random Numbers in various image formats + random uppercase English letters + random interference pixels + random positions.
4. Chinese characters are currently the latest verification codes registered. They are randomly generated, making them more difficult to create and affecting user experience. Therefore, there are usually few applications.

For the sake of simplicity, the main object we described here is 2nd types. Let's take a look at several images of such verification codes that are common on the Internet.

These four styles basically represent the verification code type mentioned in 2. Initially, it seems that the first image is the easiest to crack, the second, the third, and the fourth are the most difficult.
What is the actual situation? In fact, these three types of images are equally difficult to crack.
The first image is the easiest. The image background and numbers are in the same color, regular characters, and uniform character positions.
The second picture seems not easy. In fact, we will carefully study the rules, the background color and the interferon, no matter how they change, verify that the characters are regular and the colors are the same, so it is very easy to exclude interferon, exclude all non-character pigments.
The third image looks more complex. The color of the verification character is also changing, and the color of each character is also different, except that the background color and interferon are constantly changing. It seems that this verification code cannot be broken through. In this article, we will take this type of verification code as an example to describe the fourth picture.
In addition to the features mentioned in the third image, the fourth image also adds two linear interference rates to the text. In fact, it seems difficult and easy to remove.

Verification code recognition is generally divided into the following steps:
1. Extract the Model
2. binarization
3. Computing Features
4. comparison sample

1: extract the Model
After all, the verification code is not a professional OCR recognition. Because the verification codes of different websites are different, the most common method is to create a signature library for this verification code. When downloading the dashboard, We need to download several more images to make these images contain all the characters. The letters here are only images. Therefore, we only need to collect images including 0-9.

2: binarization
Binarization means that each pixel in the verification number on the image is represented by 1 in a number, and the other part is represented by 0. In this way, you can calculate each digital model, record these fonts, and use them as keys.

3: Computing Features
Binarization the image to be recognized to obtain the image features.

4: control sample
Compare the Image Signature and verification code pattern in step 3 to obtain the number on the verification image.
Currently, the verification code can be identified as 100%.
After completing the above steps, you may have said that you have not discovered how to retrieve interferon! In fact, the method to retrieve interferon is very simple. An important feature of interferon is that it does not affect the Display Effect of the Verification Code. Therefore, the RGB value of interferon may be lower than or higher than a specific value, for example, in the image I gave, the RGB values of interferon will not exceed 125, so we can easily remove interferon.

PHP code

  1. <? PHP
  2. Define ('word _ width', 9 );
  3. Define ('word _ HT ', 13 );
  4. Define ('offset _ x', 7 );
  5. Define ('offset _ y', 3 );
  6. Define ('word _ spacing', 4 );
  7. Class valite
  8. {
  9. Public Function setimage ($ image)
  10. {
  11. $ This-> ImagePath = $ image;
  12. }
  13. Public Function getdata ()
  14. {
  15. Return $ data;
  16. }
  17. Public Function getresult ()
  18. {
  19. Return $ dataarray;
  20. }
  21. Public Function gethec ()
  22. {
  23. $ Res = imagecreatefromjpeg ($ this-> ImagePath );
  24. $ Size = getimagesize ($ this-> ImagePath );
  25. $ DATA = array ();
  26. For ($ I = 0; $ I <$ size [1]; ++ $ I)
  27. {
  28. For ($ J = 0; $ j <$ size [0]; ++ $ J)
  29. {
  30. $ RGB = imagecolorat ($ res, $ J, $ I );
  31. $ Rgbarray = imagecolorsforindex ($ res, $ RGB );
  32. If ($ rgbarray ['red'] <125 | $ rgbarray ['green'] <125
  33. | $ Rgbarray ['blue'] <125)
  34. {
  35. $ Data [$ I] [$ J] = 1;
  36. } Else {
  37. $ Data [$ I] [$ J] = 0;
  38. }
  39. }
  40. }
  41. $ This-> dataarray = $ data;
  42. $ This-> imagesize = $ size;
  43. }
  44. Public Function run ()
  45. {
  46. $ Result = "";
  47. // Search for 4 numbers
  48. $ DATA = array ("","","","");
  49. For ($ I = 0; $ I <4; ++ $ I)
  50. {
  51. $ X = ($ I * (word_width + word_spacing) + offset_x;
  52. $ Y = offset_y;
  53. For ($ H = $ Y; $ H <(offset_y + word_hight); ++ $ H)
  54. {
  55. For ($ W = $ X; $ W <($ x + word_width); ++ $ W)
  56. {
  57. $ Data [$ I]. = $ this-> dataarray [$ H] [$ w];
  58. }
  59. }
  60. }
  61. // Match the keyword
  62. Foreach ($ data as $ numkey => $ numstring)
  63. {
  64. $ Max = 0.0;
  65. $ Num = 0;
  66. Foreach ($ this-> keys as $ key => $ value)
  67. {
  68. $ Percent = 0.0;
  69. Similar_text ($ value, $ numstring, $ percent );
  70. If (intval ($ percent)> $ max)
  71. {
  72. $ Max = $ percent;
  73. $ Num = $ key;
  74. If (intval ($ percent)> 95)
  75. Break;
  76. }
  77. }
  78. $ Result. = $ num;
  79. }
  80. $ This-> DATA = $ result;
  81. // Find the best matching number
  82. Return $ result;
  83. }
  84. Public Function draw ()
  85. {
  86. For ($ I = 0; $ I <$ this-> imagesize [1]; ++ $ I)
  87. {
  88. For ($ J = 0; $ j <$ this-> imagesize [0]; ++ $ J)
  89. {
  90. Echo $ this-> dataarray [$ I] [$ J];
  91. }
  92. Echo "\ n ";
  93. }
  94. }
  95. Public Function _ construct ()
  96. {
  97. $ This-> keys = array (
  98. '0' => '123 ',
  99. '1' => '123 ',
  100. '2' => '123 ',
  101. '3' => '123 ',
  102. '4' => '123 ',
  103. '5' =>
  104. '123 ',
  105. '6' => '123 ',
  106. '7' => '123 ',
  107. '8' => '123 ',
  108. '9' => '123 ',
  109. );
  110. }
  111. Protected $ ImagePath;
  112. Protected $ dataarray;
  113. Protected $ imagesize;
  114. Protected $ data;
  115. Protected $ keys;
  116. Protected $ numstringarray;
  117. }
  118. ?>

Copy code

Test. php

  1. <? PHP
  2. Include ('valite. php ');
  3. $ Valite = new valite ();
  4. $ Valite-> setimage('code.jpeg ');
  5. $ Valite-> gethec ();
  6. $ ERT = $ valite-> Run ();
  7. Echo $ ERT;
  8. Echo '<br> <br> ';
  9. ?>

Copy code

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.