Simple PHP OCR with GD


Read two articles first:

PHP: Crop image function

PHP: Crop image function (part 2)

Now, we try to read number in inCloak.com’s picture.

Example: 16342 http://incloak.com/images/proxylist_port_4204497.gif

Step 1:

Goto http://incloak.com/proxy-list/ we can see many pictures contain proxy port. We find that these pictures that contain a maximum of 5 digits, and have the same font, same size.

We use 3 pictures contain all number from 0 to 9:

18895 http://incloak.com/images/proxylist_port_4328980.gif
53067 http://incloak.com/images/proxylist_port_4204255.gif
16342 http://incloak.com/images/proxylist_port_4204497.gif

Step 2:
In this article PHP: Crop image function (part 2), we croped a picture to a piece of size 5×8 px
Now, we use 3 pictures above, we get 10 pictures below:

http://www.demo.tutorialspots.com/crop/crop.php?w=5&h=8&x=12&y=3&src=http://incloak.com/images/proxylist_port_4204255.gif
http://www.demo.tutorialspots.com/crop/crop.php?w=5&h=8&x=0&y=3&src=http://incloak.com/images/proxylist_port_4328980.gif
http://www.demo.tutorialspots.com/crop/crop.php?w=5&h=8&x=24&y=3&src=http://incloak.com/images/proxylist_port_4204497.gif
http://www.demo.tutorialspots.com/crop/crop.php?w=5&h=8&x=12&y=3&src=http://incloak.com/images/proxylist_port_4204497.gif
http://www.demo.tutorialspots.com/crop/crop.php?w=5&h=8&x=18&y=3&src=http://incloak.com/images/proxylist_port_4204497.gif
http://www.demo.tutorialspots.com/crop/crop.php?w=5&h=8&x=0&y=3&src=http://incloak.com/images/proxylist_port_4204255.gif
http://www.demo.tutorialspots.com/crop/crop.php?w=5&h=8&x=6&y=3&src=http://incloak.com/images/proxylist_port_4204497.gif
http://www.demo.tutorialspots.com/crop/crop.php?w=5&h=8&x=24&y=3&src=http://incloak.com/images/proxylist_port_4204255.gif
http://www.demo.tutorialspots.com/crop/crop.php?w=5&h=8&x=6&y=3&src=http://incloak.com/images/proxylist_port_4328980.gif
http://www.demo.tutorialspots.com/crop/crop.php?w=5&h=8&x=18&y=3&src=http://incloak.com/images/proxylist_port_4328980.gif

Note: we can use Photoshop to crop picture

Step 3: Read pixel and save data in map file:
We use the following code:

<?php

/**
 * @author www.tutorialspots.com
 * @copyright 2012
 */

foreach (array("0", 1, 2, 3, 4, 5, 6, 7, 8, 9) as $num)
{

    $im = imagecreatefrompng("$num.png");

    $map = '';

    for ($y = 0; $y < 8; $y++)
    {
        for ($x = 0; $x < 5; $x++)
        {
            $rgb = imagecolorat($im, $x, $y);

            $color = imagecolorsforindex($im, $rgb);

            $map .= (($color['red'] == 255 && $color['green'] == 255 && $color['blue'] == 255) || ($color['red'] ==
                0 && $color['green'] == 0 && $color['blue'] == 0)) ? 0 : 1;

        }
    }

    file_put_contents('map/' . $num . '.map', $map);
}

?>

We get 10 map file from 0 to 9.map

Step 4: Process OCR:

1 Comment

Leave a Reply