I received a job today. I recognized a simple verification code. This verification code is straightforward. It has very little interference information. So just get the information for comparison. It is easy to identify.
But everything won’t be so smooth. Maybe it’s for me. I think what I do is very tortuous…
This captcha image is generated using ASP. It is, of course, a BMP format.
But for PHP, this is a soft injury because PHP’s GD library does not include BMP at all. Although there is wbmp, it is still different.
So I googled it. The result made me feel a bit of joy. There is a ready-made class that recognizes 256-color BMP. It is very exciting. It is not a matter of white money!!
The result is disappointing. The 256-color BMP and 24-bit color are two concepts. This means that I have to rewrite it..
It seems that there are no ready-made libraries and functions. You can only write it yourself. Analyze the BMP header with the most primitive open. A byte read.
All went to google BMP 24-bit color head analysis.. so long article… immediately disappointed..
So boring to go through the manual.. Look at the imagecreatefromgif imagecreatefromjpeg, imagecreatefrompng, imagecreatefromwbmp and so on. There is no imagecreatefrombmp
So suddenly whimsy. Go to google about imagecreatefrombmp what will be the result. Sure enough, I did not expect. There have been people who wrote this function..
-
function imagecreatefrombmp($file)
- {
- global $CurrentBit, $echoMode;
-
- $f=fopen($file,“r”);
- $Header=fread($f,2);
-
- if($Header==“BM”)
- {
- $Size=freaddword($f);
- $Reserved1=freadword($f);
- $Reserved2=freadword($f);
- $FirstByteOfImage=freaddword($f);
-
- $SizeBITMAPINFOHEADER=freaddword($f);
- $Width=freaddword($f);
- $Height=freaddword($f);
- $biPlanes=freadword($f);
- $biBitCount=freadword($f);
- $RLECompression=freaddword($f);
- $WidthxHeight=freaddword($f);
- $biXPelsPerMeter=freaddword($f);
- $biYPelsPerMeter=freaddword($f);
- $NumberOfPalettesUsed=freaddword($f);
- $NumberOfImportantColors=freaddword($f);
-
- if($biBitCount<24)
- {
- $img=imagecreate($Width,$Height);
- $Colors=pow(2,$biBitCount);
- for($p=0;$p<$Colors;$p++)
- {
- $B=freadbyte($f);
- $G=freadbyte($f);
- $R=freadbyte($f);
- $Reserved=freadbyte($f);
- $Palette[]=imagecolorallocate($img,$R,$G,$B);
- };
-
-
-
-
- if($RLECompression==0)
- {
- $Zbytek=(4-ceil(($Width/(8/$biBitCount)))%4)%4;
-
- for($y=$Height-1;$y>=0;$y–)
- {
- $CurrentBit=0;
- for($x=0;$x<$Width;$x++)
- {
- $C=freadbits($f,$biBitCount);
- imagesetpixel($img,$x,$y,$Palette[$C]);
- };
- if($CurrentBit!=0) {freadbyte($f);};
- for($g=0;$g<$Zbytek;$g++)
- freadbyte($f);
- };
-
- };
- };
-
-
- if($RLECompression==1)
- {
- $y=$Height;
-
- $pocetb=0;
-
- while(true)
- {
- $ and -;
- $prefix=freadbyte($f);
- $suffix=freadbyte($f);
- $pocetb+=2;
-
- $echoit=false;
-
- if($echoit)echo “Prefix: $prefix Suffix: $suffix<BR>”;
- if(($prefix==0)and($suffix==1)) break;
- if(feof($f)) break;
-
- while(!(($prefix==0)and($suffix==0)))
- {
- if($prefix==0)
- {
- $pocet=$suffix;
- $Data.=fread($f,$pocet);
- $ pocetb + = $ number ;
- if($pocetb%2==1) {freadbyte($f); $pocetb++;};
- };
- if($prefix>0)
- {
- $ count = $ prefix ;
- for($r=0;$r<$pocet;$r++)
- $Data.=chr($suffix);
- };
- $prefix=freadbyte($f);
- $suffix=freadbyte($f);
- $pocetb+=2;
- if($echoit) echo “Prefix: $prefix Suffix: $suffix<BR>”;
- };
-
- for($x=0;$x<strlen($Data);$x++)
- {
- imagesetpixel($img,$x,$y,$Palette[ord($Data[$x])]);
- };
- $Data=“”;
-
- };
-
- };
-
-
- if($RLECompression==2)
- {
- $y=$Height;
- $pocetb=0;
-
-
- while(true)
- {
-
- $ and -;
- $prefix=freadbyte($f);
- $suffix=freadbyte($f);
- $pocetb+=2;
-
- $echoit=false;
-
- if($echoit)echo “Prefix: $prefix Suffix: $suffix<BR>”;
- if(($prefix==0)and($suffix==1)) break;
- if(feof($f)) break;
-
- while(!(($prefix==0)and($suffix==0)))
- {
- if($prefix==0)
- {
- $pocet=$suffix;
-
- $CurrentBit=0;
- for($h=0;$h<$pocet;$h++)
- $Data.=chr(freadbits($f,4));
- if($CurrentBit!=0) freadbits($f,4);
- $ pocetb + = ceil (( $ number / 2));
- if($pocetb%2==1) {freadbyte($f); $pocetb++;};
- };
- if($prefix>0)
- {
- $ count = $ prefix ;
- $i=0;
- for($r=0;$r<$pocet;$r++)
- {
- if($i%2==0)
- {
- $Data.=chr($suffix%16);
- }
- else
- {
- $Data.=chr(floor($suffix/16));
- };
- $i++;
- };
- };
- $prefix=freadbyte($f);
- $suffix=freadbyte($f);
- $pocetb+=2;
- if($echoit) echo “Prefix: $prefix Suffix: $suffix<BR>”;
- };
-
- for($x=0;$x<strlen($Data);$x++)
- {
- imagesetpixel($img,$x,$y,$Palette[ord($Data[$x])]);
- };
- $Data=“”;
-
- };
-
- };
-
-
- if($biBitCount==24)
- {
- $img=imagecreatetruecolor($Width,$Height);
- $Zbytek=$Width%4;
-
- for($y=$Height-1;$y>=0;$y–)
- {
- for($x=0;$x<$Width;$x++)
- {
- $B=freadbyte($f);
- $G=freadbyte($f);
- $R=freadbyte($f);
- $color=imagecolorexact($img,$R,$G,$B);
- if($color==-1) $color=imagecolorallocate($img,$R,$G,$B);
- imagesetpixel($img,$x,$y,$color);
- }
- for($z=0;$z<$Zbytek;$z++)
- freadbyte($f);
- };
- };
- return $img;
-
- };
-
-
- fclose($f);
-
-
- };
-
-
function freadbyte($f)
- {
- return ord(fread($f,1));
- };
-
-
function freadword($f)
- {
- $b1=freadbyte($f);
- $b2=freadbyte($f);
- return $b2*256+$b1;
- };
-
-
function freaddword($f)
- {
- $b1=freadword($f);
- $b2=freadword($f);
- return $b2*65536+$b1;
- };
The process of this function is like this: Open the file with fopen. Read one byte and one byte. Read the BMP header first. Then read the pixel line by line. Then use imagecreatetruecolor to create an image. Just read the pixel. Then draw one by one to the image you just created.
Finally return the resource of this image.
It is estimated that everyone is like me. I was scared by such a long function at the beginning. In fact, don’t be afraid. Analyze it. We want to analyze the 24-bit color BMP. So most of the above functions do not use .if($biBitCount= =24) The following are the key:
$B=freadbyte($f);
$G=freadbyte($f);
$R=freadbyte($f);
These three lines are for reading pixels. All we need is to put each pixel into an array. This is easy to manipulate.
$array[] = sprintf(“%03d”,$R).sprintf(“%03d”,$G).sprintf(“%03d”,$B);
So we put each pixel in the array of $array. But this is not enough. Because we have to identify the pixel. So I think so. Change the white (255255255) to 0. The other colors are changed to 1. This is because my verification code is relatively simple. And the interference color is very small..
The resulting $array is similar to this.
0001111000 0001111000 0001111000 0000101000
0010000100 0010000100 0010000100 0000100000
0010000100 0010000100 0010000100 0000100000
0010110100 0000000100 0000000100 0000100000
0010110100 0000101000 0000000100 0000100000
0010110100 0000110000 0011000100 0000010000
0010110100 0000001000 0010111000 0000010000
0010000100 0010000100 0010000000 0010001000
0011000100 0010000110 0010000000 0010001000
0001111000 0001111000 0011111100 0011111100
Of course, the reason why this is formed is what I processed when I output it.
It is easy to see clearly. This is 0357..
Some people will ask why it is down? This is because the BMP is stored upside down when storing images. Compare BT.
But don’t worry about this. We don’t have to give him a hard time coming over. Because we are doing comparisons. This comparison code is also upside down.
In order to more clearly explain how this array is stored in white. I will give you a detailed description. Just follow the example above.
Array subscript 0 1 2 3 4 5 6 7 8 9 10….. 39
Storage contents 0 0 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 1 0 1 0 0 0 First line
Array subscript 40 41 …. …79Store
contents 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 second line
I will not write it for the sake of omission. It is easy to see. This is a one-dimensional array. There are 399 elements in total.. But in order to identify the verification code, we have to take each of them out.
0-9 40-49 80-89 … is a group of
10-19 50-59 90-99 … for a group of
20-29 60-69 100-109… for a group of
30-39 70-79 110-119.. As a group
You can write your own algorithm:
After sorting this out, it is an array of 4 elements.
Each element has 100 digits of 0 and 1.
The element is taken out. Who is it to compare with?? Yes. We have to find a reference object..
Finding the reference object is physical strength..
Return to the above process.. Constantly refresh the verification code.. until 0 -9 has appeared. For each. Write down his appearance, say 0 is like this
0001111000
0010000100
0010000100
0010110100
0010110100
0010110100
0010110100
0010000100
0011000100
0001111000
It becomes a string. 0 is like this. 0001111000001000010000100001000010110100001011010000101101000010110100001000010000110001000001111000
is also placed in the above order in a row.
After 0-9 is written down, we can put it in an array, called $key.
This is a one-to-one correspondence with the elements we generated.
The next step is to start comparing.
Looping is inevitable. The trick is to compare the similar_text and reference objects one by one. Put the Acacia percentage into an array.
Then take the largest percentage of the array. The corresponding index value (this index value is not automatically generated) Is the number we have identified.
The following code value describes the important part
Foreach ($keys as $key => $value) { //$key is the comparison reference group
similar_text($value, $validValue, $p);
$maxArr[$key] = $p; // put all the odds Go to the array
}
and then take out the largest index value of $maxArr. This is the number we identified.
At this point, the verification code is considered complete.
But when deployed to the user, I found that his machine used fopen to open the remote URL. Only 30% chance is obtained. Other cases are HTTP Request failed…
Many methods have been changed or not. The result is my solution. .
using CURL to save local codes. then fopen to open the local images 100% OK it ..
As for the detailed code, I will not post it. Because the code is sold to others. In order to protect the interests of others, so don’t open your mouth with me. I have already given the idea. I believe you can write it.