72f4ba51b784673a15a1e89d8d9f49d1

This code is the one-shot result of generating these images, and results can be be found and tested using these URLs:

http://www.thegrandtournament.com/rank.php?id=4
http://www.thegrandtournament.com/rank.php?id=4&format=large

<?php
header("Content-type: image/png");

// get the connection information
require('config.php');

// for logging the requesting site and page, not currently used
/*
if (substr($_SERVER['HTTP_REFERER'], 0, 33) == "http://www.thegrandtournament.com")
{

} else {
	$myFile = "log.txt";
	$fh = fopen($myFile, 'a') or die("can't open file");
	$stringData = "\n".$_SERVER['HTTP_REFERER'];
	fwrite($fh, $stringData);
	fclose($fh);
}
*/

function number_suffix($number)
{
	// Validate and translate our input
	if (is_numeric($number)){
		// Get the last two digits (only once)
		$n = $number % 100;
	} else {
		// If the last two characters are numbers
		if (preg_match( '/[0-9]?[0-9]$/', $number, $matches ))
		{
			// Return the last one or two digits
			$n = array_pop($matches);
		} else {
			// Return the string, we can add a suffix to it
			return $number;
		}
	}
	// Skip the switch for as many numbers as possible.
	if (($n > 3) && ($n < 21))
	{
		return $number . 'th';
	}
	// Determine the suffix for numbers ending in 1, 2 or 3, otherwise add a 'th'
	switch ($n % 10)
	{
		case '1': return $number . 'st';
		case '2': return $number . 'nd';
		case '3': return $number . 'rd';
		default:  return $number . 'th';
	}
}

if (!mysql_connect($dbhost,$dbuser,$dbpasswd))
{
	die(mysql_error());
}
if (!mysql_select_db($dbname))
{
	die(mysql_error());
}

//get string from URL, member id
$id = mysql_real_escape_string(intval($_GET['id']));
$format = mysql_real_escape_string($_GET['format']);

$sql = "SELECT * FROM grand_users
	WHERE user_id = ".$id.";
";

if( !($result = mysql_query($sql)) )
	{
		die(mysql_error());
	}
else 
{
	$row = mysql_fetch_assoc($result);
}
$sql = "
SELECT * FROM grand_users ORDER BY games_points DESC, games_won DESC, games_lost ASC, username
	;
";

if( !($result = mysql_query($sql)) )
	{
		die(mysql_error());
	}

if (!$username = $row['username']) $username = "No such user!";
if (!$points = $row['games_points']) $points = 0;
if (!$record = ($row['games_won'] . "-" . $row['games_lost'])) $record = "0-0";

switch ($row['user_avatar_type'])
{
	case "1":
		$avatar = "images/avatars/".$row['user_avatar'];
	break;
	case "2":
	
		$handle = fopen($row['user_avatar'], "rb");
		$contents = stream_get_contents($handle);
		fclose($handle);
		
		$user_avatar = substr($row['user_avatar'],strrpos($row['user_avatar'],"/"));
		
		$fh = fopen('images/avatars/remote/'.$user_avatar, 'w+');
		fwrite($fh, $contents);
		fclose($fh);
	
		$avatar = "images/avatars/remote/".$user_avatar;
	break;
}

$current = 1;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
	if ($row["user_id"] === $id)
	{
		$rank = $current;
	}
	$current++;
}
$total = mysql_num_rows($result);

mysql_close();

//$rank = array_search($username,$all) + 1;

switch ($format)
{
	default:
	
		if ($rank) $rank = $rank . " of " . $total;
	
		$image = imagecreatefrompng('images/redblue.png');

		$white = imagecolorallocate($image,255,255,255);
		$black = imagecolorallocate($image,0,0,0);
		
		$font = 'images/visitor-tt2-brk.ttf';

		//imagettftext($image,12,0,200,12,$white,$font,'GT League Points:');
		imagettftext($image,12,0,320,12,$white,$font,$record);
		imagettftext($image,12,0,10,12,$white,$font,$username);
		imagettftext($image,12,0,200,12,$white,$font,$rank);
	break;
	case large:
	
		$tagline = number_suffix($rank)." in the league, with a ".$record." record.";
	
		$image = imagecreatefrompng('images/blue-468x60.png');
		
		if (!file_exists($avatar))
		{
			$avatar = 'templates/MSSimplicity/images/welcome.gif';
		}
		
		//determine the file extension
		$avtype = substr($avatar,-4);
		switch ($avtype)
		{
			default:
				$insert = imagecreatefromgif('templates/MSSimplicity/images/welcome.gif');
			break;
			case ".gif":
				$insert = imagecreatefromgif($avatar);	
			break;
			case ".png":
				$insert = imagecreatefrompng($avatar);		
			break;
			case ".jpg":
				$insert = imagecreatefromjpeg($avatar);
			break;
			//a little different because JPGs can have both JPEG and JPG extensions
			case "jpeg":
				$insert = imagecreatefromjpeg($avatar);
			break;
		}
		
		$promo = rand(1,1);
		switch ($promo)
		{
			case 1:
				$promo = "Spar me and get ranked.";
			break;
		}
		
			
		$font = 'images/visitor-tt2-brk.ttf';
		$altfont = 'images/kartika.ttf';
		
		// Get overlay image width and hight for later use
		$insert_x = imagesx($insert); 
		$insert_y = imagesy($insert); 
		
		$box = @imageTTFBbox(12,0,$font,$promo);
		$textwidth = abs($box[4] - $box[6]);
		$xcord = 460 - $textwidth; // 2 = some space from right side.
		
		$percentage = 58 / $insert_y;
		
		$newheight = $insert_y * $percentage;
		$newwidth = $insert_x * $percentage;
		
		$tagstart = $newwidth + 10;
		
		imagecopyresized($image,$insert,1,1,0,0,$newwidth,58,$insert_x,$insert_y);

		$white = imagecolorallocate($image,255,255,255);
		$black = imagecolorallocate($image,0,0,0);

		//imagettftext($image,12,0,200,12,$white,$font,'GT League Points:');
		// font size,
		
		$bottom = imageSY($image) - 8;
		
		imagettftext($image,16,0,$tagstart,16,$white,$font,$username);	
		imagettftext($image,14,0,$tagstart,$bottom,$white,$font,$tagline);
		imagettftext($image,12,0,$xcord,12,$white,$font,$promo);
	break;
}
imagepng($image);
?>

Refactorings

No refactoring yet !

72f4ba51b784673a15a1e89d8d9f49d1

Martindale

November 5, 2007, November 05, 2007 04:40, permalink

No rating. Login to rate!

Updated per current code.

<?php
header("Content-type: image/png");

require('config.php');

if (substr($_SERVER['HTTP_REFERER'], 0, 33) == "http://www.thegrandtournament.com")
{

} else {
	$myFile = "log.txt";
	$fh = fopen($myFile, 'a') or die("can't open file");
	$stringData = "\n".$_SERVER['HTTP_REFERER'];
	fwrite($fh, $stringData);
	fclose($fh);
}

function number_suffix($number)
{
	// Validate and translate our input
	if (is_numeric($number)){
		// Get the last two digits (only once)
		$n = $number % 100;
	} else {
		// If the last two characters are numbers
		if (preg_match( '/[0-9]?[0-9]$/', $number, $matches ))
		{
			// Return the last one or two digits
			$n = array_pop($matches);
		} else {
			// Return the string, we can add a suffix to it
			return $number;
		}
	}
	// Skip the switch for as many numbers as possible.
	if (($n > 3) && ($n < 21))
	{
		return $number . 'th';
	}
	// Determine the suffix for numbers ending in 1, 2 or 3, otherwise add a 'th'
	switch ($n % 10)
	{
		case '1': return $number . 'st';
		case '2': return $number . 'nd';
		case '3': return $number . 'rd';
		default:  return $number . 'th';
	}
}

if (!mysql_connect($dbhost,$dbuser,$dbpasswd))
{
	die(mysql_error());
}
if (!mysql_select_db($dbname))
{
	die(mysql_error());
}

//get string from URL, member id
$id = mysql_real_escape_string(intval($_GET['id']));
$format = mysql_real_escape_string($_GET['format']);

$sql = "SELECT * FROM grand_users
	WHERE user_id = ".$id.";
";

if( !($result = mysql_query($sql)) )
	{
		die(mysql_error());
	}
else 
{
	$row = mysql_fetch_assoc($result);
}
$sql = "
SELECT * FROM grand_users ORDER BY games_points DESC, games_won DESC, games_lost ASC, username
	;
";

if( !($result = mysql_query($sql)) )
	{
		die(mysql_error());
	}

if (!$username = $row['username']) $username = "No such user!";
if (!$points = $row['games_points']) $points = 0;
if (!$record = ($row['games_won'] . "-" . $row['games_lost'])) $record = "0-0";

switch ($row['user_avatar_type'])
{
	case "1":
		$avatar = "images/avatars/".$row['user_avatar'];
	break;
	case "2":
	
		$handle = fopen($row['user_avatar'], "rb");
		$contents = stream_get_contents($handle);
		fclose($handle);
		
		$user_avatar = substr($row['user_avatar'],strrpos($row['user_avatar'],"/"));
		
		$fh = fopen('images/avatars/remote/'.$user_avatar, 'w+');
		fwrite($fh, $contents);
		fclose($fh);
	
		$avatar = "images/avatars/remote/".$user_avatar;
	break;
}

$current = 1;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
	if ($row["user_id"] === $id)
	{
		$rank = $current;
	}
	$current++;
}
$total = mysql_num_rows($result);

mysql_close();

//$rank = array_search($username,$all) + 1;

switch ($format)
{
	default:
	
		if ($rank) $rank = $rank . " of " . $total;
	
		$image = imagecreatefrompng('images/redblue.png');

		$white = imagecolorallocate($image,255,255,255);
		$black = imagecolorallocate($image,0,0,0);
		
		$font = 'images/visitor-tt2-brk.ttf';

		//imagettftext($image,12,0,200,12,$white,$font,'GT League Points:');
		imagettftext($image,12,0,320,12,$white,$font,$record);
		imagettftext($image,12,0,10,12,$white,$font,$username);
		imagettftext($image,12,0,200,12,$white,$font,$rank);
	break;
	case large:
	
		$tagline = number_suffix($rank)." in the league, with a ".$record." record.";
	
		$image = imagecreatefrompng('images/blue-468x60.png');
		
		if (!file_exists($avatar))
		{
			$avatar = 'templates/MSSimplicity/images/welcome.gif';
		}
		
		//determine the file extension
		$avtype = substr($avatar,-4);
		switch ($avtype)
		{
			default:
				$insert = imagecreatefromgif('templates/MSSimplicity/images/welcome.gif');
			break;
			case ".gif":
				$insert = imagecreatefromgif($avatar);	
			break;
			case ".png":
				$insert = imagecreatefrompng($avatar);		
			break;
			case ".jpg":
				$insert = imagecreatefromjpeg($avatar);
			break;
			//a little different because JPGs can have both JPEG and JPG extensions
			case "jpeg":
				$insert = imagecreatefromjpeg($avatar);
			break;
		}
		
		$promo = rand(1,1);
		switch ($promo)
		{
			case 1:
				$promo = "Spar me and get ranked.";
			break;
		}
		
			
		$font = 'images/visitor-tt2-brk.ttf';
		$altfont = 'images/kartika.ttf';
		
		// Get overlay image width and hight for later use
		$insert_x = imagesx($insert); 
		$insert_y = imagesy($insert); 
		
		$box = @imageTTFBbox(12,0,$font,$promo);
		$textwidth = abs($box[4] - $box[6]);
		$xcord = 460 - $textwidth; // 2 = some space from right side.
		
		$percentage = 58 / $insert_y;
		
		$newheight = $insert_y * $percentage;
		$newwidth = $insert_x * $percentage;
		
		$tagstart = $newwidth + 10;
		
		imagecopyresized($image,$insert,1,1,0,0,$newwidth,58,$insert_x,$insert_y);

		$white = imagecolorallocate($image,255,255,255);
		$black = imagecolorallocate($image,0,0,0);

		//imagettftext($image,12,0,200,12,$white,$font,'GT League Points:');
		// font size,
		
		$bottom = imageSY($image) - 8;
		
		imagettftext($image,16,0,$tagstart,16,$white,$font,$username);	
		imagettftext($image,14,0,$tagstart,$bottom,$white,$font,$tagline);
		imagettftext($image,12,0,$xcord,12,$white,$font,$promo);
	break;
}
imagepng($image);
?>
2cc1933faea9b89b9418143cef9a9efe

Sandoze

November 6, 2007, November 06, 2007 17:48, permalink

No rating. Login to rate!

Sorry I don't have time for a rewrite. I assume things work well. The biggest change I would make is to break that one gigantic function in to several smaller private functions. But that's just me. If I have a function that is doing to many things I break it into smaller chunks.. It makes it easier to debug, change, test.. etc.. not to mention readable.

Your refactoring





Format Copy from initial code

or Cancel