Page 1 of 1

Making a Dynamic minecraft status image

Posted: Wed Jun 06, 2012 11:41 pm
by tsm200
i hope someone can help me :),

Alright i got the minecraft status using minequery working and got a image created by php done
only when i try to combine those 2 together the image becomes invalid :(
here is my code tyring to combine both
<?php

header('Content-Type: image/png');
create_image();

//fetches server information from minequery.
function fetch_server_info($ip, $port){
	$socket = fsockopen($ip, $port, $errno, $errstr, 2);
	
	if ($socket === false){
		return false;
	}
	
	fwrite($socket, "QUERY_JSON\n");
	
	$responce = stream_get_contents($socket);
	
	return json_decode($responce, true);
}


$config['server']['ip']    = '66.7.222.221';
$config['server']['port']  = '25566';

$info =  fetch_server_info($config['server']['ip'], $config['server']['port']);

$test = $info['playerCount'];


function create_image(){

$img = imagecreatetruecolor(214, 341);

$border = imagecolorallocate($img, 0x1c, 0x1c, 0x1c);
$bg     = imagecolorallocate($img, 0x42, 0x42, 0x42);

imagefill($img, 0, 0, $border);
imagefilledrectangle($img, 3, 3, 210, 338, $bg);



$font1 = imagecolorallocate($img, 0xff, 0xbf, 0x00);
$font2 = imagecolorallocate($img, 0x3a, 0xdf, 0x00);
$test1 = '3';
imagettftext($img, 12, 0, 12, 50, $font1, './Fire1.ttf', 'Server IP:');
imagettftext($img, 12, 0, 12, 75, $font2, './Fire1.ttf', $config['server']['ip']);
imagettftext($img, 12, 0, 12, 100, $font1, './Fire1.ttf', 'Online Players:');
imagettftext($img, 12, 0, 12, 125, $font2, './Fire1.ttf', $info['playerCount']; \ $info['maxPlayers']);
imagettftext($img, 12, 0, 12, 150, $font1, './Fire1.ttf', 'Status:');
imagettftext($img, 12, 0, 12, 175, $font2, './Fire1.ttf', 'ONLINE');
imagettftext($img, 12, 0, 12, 200, $font1, './Fire1.ttf', 'Players:');
imagepng($img);
}


?>

Re: Making a Dynamic minecraft status image

Posted: Thu Jun 07, 2012 12:31 am
by jacek
The problem probably comes from the way you are trying to access variables that are defined outside of those functions.

You may as well remove the functions completely and just have the code all in one place since this will probably never be used for anything else
<?php
 
header('Content-Type: image/png');

$config['server']['ip']    = '66.7.222.221';
$config['server']['port']  = '25566';

//fetches server information from minequery.
$socket = fsockopen($config['server']['ip'], $config['server']['port'], $errno, $errstr, 2);

if ($socket === false){
	die('erm :(');
}

fwrite($socket, "QUERY_JSON\n");

$responce = stream_get_contents($socket);

$info = json_decode($responce, true);

$info =  fetch_server_info($config['server']['ip'], $config['server']['port']);

// create the iamge
$img = imagecreatetruecolor(214, 341);

$border = imagecolorallocate($img, 0x1c, 0x1c, 0x1c);
$bg     = imagecolorallocate($img, 0x42, 0x42, 0x42);

imagefill($img, 0, 0, $border);
imagefilledrectangle($img, 3, 3, 210, 338, $bg);

$font1 = imagecolorallocate($img, 0xff, 0xbf, 0x00);
$font2 = imagecolorallocate($img, 0x3a, 0xdf, 0x00);
$test1 = '3';

imagettftext($img, 12, 0, 12, 50, $font1, './Fire1.ttf', 'Server IP:');
imagettftext($img, 12, 0, 12, 75, $font2, './Fire1.ttf', $config['server']['ip']);
imagettftext($img, 12, 0, 12, 100, $font1, './Fire1.ttf', 'Online Players:');
imagettftext($img, 12, 0, 12, 125, $font2, './Fire1.ttf', "{$info['playerCount']}/{$info['maxPlayers']}");
imagettftext($img, 12, 0, 12, 150, $font1, './Fire1.ttf', 'Status:');
imagettftext($img, 12, 0, 12, 175, $font2, './Fire1.ttf', 'ONLINE');
imagettftext($img, 12, 0, 12, 200, $font1, './Fire1.ttf', 'Players:');
imagepng($img);

?>

Re: Making a Dynamic minecraft status image

Posted: Thu Jun 07, 2012 1:30 am
by tsm200
thanks but i have decited to just leave the image, and do it text.
i got the server status page working only i just uploaded it to my server and on the site it says
that the minecraft server is offline, and locale it still works fine :s do you know what the problem could be?

here is the code:
<?php


//fetches server information from minequery.
function fetch_server_info($ip, $port){
	$socket = @fsockopen($ip, $port, $errno, $errstr, 0.5);
	
	if ($socket === false){
		return false;
	}
	
	fwrite($socket, "QUERY_JSON\n");
	
	$responce = stream_get_contents($socket);
	
	return json_decode($responce, true);
}



$config['server']['ip']    = '66.7.222.221';
$config['server']['port']  = '25566';

$info =  fetch_server_info($config['server']['ip'], $config['server']['port']);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title><?php echo $config['server']['port']; ?>Status</title>
	</head>
	<body>
		<div>
			<p><?php echo $config['server']['ip']; ?></h1></p>
			
			<?php
			
			if ($info === false){
				?>
				<p>
				Status: Offline
				</p>
				<?php
			}else{
				?>
				<p>
					Status: Online
				</p>
				<p>
					Slots <?php echo $info['playerCount'];?> / <?php echo $info['maxPlayers'];?>
				</p>
				<p>
					Players: <?php echo implode(', ', $info['playerList']); ?>
				</p>
				<?php		
				}
			?>
 
			
		</div>
	</body>
</html>

Re: Making a Dynamic minecraft status image

Posted: Thu Jun 07, 2012 1:32 am
by jacek
Try removing the @ from before the fsockopen(), it might be hiding an important error.

Also there is no point defining a function that you only use once, you may as well just inline all of the code or you could create a separate file for the functions which would be more useful.

Re: Making a Dynamic minecraft status image

Posted: Thu Jun 07, 2012 1:35 am
by tsm200
just Warning: fsockopen() [function.fsockopen]: unable to connect to 66.7.222.221:25566 (Connection timed out) in /home/killerpv/public_html/statustsm.php on line 6 :(

Re: Making a Dynamic minecraft status image

Posted: Thu Jun 07, 2012 2:01 am
by jacek
tsm200 wrote:just Warning: fsockopen() [function.fsockopen]: unable to connect to 66.7.222.221:25566 (Connection timed out) in /home/killerpv/public_html/statustsm.php on line 6 :(
You could try increasing the timeout slightly, for testing set it to 30 seconds, if it times out then then I would ask your host if they are blocking connections.