User:Eupeodes/Statbar

From Geohashing
< User:Eupeodes
Revision as of 18:05, 27 May 2014 by imported>Eupeodes
Eupeodes Statbar.png

For geocaching a nice statistics bar is available. On my site I used this but also wanted one for geohashing. Since I don't know of any other bar I wrote one myself. The bar needs 2 parameters: user: The page where the expedition list is, can be just your username but can also be user/Expeditions text: A line of text displayed at the top.

For calculating the number of expeditions the arrows of Aperfectbot are used. So the number of green arrows is the number of successes and the number of red and green arrows is the total. Data is cached for 24 hours, so it can take some time before an image is updated.

Example: http://gh.eupeodes.nl/statbar/?user=Eupeodes/Expeditions&text=Geohashing+stats

Code

directory cache is needed in same dir as index.php

File: index.php

<?php

$user = $_GET['user'];
$text = $_GET['text'];

$file = 'cache/'.urlencode($user.$text).".png";

header("Content-type: image/png");
header("Cache-Control: max-age=86400"); // HTTP/1.1

if(!file_exists($file) || filemtime($file) < time()-86400){
   $base_url = 'http://wiki.xkcd.com/geohashing/User:';
   $url = $base_url.$user;
   $data = file_get_contents($url);
   $success = substr_count($data, 'alt="Arrow2.png"');
   $fail = substr_count($data, 'alt="Arrow4.png"');
   $count = $success+$fail;
   $img = imagecreatetruecolor(200,50);
   $img2 = imagecreatefrompng('icon.png');
   $bg_color = imagecolorallocate($img, 255,255,255);
   imagefilledrectangle($img,1,1,198,48,$bg_color);
   imagecopyresampled($img, $img2, 1,0,0,0,50,49,135,135);
   $text_color = imagecolorallocate($img, 0,0,0);
   imagestring($img, 3, 55, 3, $text, $text_color);
   imagestring($img, 2, 55, 18, "Attempts: ".$count, $text_color);
   imagestring($img, 2, 55, 32, "Successes: ".$success, $text_color);
   imagepng($img, $file);
}
echo file_get_contents($file);
?>