jacklul

DST Server Status - serverbadge.php

Sep 2nd, 2015
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.09 KB | None | 0 0
  1. <?php
  2.  
  3. $serverstatus = "serverstatus.json";    // path to serverstatus file (generated by API)
  4. $background = "bg.png";         // image has to be 400x100, example: http://i.imgur.com/8678cDk.png
  5. $customfont = "OpenSans-Regular";   // path to OpenSans-Regular.ttf, download: https://www.google.com/fonts#UsePlace:use/Collection:Open+Sans
  6. $outputfile = "badge.png";      // badge will be saved to this file, if left empty image will be displayed by the script after generation
  7.  
  8. if(file_exists($serverstatus) && file_exists($background) && file_exists($customfont.".ttf"))
  9. {
  10.     if(empty($outputfile))
  11.         header("Content-type: image/png");
  12.    
  13.     putenv('GDFONTPATH=' . realpath("."));
  14.     $fontfile = $customfont;
  15.     $fontwidth = imagefontwidth($fontfile);
  16.    
  17.     $data = json_decode(file_get_contents($serverstatus), true);
  18.     $data = $data[0];
  19.    
  20.     $image = imagecreatefrompng($background);
  21.  
  22.     // Server Name
  23.     $string = preg_replace('!\s+!', ' ', $data['settings']['servername']);
  24.     $color = imagecolorallocate($image, 255, 255, 70);
  25.     $size  = 17;
  26.     $bbox = imagettfbbox($size, $angle, $fontfile, $string);        //centered
  27.     $pos_x = (imagesx($image) / 2) - (($bbox[2] - $bbox[0]) / 2);   //centered
  28.     $pos_y = 25;
  29.     imagettftext($image, $size, $angle, $pos_x, $pos_y, $color, $fontfile, $string);
  30.  
  31.     // Game Mode - Season - Day
  32.     $string = ucfirst($data['settings']['gamemode'])." - ".ucfirst($data['statevars']['season'])." - Day ".($data['statevars']['cycles']+1)." ";
  33.     $color = imagecolorallocate($image, 255, 255, 70);
  34.     $size  = 12;
  35.     $bbox = imagettfbbox($size, $angle, $fontfile, $string);        //centered
  36.     $pos_x = (imagesx($image) / 2) - (($bbox[2] - $bbox[0]) / 2);   //centered
  37.     $pos_y = 50;
  38.     imagettftext($image, $size, $angle, $pos_x, $pos_y, $color, $fontfile, $string);
  39.    
  40.     // Players / Maxplayers
  41.     if($data['settings']['dedicated'])
  42.         $players = sizeof($data['players'])-1;
  43.     else
  44.         $players = sizeof($data['players']);
  45.    
  46.     $string = "Players: ".$players." / ".$data['settings']['maxplayers'];
  47.     $color = imagecolorallocate($image, 255, 255, 70);
  48.     $size  = 12;
  49.     $bbox = imagettfbbox($size, $angle, $fontfile, $string);        //centered
  50.     $pos_x = (imagesx($image) / 2) - (($bbox[2] - $bbox[0]) / 2);   //centered
  51.     $pos_y = 70;
  52.     imagettftext($image, $size, $angle, $pos_x, $pos_y, $color, $fontfile, $string);
  53.    
  54.     // Day/Dusk/Night
  55.     if($data['statevars']['isfullmoon'] == 1)
  56.         $phaseprefix = "Full Moon ";
  57.     elseif($data['statevars']['israining'] == 1)
  58.         $phaseprefix = "Rainy ";
  59.     elseif($data['statevars']['issnowing'] == 1)
  60.         $phaseprefix = "Snowy ";
  61.    
  62.     $string = $phaseprefix.ucfirst($data['statevars']['phase']);
  63.     $color = imagecolorallocate($image, 255, 255, 70);
  64.     $size  = 12;
  65.     $bbox = imagettfbbox($size, $angle, $fontfile, $string);        //centered
  66.     $pos_x = (imagesx($image) / 2) - (($bbox[2] - $bbox[0]) / 2);   //centered
  67.     $pos_y = 90;
  68.     imagettftext($image, $size, $angle, $pos_x, $pos_y, $color, $fontfile, $string);
  69.  
  70.     if(!empty($outputfile))
  71.     {
  72.         imagepng($image, $outputfile);
  73.         echo "Image saved to: ".$outputfile;
  74.     }
  75.     else
  76.         imagepng($image);
  77.    
  78.     imagedestroy($image);
  79. }
  80. else
  81.     die("One of the required files is missing!");
  82.  
  83. ?>
Advertisement
Add Comment
Please, Sign In to add comment