', '.', '/', '?'), '', $name); if ($name == '') return; // Names in Tibia always start with an uppercase letter $name = ucfirst($name); // Write cache hits/misses to a log file $fp = fopen('log.txt', 'a'); // Set up memcache $mc = new Memcache(); $mc->connect('127.0.0.1', 11211); // Our memcache key, or, potentially a file-based method of storage. $x = './data/' . $time . '/userbar_' . md5($name) . '.png'; $y = false; if ($mc) { $y = $mc->get($x); } else { echo "Error: couldn't connect to memcache"; exit; } // Image was in memcache, serve it if ($y !== false) { header('Date: ' . gmdate('D, j M Y H:i:s') . ' GMT'); header('Last-Modified: ' . gmdate('D, j M Y H:i:s', $time) . ' GMT'); header('Content-Type: image/png'); fwrite($fp, "hit $x\n"); fclose($fp); echo $y; exit; } else { fwrite($fp, "miss $x\n"); fclose($fp); } // Save the original name as we change it later $namepng = $name; // Connect to the database mysql_connect('localhost', 'username', 'password'); mysql_select_db('databasename'); $res = mysql_query($x = 'SELECT * FROM online WHERE name=\'' . @mysql_real_escape_string($name) . '\''); if (@mysql_num_rows($res) > 0) { // Player is currently online $online = true; $row = @mysql_fetch_array($res); $name = $row['name']; $level = $row['level']; $voc = convert_voc($row['vocation']); } else { $online = false; } // Image dimensions $w = 350; $h = 19; // Create the image $im = imagecreatetruecolor($w, $h); // Set up some colours $col = imagecolorallocate($im, 255, 255, 255); $col2 = imagecolorallocate($im, 0, 0, 0); $col3 = imagecolorallocate($im, 69, 69, 69); $coloff = imagecolorallocate($im, 246, 94, 94); $colon = imagecolorallocate($im, 94, 246, 94); $colstuff = imagecolorallocate($im, 94, 246, 246); // Set up the background colour and the border imagefilledrectangle($im, 0, 0, $w, $h, $col2); imagefilledrectangle($im, 1, 1, $w - 2, $h - 2, $col3); // Font path $font = 'visitor.fon'; // Include ": " in the name so we can calculate dimensions properly $name .= ': '; // Find out name dimensions so we know where to put online/offline text $namebb = imagettfbbox(7, 0, $font, $name); // Draw name imagefttext($im, 7, 0, 8, 12, $col, $font, $name); // Draw online/offline status if ($online) imagefttext($im, 7, 0, $namebb[4] + 4, 12, $colon, $font, 'online'); else imagefttext($im, 7, 0, $namebb[4] + 4, 12, $coloff, $font, 'offline'); if ($online) { // Player is online, draw level/vocation status $levelbb = imagettfbbox(7, 0, $font, $level); imagefttext($im, 7, 0, $w - $levelbb[4] - 8, 12, $colstuff, $font, $level); imagefttext($im, 7, 0, $w - $levelbb[4] - 46, 12, $col, $font, 'Level: '); $vocbb = imagettfbbox(7, 0, $font, $voc); imagefttext($im, 7, 0, $w - $levelbb[4] - 46 - $vocbb[4] - 20, 12, $colstuff, $font, $voc); imagefttext($im, 7, 0, $w - $levelbb[4] - 46 - $vocbb[4] - 46, 12, $col, $font, 'Voc: '); } // Headers. Last-Modified is used for client-side caching header('Date: ' . gmdate('D, j M Y H:i:s') . ' GMT'); header('Last-Modified: ' . gmdate('D, j M Y H:i:s', $time) . ' GMT'); header('Content-type: image/png'); // Capture the image so we can store it in memcache ob_start(); imagepng($im); $data = ob_get_contents(); ob_end_clean(); // Output the image echo $data; // Store the image in memcache $mc->set('./data/' . $time . '/userbar_' . md5($namepng) . '.png', $data, 0, 600); // Destroy gd resources imagecolordeallocate($im, $col); imagecolordeallocate($im, $col2); imagecolordeallocate($im, $col3); imagecolordeallocate($im, $coloff); imagecolordeallocate($im, $colon); imagecolordeallocate($im, $colstuff); imagedestroy($im); ?>