Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * --------------------------------------------------------------------------------
- * - Generate Minecraft Online-Status-Image
- * --------------------------------------------------------------------------------
- * This code is based on Revi's initial idea and work
- * http://forums.bukkit.org/threads/web-beautiful-monitoring.122457/#post-1519585
- *
- * Features
- * - supports png, jpg and gif
- * - per image cache files
- * - keep the cache clean (30)
- */
- /**
- * --------------------------------------------------------------------------------
- * - Configuration Section
- * --------------------------------------------------------------------------------
- */
- error_reporting(0); # set to E_ALL when debugging otherwise you can leave this alone
- $host = 'localhost'; # IP or hostname
- $port = 25565; # port
- $folder_name = 'cache'; # folder used for caching; Make sure the folder is created and has the correct access rights
- $time_update_cache = 5; # update cache every x second, decrease this value if you want a faster response time
- $offline_message = 'offline'; # this message will be shown, when the server is offline or in case of connection problems
- $file_type = @$_GET['t']; # supported file_types (png|jpg|gif)
- $keep_files = 30; # if we spam the refresh button we may loose the old cache file and leave an unused image behind.
- # to prevent this we only keep 30 files (10 per file_type) at max. This can also be doubled safely.
- /**
- * You normally don't have to edit below this line.
- * Only edit below this line if you know what you are doing.
- */
- $file_type = (is_supported_filetype($file_type)) ? $file_type : 'png'; # always fallback to png
- header("Content-type: image/" . $file_type);
- $folder = escapeshellcmd($folder_name);
- $new_cache_time = time();
- $file_list = glob('./' . $folder . '/*.' . $file_type); # get all image files in $folder_name
- if (count($file_list) >= $keep_files)
- clear_cache($file_list);
- $image_path = '';
- $old_cache_time = str_replace('.' . $file_type,'',str_replace('./' . $folder . '/','', $file_list[0])); # get the oldest cache file time from filename
- function monitoring_view($online = 1, $max_online = 21, $img) {
- global $offline_message;
- $x_start = 0;
- $y_start = 0;
- $y_end = 20;
- $height = 20;
- $width = 200;
- //$img = imagecreatetruecolor($width, $height);
- $d = imagecolorexact($img ,0, 0, 0 );
- if ($online != 'f'){
- $x_end = $online * ($width / $max_online);
- imagefill($img, 0, 0, imagecolorallocate($img, 230, 220, 230));
- $s=0; # saturation (increase red and blue)
- $red = imagecolorallocate($img, 200-$s, 255, 150-$s); # allocate the imagecolor
- // set pixels for y and x
- for ($i = $y_start; $i < $y_end; $i++){ // y
- for ($j = $x_start; $j < $x_end; $j++){ // x
- imagesetpixel($img, $j, $i, $red);
- $red = imagecolorallocate($img, 180-$s, 255, 150-$s);
- $s = $s + 0.03;
- }
- }
- imagestring($img, 5, 80, 2, $online . '/' . $max_online, $d);
- } else {
- imagefill($img, 0, 0, ($blue = imagecolorallocate($img, 255, 0, 0)));
- imagestring($img, 5, 70, 2, $offline_message, $d);
- }
- return $img;
- }
- function clear_cache($file_list) {
- foreach ($file_list as $file) {
- unlink($file);
- }
- }
- function get_image_path() {
- global $image_path;
- return $image_path;
- }
- function set_image_path($path) {
- global $image_path;
- $image_path = $path;
- }
- function is_supported_filetype($file_type) {
- switch ($file_type) {
- case 'png':
- case 'jpg':
- case 'gif':
- return true;
- }
- return false;
- }
- function get_image($img) {
- global $file_type;
- switch ($file_type) {
- case 'jpg':
- return imagejpeg($img);
- case 'gif':
- return imagegif($img);
- }
- return imagepng($img);
- }
- function set_image($img, $path) {
- global $file_type;
- switch ($file_type) {
- case 'jpg':
- return imagejpeg($img, $path);
- case 'gif':
- return imagegif($img, $path);
- }
- return imagepng($img, $path);
- }
- function create_image() {
- global $file_type;
- $path = get_image_path();
- switch ($file_type) {
- case 'jpg':
- return imagecreatefromjpeg($path);
- case 'gif':
- return imagecreatefromgif($path);
- }
- return imagecreatefrompng($path);
- }
- /**
- * extract online informations from connection packet
- */
- function online($host, $port) {
- global $offline_message;
- $socket = @fsockopen($host, $port);
- if ($socket !== false) {
- @fwrite($socket, "\xFE");
- $data = "";
- $data = @fread($socket, 1024);
- @fclose($socket);
- if ($data !== false && substr($data, 0, 1) == "\xFF") {
- $info = explode("\xA7", mb_convert_encoding(substr($data, 1), "iso-8859-1", "utf-16be"));
- return $info;
- }
- }
- return $offline_message;
- }
- /**
- * Handle cache files
- */
- if (!@file_exists($folder)) {
- @mkdir($folder);
- }
- if ($file_list[0] != '') {
- if ($new_cache_time - $old_cache_time >= $time_update_cache) {
- $online = online($host, $port);
- $img = monitoring_view($online[1], $online[2], imagecreatetruecolor(200, 20));
- set_image($img, './' . $folder . '/' . $new_cache_time . '.' . $file_type);
- get_image($img);
- imagedestroy($img);
- // unlink old file after the new one has been created
- unlink('./' . $folder . '/' . $old_cache_time . '.' . $file_type);
- } else {
- set_image_path('./' . $folder . '/' . $old_cache_time . '.' . $file_type);
- $img = create_image(get_image_path());
- get_image($img);
- imagedestroy($img);
- }
- } else {
- $online = online($host, $port);
- $img = monitoring_view($online[1], $online[2], imagecreatetruecolor(200, 20));
- set_image($img, './' . $folder . '/' . $new_cache_time . '.' . $file_type);
- get_image($img);
- imagedestroy($img);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement