Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $serverstatus = "serverstatus.json"; // path to serverstatus file (generated by API)
- $background = "bg.png"; // image has to be 400x100, example: http://i.imgur.com/8678cDk.png
- $customfont = "OpenSans-Regular"; // path to OpenSans-Regular.ttf, download: https://www.google.com/fonts#UsePlace:use/Collection:Open+Sans
- $outputfile = "badge.png"; // badge will be saved to this file, if left empty image will be displayed by the script after generation
- if(file_exists($serverstatus) && file_exists($background) && file_exists($customfont.".ttf"))
- {
- if(empty($outputfile))
- header("Content-type: image/png");
- putenv('GDFONTPATH=' . realpath("."));
- $fontfile = $customfont;
- $fontwidth = imagefontwidth($fontfile);
- $data = json_decode(file_get_contents($serverstatus), true);
- $data = $data[0];
- $image = imagecreatefrompng($background);
- // Server Name
- $string = preg_replace('!\s+!', ' ', $data['settings']['servername']);
- $color = imagecolorallocate($image, 255, 255, 70);
- $size = 17;
- $bbox = imagettfbbox($size, $angle, $fontfile, $string); //centered
- $pos_x = (imagesx($image) / 2) - (($bbox[2] - $bbox[0]) / 2); //centered
- $pos_y = 25;
- imagettftext($image, $size, $angle, $pos_x, $pos_y, $color, $fontfile, $string);
- // Game Mode - Season - Day
- $string = ucfirst($data['settings']['gamemode'])." - ".ucfirst($data['statevars']['season'])." - Day ".($data['statevars']['cycles']+1)." ";
- $color = imagecolorallocate($image, 255, 255, 70);
- $size = 12;
- $bbox = imagettfbbox($size, $angle, $fontfile, $string); //centered
- $pos_x = (imagesx($image) / 2) - (($bbox[2] - $bbox[0]) / 2); //centered
- $pos_y = 50;
- imagettftext($image, $size, $angle, $pos_x, $pos_y, $color, $fontfile, $string);
- // Players / Maxplayers
- if($data['settings']['dedicated'])
- $players = sizeof($data['players'])-1;
- else
- $players = sizeof($data['players']);
- $string = "Players: ".$players." / ".$data['settings']['maxplayers'];
- $color = imagecolorallocate($image, 255, 255, 70);
- $size = 12;
- $bbox = imagettfbbox($size, $angle, $fontfile, $string); //centered
- $pos_x = (imagesx($image) / 2) - (($bbox[2] - $bbox[0]) / 2); //centered
- $pos_y = 70;
- imagettftext($image, $size, $angle, $pos_x, $pos_y, $color, $fontfile, $string);
- // Day/Dusk/Night
- if($data['statevars']['isfullmoon'] == 1)
- $phaseprefix = "Full Moon ";
- elseif($data['statevars']['israining'] == 1)
- $phaseprefix = "Rainy ";
- elseif($data['statevars']['issnowing'] == 1)
- $phaseprefix = "Snowy ";
- $string = $phaseprefix.ucfirst($data['statevars']['phase']);
- $color = imagecolorallocate($image, 255, 255, 70);
- $size = 12;
- $bbox = imagettfbbox($size, $angle, $fontfile, $string); //centered
- $pos_x = (imagesx($image) / 2) - (($bbox[2] - $bbox[0]) / 2); //centered
- $pos_y = 90;
- imagettftext($image, $size, $angle, $pos_x, $pos_y, $color, $fontfile, $string);
- if(!empty($outputfile))
- {
- imagepng($image, $outputfile);
- echo "Image saved to: ".$outputfile;
- }
- else
- imagepng($image);
- imagedestroy($image);
- }
- else
- die("One of the required files is missing!");
- ?>
Advertisement
Add Comment
Please, Sign In to add comment