GD Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ReDucTor
Forum Commoner
Posts: 90
Joined: Thu Aug 15, 2002 6:13 am

GD Help

Post by ReDucTor »

I'm drawing a downline tree, sorta like a factor tree, or family tree, you know where you start with one, draw things under, then keep drawing out.

I can't seem to get it to work, any help would be appresiated

Code: Select all

<?
$REQUIRED_LOGIN=0;
$NO_DEBUG=1;
include(dirname(__FILE__)."/common.inc.php");
if(!$this_user)
	fatal_error(GetString("ACCESS_DENIED"));

if(empty($varsї'x']))
	$varsї'x'] = $this_user->uid;

$x = $this_user->GetCopies();
$y=false;
foreach($x as $a)
	if($a->uid == intval($varsї'x']))
		$y = $a;

if($y==false)
	fatal_error(GetString("ACCESS_DENIED"));

$downline = $y->GetDownline();
$user = array('username' => $this_user->dataї'username'],
			  'downline' => $downline);
$font = 3;

function downline_total_spread($user)
{
	$spread=sizeof($userї'downline']);
	if($spread==0)
		return 1;

	foreach($userї'downline'] as $u)
	{
		$x = downline_total_spread($u);
		if($x>$spread)
			$spread = $x;
	}
	return $spread;
}

$max_name_lengths=array();

function fill_in_max_name_lengths($user, $x=0)
{
	global $max_name_lengths;
	if($x==0)
		$max_name_lengthsї0]=strlen($userї'username']);

	$x++;
	foreach($userї'downline'] as $u)
	{
		if($max_name_lenghtsї$x]<strlen($uї'username']))
			$max_name_lengthsї$x] = strlen($uї'username']);

		fill_in_max_name_lengths($u,$x);
	}
}

function get_total_max_name_lengths()
{
	global $max_name_lengths;
	$y=0;
	foreach($max_name_lengths as $x)
		$y+=$x;
	return $y;
}


// x = Position to draw on the x axis
// y = Position to draw from the top of the name
function DrawUser($im, $user, $x, $y, $pos=0)
{
	global $font, $norm_col, $rep_col, $line_col;
	// Move down to the bottom, coz we draw up
	$y+=imagefontwidth($font)*strlen($userї'username']);
	// Store the X
	$end_x=$x;
	// It draws from the corner, so center it, don't want lines on the side
	$x-=(imagefontheight($font)*0.5);
	// Draw the name
	imagestringup($im, $font, intval($x), intval($y), $userї'username'], ($userї'member_id']==$userї'real_member_id']?$norm_col:$rep_col));
	// Store the y axis
	$end_y = $y;
	// Get down to line up the names
	$y+=($max_name_lengthsї$pos+1]*imagefontwidth($font)*0.5);
	// Move the x over to the furthest position, we are wierd, we do
	// right to left, coz im too lasy to do it differently
	$x += $this_spread*imagefontheight(downline_total_spread($user));
	foreach($userї'downline'] as $u)
	{
		// Move it up, to the position for this name
		$draw_y = $y-strlen($uї'username'])*imagefontwidth($font)*0.5;
		// Keeps it looking decent
		$draw_x = $x;
		imageline($im, intval($end_x), intval($end_y), $draw_x, $draw_y, $line_col);
		// Draw the user
		DrawUser($im,$user,$draw_x,$draw_y,$pos+1);
		// Recalculate the x axis, so it lines up right
		$x-=downline_total_spread($u);
	}
}

fill_in_max_name_lengths($user);
$width=downline_total_spread($user)*imagefontheight($font);
$height=get_total_max_name_lengths()*imagefontwidth($font);
$im = @imagecreate ($width, $height)
    or die ("Cannot Initialize new GD image stream");
$bg_col = imagecolorallocate ($im, $bg_red, $bg_green, $bg_blue);
$norm_col = imagecolorallocate ($im, $norm_red, $norm_green, $norm_blue);
$rep_col = imagecolorallocate ($im, $rep_red, $rep_green, $rep_blue);
$line_col = imagecolorallocate($im, $line_red, $line_green, $line_blue);
DrawUser($im, $user, $width*0.5, 0);
header ("Content-type: image/jpeg");
imagejpeg ($im);

?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

By 'I can't seem to get it to work' what exactly do you mean?

How is it working/not working like it shouldn't/should. What in fact does the script do when you run it? Are there any error messages? This sort of information makes it a lot easier for us to help you.

Mac
ReDucTor
Forum Commoner
Posts: 90
Joined: Thu Aug 15, 2002 6:13 am

Post by ReDucTor »

It is ment to draw a tree like thing but instead I just get a "Zero Sized Reply".

But if I comment out "DrawUser()" Within the DrawUser() function, it draws the user, but in completely the wrong place.
Post Reply