Skip to content

Instantly share code, notes, and snippets.

@Minikea
Created February 12, 2020 12:17
Show Gist options
  • Save Minikea/5bc513a9e781a3f26277b09674fb4e03 to your computer and use it in GitHub Desktop.
Save Minikea/5bc513a9e781a3f26277b09674fb4e03 to your computer and use it in GitHub Desktop.
TShock (terraria) REST dynamic image generator in PHP
<?php
ini_set("default_socket_timeout",2);
//--- variables ---
$ip = '127.0.0.1'; //actual IP of the REST tshock server
$port = '7878'; //port of rest server, not terraria server
$token = "xxx"; //token to generate in tshock, see documentation
$json = json_decode(file_get_contents('https://'.$ip.':'.$port.'/world/read?token='.$token), true);
if ($json['status'] == '200')
{
$daytime = $json['daytime'];
}
$json = json_decode(file_get_contents('https://'.$ip.':'.$port.'/status?token='.$token), true);
header('Content-Type: image/png');
$im = @imagecreatefrompng('Images/terraria_online.png'); // you need to create the static online image first
$font = 'fonts/visitor1.ttf';
$font2 = 'fonts/visitor2.ttf';
$red = imagecolorallocate($im, 255, 24, 24);
$blue = imagecolorallocate($im, 40,40,200);
$black = imagecolorallocate($im, 10,10,10);
$purple = imagecolorallocate($im, 127,0,127);
$grey = imagecolorallocate($im, 50, 50, 50);
$white = imagecolorallocate($im, 255, 255, 255);
//server found and running
if ($json['status'] == '200')
{
//daytime
if ($daytime != True)
{
$ok = imagefilter($im, IMG_FILTER_BRIGHTNESS, -100);
}
// text in the grey box
imagettftext($im, 18, 0, 31, 16, $grey, $font, $json['name']);
imagettftext($im, 18, 0, 30, 15, $red, $font, $json['name']);
imagettftext($im, 14, 0, 31, 34, $grey, $font, 'Carte: '.$json['world']);
imagettftext($im, 14, 0, 30, 33, $black, $font, 'Carte: '.$json['world']);
imagettftext($im, 14, 0, 31, 49, $grey, $font, 'Up: '.$json['uptime']);
imagettftext($im, 14, 0, 30, 48, $black, $font, 'Up: '.$json['uptime']);
// imagettftext($im, 14, 0, 31, 49, $grey, $font, 'Port: '.$json['port']); //only if you want to show port
// imagettftext($im, 14, 0, 30, 48, $black, $font, 'Port: '.$json['port']);
imagettftext($im, 14, 0, 9, 71, $grey, $font, 'Joueurs en ligne: '.$json['playercount']);
imagettftext($im, 14, 0, 8, 70, $blue, $font, 'Joueurs en ligne: '.$json['playercount']);
if ($json['playercount'] > 0)
{
$json = json_decode(file_get_contents('http://'.$ip.':'.$port.'/v2/users/activelist?token='.$token), true);
$playerarray = explode(', ',$json['activeusers']);
$playerarray = array_map('strtolower',$playerarray);
// show VIPs' avatar
$filename = './vip.txt';
$vips = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($vips as $vip_num => $vip)
{
$vip = explode(';',$vip);
$name = strtolower($vip[0]);
if (in_array($name, $playerarray))
{
$player = @imagecreatefrompng('Images/'.$name.'.png');
$long = imageSX($player);
$haut = imageSY($player);
$ok = imagecopy($im,$player,$vip[1],$vip[2]-$haut,0,0,$long,$haut);
}
}
// name of players (VIP or not)
$i = -1;
$j = 0;
// since my server only accept 8 players, I don't have any overflow control on the number of player's name to write but this code can do the work for any number of player (< to wi$ foreach( $playerarray as $player )
{
if ($i++ >= 6)
{
$i = 0;
$j++;
}
imagettftext($im, 11, 0, 11+($j*75), 101+($i*15), $grey, $font2, $player);
// imagettftext($im, 11, 0, 10+($j*75), 100+($i*15), $purple, $font, $player);
imagettftext($im, 11, 0, 10+($j*75), 100+($i*15), $white, $font2, $player);
}
}
}
// offline or not "statut != 200"
else
{
$im = @imagecreatefrompng('Images/terraria_offline.png'); // you need to create the static offline image first
imagettftext($im, 18, 0, 11, 31, $purple, $font, 'IP:'.$ip.':'.$port);
imagettftext($im, 18, 0, 10, 30, $grey, $font, 'IP:'.$ip.':'.$port);
imagettftext($im, 42, 17, 16, 131, $purple, $font, 'offline');
imagettftext($im, 42, 17, 15, 130, $black, $font, 'offline');
}
imagepng($im);
imagedestroy($im);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment