Skip to content

Instantly share code, notes, and snippets.

@uestla
Created October 21, 2012 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uestla/3927622 to your computer and use it in GitHub Desktop.
Save uestla/3927622 to your computer and use it in GitHub Desktop.
require_once __DIR__ . '/geocaching.php';
var_dump( getFoundCacheCount( '3f4da330-f5d0-411d-bcee-74672b284351' ) );
/**
* Simple OCR for getting the found cache count from the statusbar image.
*
* Requires `allow_url_fopen` to be set to TRUE
*
* @param string your geocaching UID
* @return int amount of caches you've found
*
* @see http://img.geocaching.com/stats/img.aspx?uid=3f4da330-f5d0-411d-bcee-74672b284351 - status bar example
* @see http://diskuse.jakpsatweb.cz/?action=vthread&forum=17&topic=141026 (cze) - thanks to Cho
*/
function getFoundCacheCount( $uID )
{
$masks = array(
'01410242034304440545',
'1020',
'00404142332415',
'0041233344450646',
'20112102122223240515253526',
'1222324344450646',
'10400102031323330444054546',
'403225',
'4041234405450646',
'000141024213233343444506',
'2115',
);
$starts = array(93, 23);
$charRes = array(5, 7);
$colourLimit = 50;
$result = '';
$image = imageCreateFromString( file_get_contents( 'http://img.geocaching.com/stats/img.aspx?uid=' . $uID ) );
for ($i = 0; $i < 100; $i++) {
$crop = imageCreateTrueColor( $charRes[0], $charRes[1] );
imageCopy( $crop, $image, 0, 0, $starts[0] + $i, $starts[1], $charRes[0], $charRes[1] );
$mask = '';
for ($j = 0; $j < $charRes[1]; $j++) {
for ($k = 0; $k < $charRes[0]; $k++) {
$rgb = imageColorsForIndex( $crop, imageColorAt($crop, $k, $j) );
if ($rgb['red'] < $colourLimit && $rgb['green'] < $colourLimit && $rgb['blue'] < $colourLimit) {
$mask .= $k . $j;
}
}
}
imageDestroy($crop);
foreach ($masks as $key => $val) {
if ($val === $mask) {
if ($key === 10) { // slash char
break 2;
} else {
$result .= $key;
}
}
}
}
imageDestroy($image);
return (int) $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment