Advertisement
Guest User

Generate Minecraft Online-Status-Images

a guest
Jan 28th, 2013
2,246
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /**
  3. * --------------------------------------------------------------------------------
  4. * - Generate Minecraft Online-Status-Image
  5. * --------------------------------------------------------------------------------
  6. * This code is based on Revi's initial idea and work
  7. * http://forums.bukkit.org/threads/web-beautiful-monitoring.122457/#post-1519585
  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 = 1, $max_online = 21, $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.     //$img = imagecreatetruecolor($width, $height);
  59.     $d = imagecolorexact($img ,0, 0, 0 );
  60.    
  61.     if ($online != 'f'){
  62.         $x_end = $online * ($width / $max_online);
  63.         imagefill($img, 0, 0, imagecolorallocate($img, 230, 220, 230));
  64.         $s=0; # saturation (increase red and blue)
  65.         $red = imagecolorallocate($img, 200-$s, 255, 150-$s); # allocate the imagecolor
  66.        
  67.         // set pixels for y and x
  68.         for ($i = $y_start; $i < $y_end; $i++){ // y
  69.             for ($j = $x_start; $j < $x_end; $j++){ // x
  70.                 imagesetpixel($img, $j, $i, $red);
  71.                 $red = imagecolorallocate($img, 180-$s, 255, 150-$s);
  72.                 $s = $s + 0.03;
  73.             }
  74.         }
  75.        
  76.         imagestring($img, 5, 80, 2, $online . '/' . $max_online, $d);  
  77.     } else {
  78.         imagefill($img, 0, 0, ($blue = imagecolorallocate($img, 255, 0, 0)));
  79.         imagestring($img, 5, 70, 2, $offline_message, $d);
  80.     }
  81.    
  82.     return $img;
  83. }
  84.  
  85. function clear_cache($file_list) {
  86.     foreach ($file_list as $file) {
  87.         unlink($file);
  88.     }
  89. }
  90.  
  91. function get_image_path() {
  92.     global $image_path;
  93.    
  94.     return $image_path;
  95. }
  96.  
  97. function set_image_path($path) {
  98.     global $image_path;
  99.    
  100.     $image_path = $path;
  101. }
  102.  
  103. function is_supported_filetype($file_type) {
  104.     switch ($file_type) {
  105.         case 'png':
  106.         case 'jpg':
  107.         case 'gif':
  108.             return true;
  109.     }
  110.    
  111.     return false;
  112. }
  113.  
  114. function get_image($img) {
  115.     global $file_type;
  116.    
  117.     switch ($file_type) {          
  118.         case 'jpg':
  119.             return imagejpeg($img);
  120.         case 'gif':
  121.             return imagegif($img);
  122.     }
  123.    
  124.     return imagepng($img);
  125. }
  126.  
  127.  
  128. function set_image($img, $path) {
  129.     global $file_type;
  130.    
  131.     switch ($file_type) {          
  132.         case 'jpg':
  133.             return imagejpeg($img, $path);
  134.         case 'gif':
  135.             return imagegif($img, $path);
  136.     }
  137.    
  138.     return imagepng($img, $path);
  139. }
  140.  
  141. function create_image() {
  142.     global $file_type;
  143.    
  144.     $path = get_image_path();
  145.    
  146.     switch ($file_type) {
  147.         case 'jpg':
  148.             return imagecreatefromjpeg($path);
  149.         case 'gif':
  150.             return imagecreatefromgif($path);
  151.     }
  152.    
  153.     return imagecreatefrompng($path);
  154. }
  155.  
  156.  /**
  157.  * extract online informations from connection packet
  158.  */
  159. function online($host, $port) {
  160.     global $offline_message;
  161.    
  162.     $socket = @fsockopen($host, $port);
  163.    
  164.     if ($socket !== false) {
  165.         @fwrite($socket, "\xFE");
  166.         $data = "";
  167.         $data = @fread($socket, 1024);
  168.         @fclose($socket);
  169.        
  170.         if ($data !== false && substr($data, 0, 1) == "\xFF") {
  171.             $info = explode("\xA7", mb_convert_encoding(substr($data, 1), "iso-8859-1", "utf-16be"));
  172.             return $info;
  173.         }
  174.     }
  175.    
  176.     return $offline_message;
  177. }
  178.  
  179. /**
  180. * Handle cache files
  181. */
  182. if (!@file_exists($folder)) {
  183.     @mkdir($folder);
  184. }
  185.  
  186. if ($file_list[0] != '') {
  187.     if ($new_cache_time - $old_cache_time >= $time_update_cache) {
  188.         $online = online($host, $port);
  189.         $img = monitoring_view($online[1], $online[2], imagecreatetruecolor(200, 20));
  190.        
  191.         set_image($img, './' . $folder . '/' . $new_cache_time . '.' . $file_type);
  192.         get_image($img);
  193.         imagedestroy($img);
  194.        
  195.         // unlink old file after the new one has been created
  196.         unlink('./' . $folder . '/' . $old_cache_time . '.' . $file_type);
  197.     } else {
  198.         set_image_path('./' . $folder . '/' . $old_cache_time . '.' . $file_type);
  199.         $img = create_image(get_image_path());
  200.         get_image($img);
  201.         imagedestroy($img);
  202.     }
  203. } else {
  204.     $online = online($host, $port);
  205.     $img = monitoring_view($online[1], $online[2], imagecreatetruecolor(200, 20));
  206.     set_image($img, './' . $folder . '/' . $new_cache_time . '.' . $file_type);
  207.     get_image($img);
  208.     imagedestroy($img);
  209. }
  210. ?>
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement