Home  >  Article  >  Backend Development  >  PHP verification code recognition, php verification code_PHP tutorial

PHP verification code recognition, php verification code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:21:271800browse

PHP verification code recognition, php verification code

First of all, I recommend a few articles about verification code recognition, which I think are good

PHP implements verification code recognition (primary)

About bp Neural Grid Recognition Verification Code

1. Ideas

When we encounter a verification code, if we want to recognize it, what do we need to do?
Let’s observe a few verification codes first......


When we observe it with the human eye, we will clearly recognize the characters contained in the verification code. So what is the "recognition mechanism" of the human eye?
It is probably the color difference between the background of the characters in the verification code image. Just imagine, if there is no color difference between the characters and the background, can we judge the verification code? Obviously not.

So, we can start from people.

Start with the color of the image, that is, the RGB information of the image.

RGB色彩模式是工业界的一种颜色标准,是通过对红(R)、绿(G)、蓝(B)三个颜色通道的变化以及它们相互之间的叠加来得到各式各样的颜色的,RGB即是代表红、绿、蓝三个通道的颜色,这个标准几乎包括了人类视力所能感知的所有颜色,是目前运用最广的颜色系统之一。

Define a function to obtain RGB information

<span> 1</span> <span>//</span><span>代码本来是一个类,现在拆开来写的,有可能有不严谨的地方,大家可以看得懂就好了</span>
<span> 2</span> 
<span> 3</span> <span>/*</span>
<span> 4</span> <span> *取得图片路径和图片尺寸
</span><span> 5</span>  <span>*/</span>
<span> 6</span> <span>$this</span>->ImagePath = <span>$Image</span><span>;
</span><span> 7</span> <span>$this</span>->ImageSize = <span>getimagesize</span>(<span>$Image</span><span>);
</span><span> 8</span> 
<span> 9</span> <span>/*</span>
<span>10</span> <span> *获取图像标识符,保存到ImageInfo,只能处理bmp,png,jpg图片
</span><span>11</span> <span> *ImageCreateFromBmp是我自己定义的函数,最后会给出
</span><span>12</span>  <span>*/</span>
<span>13</span> <span>function</span><span> getInfo(){
</span><span>14</span>     <span>$filetype</span> = <span>substr</span>(<span>$this</span>->ImagePath,-3<span>);
</span><span>15</span>     <span>if</span>(<span>$filetype</span> == 'bmp'<span>){
</span><span>16</span>         <span>$this</span>->ImageInfo = <span>$this</span>->ImageCreateFromBmp(<span>$this</span>-><span>ImagePath);
</span><span>17</span>     }<span>elseif</span>(<span>$filetype</span> == 'jpg'<span>){
</span><span>18</span>         <span>$this</span>->ImageInfo = imagecreatefromjpeg(<span>$this</span>-><span>ImagePath);    
</span><span>19</span>     }<span>elseif</span>(<span>$filetype</span> == 'png'<span>){
</span><span>20</span>         <span>$this</span>->ImageInfo = imagecreatefrompng(<span>$this</span>-><span>ImagePath);    
</span><span>21</span> <span>    }
</span><span>22</span> <span>}
</span><span>23</span> 
<span>24</span> <span>/*</span><span>获取图片RGB信息</span><span>*/</span>
<span>25</span> <span>function</span><span> getRgb(){
</span><span>26</span>     <span>$rgbArray</span> = <span>array</span><span>();
</span><span>27</span>     <span>$res</span> = <span>$this</span>-><span>ImageInfo;
</span><span>28</span>     <span>$size</span> = <span>$this</span>-><span>ImageSize;
</span><span>29</span>     <span>$wid</span> = <span>$size</span>['0'<span>];
</span><span>30</span>     <span>$hid</span> = <span>$size</span>['1'<span>];
</span><span>31</span>     <span>for</span>(<span>$i</span>=0; <span>$i</span> < <span>$hid</span>; ++<span>$i</span><span>){
</span><span>32</span>         <span>for</span>(<span>$j</span>=0; <span>$j</span> < <span>$wid</span>; ++<span>$j</span><span>){
</span><span>33</span>             <span>$rgb</span> = imagecolorat(<span>$res</span>,<span>$j</span>,<span>$i</span><span>);
</span><span>34</span>             <span>$rgbArray</span>[<span>$i</span>][<span>$j</span>] = imagecolorsforindex(<span>$res</span>, <span>$rgb</span><span>);
</span><span>35</span> <span>        }
</span><span>36</span> <span>    }
</span><span>37</span>     <span>return</span> <span>$rgbArray</span><span>;
</span><span>38</span> }

2. Binarization

Because the human eye can distinguish the verification code, the RGB information of the verification code will have certain characteristics. At this time, we need to observe it. It is difficult to observe directly printing the RGB array..., there are so many

In the recognition of verification code in PHP (primary article), the author’s judgment is based on

无论验证数字颜色如何变化,该数字的 RGB 值总有一个值小于 125

We first obtain his grayscale and then judge

