Advertisement
Guest User

Minecraft Online-Status Image-Generator (1.4.7)

a guest
Jan 29th, 2013
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.83 KB | None | 0 0
  1. <?php
  2. /**
  3. * --------------------------------------------------------------------------------
  4. * - Minecraft Online-Status Image-Generator
  5. * --------------------------------------------------------------------------------
  6. * This code is based on Revi's initial idea and work
  7. * http://forums.bukkit.org/threads/web-beautiful-monitoring.122457
  8. *
  9. * Features
  10. * - supports png, jpg and gif
  11. * - per image cache files
  12. * - keep the cache clean (30)
  13. */
  14.  
  15.  
  16. /**
  17. * --------------------------------------------------------------------------------
  18. * - Configuration Section
  19. * --------------------------------------------------------------------------------
  20. */
  21. error_reporting(0); # set to E_ALL when debugging otherwise you can leave this alone
  22.  
  23. $host = 'localhost';      # IP or hostname
  24. $port = 25565;                  # port
  25. $folder_name = 'cache';         # folder used for caching; Make sure the folder is created and has the correct access rights
  26. $time_update_cache = 5;         # update cache every x second, decrease this value if you want a faster response time
  27. $offline_message = 'offline';   # this message will be shown, when the server is offline or in case of connection problems
  28. $file_type = @$_GET['t'];       # supported file_types (png|jpg|gif)
  29. $keep_files = 30;               # if we spam the refresh button we may loose the old cache file and leave an unused image behind.
  30.                                # to prevent this we only keep 30 files (10 per file_type) at max. This can also be doubled safely.
  31.                                
  32. /**
  33. * You normally don't have to edit below this line.
  34. * Only edit below this line if you know what you are doing.
  35. */
  36. $file_type = (is_supported_filetype($file_type)) ? $file_type : 'png'; # always fallback to png
  37. header("Content-type: image/" . $file_type);
  38.  
  39. $folder = escapeshellcmd($folder_name);
  40. $new_cache_time = time();
  41. $file_list = glob('./' . $folder . '/*.' . $file_type); # get all image files in $folder_name
  42.  
  43. if (count($file_list) >= $keep_files)
  44.     clear_cache($file_list);
  45.    
  46. $image_path = '';
  47. $old_cache_time = str_replace('.' . $file_type,'',str_replace('./' . $folder . '/','', $file_list[0])); # get the oldest cache file time from filename
  48.  
  49. function monitoring_view($online_data, $img) {
  50.     global $offline_message;
  51.    
  52.     $x_start = 0;
  53.     $y_start = 0;
  54.     $y_end = 20;
  55.     $height = 20;
  56.     $width = 200;
  57.    
  58.     $max_online = end($online_data);
  59.     $online = prev($online_data);
  60.     reset($online_data);
  61.    
  62.     //$img = imagecreatetruecolor($width, $height);
  63.     $d = imagecolorexact($img ,0, 0, 0 );
  64.    
  65.     if ($online != 'f'){
  66.         $x_end = $online * ($width / $max_online);
  67.         imagefill($img, 0, 0, imagecolorallocate($img, 230, 220, 230));
  68.         $s=0; # saturation (increase red and blue)
  69.         $red = imagecolorallocate($img, 200-$s, 255, 150-$s); # allocate the imagecolor
  70.        
  71.         // set pixels for y and x
  72.         for ($i = $y_start; $i < $y_end; $i++){ // y
  73.             for ($j = $x_start; $j < $x_end; $j++){ // x
  74.                 imagesetpixel($img, $j, $i, $red);
  75.                 $red = imagecolorallocate($img, 180-$s, 255, 150-$s);
  76.                 $s = $s + 0.03;
  77.             }
  78.         }
  79.        
  80.         imagestring($img, 5, 80, 2, $online . '/' . $max_online, $d);  
  81.     } else {
  82.         imagefill($img, 0, 0, ($blue = imagecolorallocate($img, 255, 0, 0)));
  83.         imagestring($img, 5, 70, 2, $offline_message, $d);
  84.     }
  85.    
  86.     return $img;
  87. }
  88.  
  89. function clear_cache($file_list) {
  90.     foreach ($file_list as $file) {
  91.         unlink($file);
  92.     }
  93. }
  94.  
  95. function get_image_path() {
  96.     global $image_path;
  97.    
  98.     return $image_path;
  99. }
  100.  
  101. function set_image_path($path) {
  102.     global $image_path;
  103.    
  104.     $image_path = $path;
  105. }
  106.  
  107. function is_supported_filetype($file_type) {
  108.     switch ($file_type) {
  109.         case 'png':
  110.         case 'jpg':
  111.         case 'gif':
  112.             return true;
  113.     }
  114.    
  115.     return false;
  116. }
  117.  
  118. function get_image($img) {
  119.     global $file_type;
  120.    
  121.     switch ($file_type) {          
  122.         case 'jpg':
  123.             return imagejpeg($img);
  124.         case 'gif':
  125.             return imagegif($img);
  126.     }
  127.    
  128.     return imagepng($img);
  129. }
  130.  
  131.  
  132. function set_image($img, $path) {
  133.     global $file_type;
  134.    
  135.     switch ($file_type) {          
  136.         case 'jpg':
  137.             return imagejpeg($img, $path);
  138.         case 'gif':
  139.             return imagegif($img, $path);
  140.     }
  141.    
  142.     return imagepng($img, $path);
  143. }
  144.  
  145. function create_image() {
  146.     global $file_type;
  147.    
  148.     $path = get_image_path();
  149.    
  150.     switch ($file_type) {
  151.         case 'jpg':
  152.             return imagecreatefromjpeg($path);
  153.         case 'gif':
  154.             return imagecreatefromgif($path);
  155.     }
  156.    
  157.     return imagecreatefrompng($path);
  158. }
  159.  
  160.  /**
  161.  * extract online informations from connection packet
  162.  */
  163. function online($host, $port) {
  164.     global $offline_message;
  165.    
  166.     $socket = @fsockopen($host, $port);
  167.    
  168.     if ($socket !== false) {
  169.         @fwrite($socket, "\xFE");
  170.         $data = "";
  171.         $data = @fread($socket, 1024);
  172.         @fclose($socket);
  173.            
  174.         if ($data !== false && substr($data, 0, 1) == "\xFF") {
  175.             $info = explode("\xA7", mb_convert_encoding(substr($data, 1), "iso-8859-1", "utf-16be"));
  176.             // var_dump($info) # uncomment to debug the parsed online packet
  177.             return $info;
  178.         }
  179.     }
  180.    
  181.     return $offline_message;
  182. }
  183.  
  184. /**
  185. * Handle cache files
  186. */
  187. if (!@file_exists($folder)) {
  188.     @mkdir($folder);
  189. }
  190.  
  191. if ($file_list[0] != '') {
  192.     if ($new_cache_time - $old_cache_time >= $time_update_cache) {
  193.         $online_data = online($host, $port);
  194.         $img = monitoring_view($online_data, imagecreatetruecolor(200, 20));
  195.        
  196.         set_image($img, './' . $folder . '/' . $new_cache_time . '.' . $file_type);
  197.         get_image($img);
  198.         imagedestroy($img);
  199.        
  200.         // unlink old file after the new one has been created
  201.         unlink('./' . $folder . '/' . $old_cache_time . '.' . $file_type);
  202.     } else {
  203.         set_image_path('./' . $folder . '/' . $old_cache_time . '.' . $file_type);
  204.         $img = create_image(get_image_path());
  205.         get_image($img);
  206.         imagedestroy($img);
  207.     }
  208. } else {
  209.     $online_data = online($host, $port);
  210.     $img = monitoring_view($online_data, imagecreatetruecolor(200, 20));
  211.     set_image($img, './' . $folder . '/' . $new_cache_time . '.' . $file_type);
  212.     get_image($img);
  213.     imagedestroy($img);
  214. }
  215. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement