- Background color is random
- Color figures is always the same
- X position of various figures
- Y position remains unchanged figures
What it means to "break a captcha? Basically, need to display text in the figures of each image (of course, are random). But look at the code. Php to generate images:
<? Php
header ('Content-type: image / png');
$ Img = imagecreate (239, 25);
$ R = rand (1, 255);
$ G = rand (1, 255);
$ B = rand (1, 255);
$ L1 = rand (0, 4);
$ L2 = rand (0, 4);
$ L3 = rand (0, 4);
$ L4 = rand (0, 4);
$ L5 = rand (0, 4);
$ Poz1 = rand (0, 48);
$ Poz2 poz1 + = $ rand (9, 48);
$ Poz3 poz2 + = $ rand (9, 48);
$ Poz4 poz3 + = $ rand (9, 48);
$ Poz5 poz4 + = $ rand (9, 48);
$ Background = imagecolorallocate ($ img, $ r, $ g, $ B);
$ Color = imagecolorallocate ($ img, 100, 100, 100);
imagechar ($ img, 7, $ poz1, 6, $ l1, $ color);
imagechar ($ img, 7, $ poz2, 6, $ l2, $ color);
imagechar ($ img, 7, $ poz3, 6, $ l3, $ color);
imagechar ($ img, 7, $ poz4, 6, $ L4, $ color);
imagechar ($ img, 7, $ poz5, 6, $ l5, $ color);
imagepng ($ img);
?>
1) Background - white figures - black.
The first step is to remove color. Image background will become white and the figures will be black. To see the PHP code:
$ Img = imagecreatefrompng (http://site.com/captcha/cap1.png ");
$ White = imagecolorallocate ($ img, 255, 255, 255);
$ Black = imagecolorallocate ($ img, 0, 0, 0);
/ / Simplify image
/ / White background
/ / Written in black
for ($ i = 0, $ i <239; $ i + +)
{
for ($ j = 0, $ j <25; $ j + +)
{
$ Pixel = imagecolorat ($ img, $ i, $ j);
$ Rgb = imagecolorsforindex ($ img, $ pixel);
if ($ rgb ['red']! = 100 & & $ rgb ['green']! = 100 & & $ rgb ['blue']! = 100)
{
imagesetpixel ($ img, $ i, $ j, $ white);
}
else
{
imagesetpixel ($ img, $ i, $ j, $ black);
}
}
}
/ / Finished simplification
cap1.png image is to be "broken." Note that the numbers of code (which r, g, b = 100) are set to black, rest white pixel. Now the image is black and white.2) Find the rectangle figures.
Like I said, random numbers are placed on the axis Ox. The idea is that we need to divide the original image (the mare/cap1.png) into 5 smaller files, each containing only a number! An important detail is that the default font used imagechar function () is monospaced (courier far as I know), and any number "written" font size = 7 can be framed in a rectangle of length 9.
So for example, to find the first digit, you must find first black pixel (looking from left to right) and retain them X'ul. Then the rectangle extracted from X to X 8. PHP Code:
/ / Find the first digit rectangle
for ($ i = 0, $ i <239; $ i + +)
{
for ($ j = 0, $ j <25; $ j + +)
{
$ Pixel = imagecolorat ($ img, $ i, $ j);
$ Rgb = imagecolorsforindex ($ img, $ pixel);
if ($ rgb ['red'] == 0 & & $ rgb ['green'] == 0 & & $ rgb ['blue'] == 0)
{
$ Istart = $ i, $ i $ IFIN = 8; break 2;
}
}
}
/ / Find the rectangle of figure 2
for ($ i = $ 1 IFIN; $ i <239; $ i + +)
{
for ($ j = 0, $ j <25; $ j + +)
{
$ Pixel = imagecolorat ($ img, $ i, $ j);
$ Rgb = imagecolorsforindex ($ img, $ pixel);
if ($ rgb ['red'] == 0 & & $ rgb ['green'] == 0 & & $ rgb ['blue'] == 0)
{
$ Iistart = $ i; iifinal $ 8 = $ i; break 2;
}
}
}
/ / Find the rectangle of figure 3
for ($ i = $ iifinal +1, $ i <239; $ i + +)
{
for ($ j = 0, $ j <25; $ j + +)
{
$ Pixel = imagecolorat ($ img, $ i, $ j);
$ Rgb = imagecolorsforindex ($ img, $ pixel);
if ($ rgb ['red'] == 0 & & $ rgb ['green'] == 0 & & $ rgb ['blue'] == 0)
{
$ Iiistart = $ i; iiifinal $ 8 = $ i; break 2;
}
}
}
/ / Find the rectangle of figure 4
for ($ i = $ iiifinal +1, $ i <239; $ i + +)
{
for ($ j = 0, $ j <25; $ j + +)
{
$ Pixel = imagecolorat ($ img, $ i, $ j);
$ Rgb = imagecolorsforindex ($ img, $ pixel);
if ($ rgb ['red'] == 0 & & $ rgb ['green'] == 0 & & $ rgb ['blue'] == 0)
{
$ Ivstart = $ i; ivfinal $ 8 = $ i; break 2;
}
}
}
/ / Find the rectangle of figure 5
for ($ i = $ ivfinal +1, $ i <239; $ i + +)
{
for ($ j = 0, $ j <25; $ j + +)
{
$ Pixel = imagecolorat ($ img, $ i, $ j);
$ Rgb = imagecolorsforindex ($ img, $ pixel);
if ($ rgb ['red'] == 0 & & $ rgb ['green'] == 0 & & $ rgb ['blue'] == 0)
{
$ Vstart = $ i; vfinal $ 8 = $ i; break 2;
}
}
}
/ / Finished finding rectangles
I found what I needed (values are retained in the variables $ istart, $ iistart, $ iiistart, $ ivstart, $ vstart). Now we need to crop the original image to extract just the numbers (for use this function imagecopyresampled -> http://www.php.net/m...pyresampled.php)/ / Division in 5 files / / From left to right Cropuire $ Img1 = imagecreate (9, 25); $ Img2 = imagecreate (9, 25); $ Img3 = imagecreate (9, 25); $ Img4 = imagecreate (9, 25); $ Img5 = imagecreate (9, 25); imagecopyresampled ($ img1, $ img, 0, 0, $ istart, 0.9, 25, 9, 25); imagecopyresampled ($ img2, $ img, 0, 0, $ iistart, 0.9, 25, 9, 25); imagecopyresampled ($ img3, $ img, 0, 0, $ iiistart, 0.9, 25, 9, 25); imagecopyresampled ($ img4, $ img, 0, 0, $ ivstart, 0.9, 25, 9, 25); imagecopyresampled ($ img5, $ img, 0, 0, $ vstart, 0.9, 25, 9, 25); / / Finished cropuire from left to rightFrom what is seen above, the figures are saved in 9x25 images. Let's see them:
3) Comparing images.
Now that I obtinuit images that contain numbers (0, 1, 2, 3, 4), it is saved to the server for later comparison. Where we can use the php script should use the following code:
$ Cif [0] = imagecreatefrompng (cifre/0.png "); $ Cif [1] = imagecreatefrompng (cifre/1.png "); $ Cif [2] = imagecreatefrompng (cifre/2.png "); $ Cif [3] = imagecreatefrompng (cifre/3.png "); $ Cif [4] = imagecreatefrompng (cifre/4.png ");What remains to be done? To compare the pixels of $ img1, $ img2, $ img3, $ img4, $ img5 with each $ cif [0], $ cif [1], $ cif [2], $ cif [3], $ cif [4] at a time. If $ coincides with $ img1 cif [2], the corresponding figure is 2.
/ / Function compare images
function compare ($ imagine12, $ cifra12)
{
$ True = 0;
for ($ i = 0, $ i <239; $ i + +)
{
for ($ j = 0, $ j <25; $ j + +)
{
$ Pixelx = imagecolorat ($ imagine12, $ i, $ j);
$ Pixely = imagecolorat ($ cifra12, $ i, $ j);
if ($ pixelx! = $ pixely)
{
/ / Different pixel
$ True = 1, break 2;
}
}
}
return $ true;
}
/ / End function
/ / Compare images & stuff
/ / If the method returns 0, the images are identical
/ / Returns 1 if the images are different
/ / And go to compare with the following
for ($ x = 0; $ x <= 4; $ x + +)
{
$ Value = compare ($ img1, $ cif [$ x]);
if ($ value == 0) {echo $ x. "\ r \ n"; break;}
}
for ($ x = 0; $ x <= 4; $ x + +)
{
$ Value = compare ($ img2, $ cif [$ x]);
if ($ value == 0) {echo $ x. "\ r \ n"; break;}
}
for ($ x = 0; $ x <= 4; $ x + +)
{
$ Value = compare ($ img3, $ cif [$ x]);
if ($ value == 0) {echo $ x. "\ r \ n"; break;}
}
for ($ x = 0; $ x <= 4; $ x + +)
{
$ Value = compare ($ img4, $ cif [$ x]);
if ($ value == 0) {echo $ x. "\ r \ n"; break;}
}
for ($ x = 0; $ x <= 4; $ x + +)
{
$ Value = compare ($ img5, $ cif [$ x]);
if ($ value == 0) {echo $ x. "\ r \ n"; break;}
}
That's it. You have managed to crack a captcha! Congratulations