<span> 1</span> <span>/*</span>
<span> 2</span> <span> *获取灰度信息
</span><span> 3</span>  <span>*/</span>
<span> 4</span> <span>function</span><span> getGray(){
</span><span> 5</span>     <span>$grayArray</span> = <span>array</span><span>();
</span><span> 6</span>     <span>$size</span> = <span>$this</span>-><span>ImageSize;
</span><span> 7</span>     <span>$rgbarray</span> = <span>$this</span>-><span>getRgb();
</span><span> 8</span>     <span>$wid</span> = <span>$size</span>['0'<span>];
</span><span> 9</span>     <span>$hid</span> = <span>$size</span>['1'<span>];
</span><span>10</span>     <span>for</span>(<span>$i</span>=0; <span>$i</span> < <span>$hid</span>; ++<span>$i</span><span>){
</span><span>11</span>         <span>for</span>(<span>$j</span>=0; <span>$j</span> < <span>$wid</span>; ++<span>$j</span><span>){
</span><span>12</span>             <span>$grayArray</span>[<span>$i</span>][<span>$j</span>] = (299*<span>$rgbarray</span>[<span>$i</span>][<span>$j</span>]['red']+587*<span>$rgbarray</span>[<span>$i</span>][<span>$j</span>]['green']+144*<span>$rgbarray</span>[<span>$i</span>][<span>$j</span>]['blue'])/1000<span>;
</span><span>13</span> <span>        }
</span><span>14</span> <span>    }
</span><span>15</span>     <span>return</span> <span>$grayArray</span><span>;
</span><span>16</span> }

Then we print the picture based on the grayscale information. Note that we do not print the grayscale information

<span> 1</span> <span>/*</span><span>根据灰度信息打印图片</span><span>*/</span>
<span> 2</span> <span>function</span><span> printByGray(){
</span><span> 3</span>     <span>$size</span> = <span>$this</span>-><span>ImageSize;    
</span><span> 4</span>     <span>$grayArray</span> = <span>$this</span>-><span>getGray();
</span><span> 5</span>     <span>$wid</span> = <span>$size</span>['0'<span>];
</span><span> 6</span>     <span>$hid</span> = <span>$size</span>['1'<span>];
</span><span> 7</span>     <span>for</span>(<span>$k</span>=0;<span>$k</span><25;<span>$k</span>++<span>){
</span><span> 8</span>         <span>echo</span> <span>$k</span>."\n"<span>;
</span><span> 9</span>         <span>for</span>(<span>$i</span>=0; <span>$i</span> < <span>$hid</span>; ++<span>$i</span><span>){
</span><span>10</span>             <span>for</span>(<span>$j</span>=0; <span>$j</span> < <span>$wid</span>; ++<span>$j</span><span>){
</span><span>11</span>                 <span>if</span>(<span>$grayArray</span>[<span>$i</span>][<span>$j</span>] < <span>$k</span>*10<span>){
</span><span>12</span>                     <span>echo</span> '■'<span>;
</span><span>13</span>                 }<span>else</span><span>{
</span><span>14</span>                     <span>echo</span> '□'<span>;
</span><span>15</span> <span>                }
</span><span>16</span> <span>            }
</span><span>17</span>             <span>echo</span> "|\n"<span>;
</span><span>18</span> <span>        }
</span><span>19</span>         <span>echo</span> "---------------------------------------------------------------------------------------------------------------\n"<span>;
</span><span>20</span> <span>    }
</span><span>21</span>     
<span>22</span> }

Notice that there will be obvious output from $grayArray[$i][$j] b1a54e154d2fd844ba290352821b4cca";
}else{
echo "The verification code is incorrect! a96f0e76ea4396e48604fd3a156fe1ad";
}
}
?>
< ;html>
93f0f5c25f18dab9d176bd4f6de5d30e
b2386ffb911b14667cb8f0f91ea547a7
Verification code example
6e916e0f7d1e588d4f442bf645aedb2f
92259ab03b0951281363b3bbc5077e8e
9c3bca370b5104690d9ef395f2c5f8d1
6c04bd5ca3fcae76e30b72ad730ca86d
649402a5d6a6b449efdf61da09f319a7
076402276aae5dbec7f672f8f4e5cc81
c88b046b147a6f065fab8ac7500f5987
Please enter the verification code: 3d599369861630f4c5165998a9d6c4d3
750110aaf30b92142366eec6a125c49a
f5a47148e367a6035fd7a2faa965022e
36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e

Note that the address of the image is written to the file address of the image generated by PHP

check.php is used to generate the image

session_start();
header('content-type:image/jpeg');
$image_width=70;
$image_height=25;
$str="";
$arr =array(1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G',' H','I','J','K','L','M','N','P','Q','R','S','T','U' ,'V','W','X','Y','Z');
for ($i =0; $i...the rest of the full text>>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/857272.htmlTechArticlePHP verification code identification, php verification code first recommend a few articles about verification code identification, I think it is a good php implementation Recognition of verification code (primary article) About bp neural grid identification of verification code 1. Thoughts...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn