<?php
$img = imagecreatefrompng("test.png");
 
$letters = str_split(":13LABCDEFGHIJKMNOPQRSTUVWXYZ02456789<>"); // order by coverage
 
$result = array();
 
for ($x = 0; $x < 240; $x += 40) {
    for ($y = 0; $y < 200; $y += 40) {
        foreach ($letters as $letter) {
            $tmp = imagecreate(40, 40);
            imagecopy($tmp, $img, 0, 0, $x, $y, 40, 40);
            imagestring($tmp, 5, 16, 12, $letter, imagecolorallocate($tmp, 0, 0, 0));
            $found = false;
            for ($i = 0; $i < 40; $i++) {
                for ($j = 0; $j < 40; $j++) {
                    if (imagecolorat($tmp, $i, $j) == 1) {
                        $found = true;
                        break 2;
                    }
                }
            }
            if (!$found) {
                $result[] = array(imagecolorat($img, $x, $y), $letter);
                break;
            }
        }
        imagestring($img, 5, $x + 16, $y + 12, $letter, imagecolorallocate($img, 0, 0, 0));
    }
}
 
array_multisort($result);
 
foreach ($result as $r) {
    echo $r[1];
}
?>