Jump to content

[SOLVED] Passing a variable set to a GD drawing php file


Kurrel

Recommended Posts

Hi Freaks,

 

I've tried to figure this one out through asking pertinent questions and reading up on material as much as possible but I have run up against a brick wall around which my experience cannot get me.

 

Here is the whole sale file linegraphdrawer.php :

 

<?php
// get details {
// Set 1 {
	$values[1]["Jan"]	= 500;
	$values[1]["Feb"]	= 200;
	$values[1]["Mar"]	= 260;
	$values[1]["Apr"]	= 330;

	$values[2]["Feb"]	= 210;
	$values[2]["Mar"]	= 330;
	$values[2]["Apr"]	= 340;
	$values[2]["May"]	= 320;
	$values[2]["Jun"] = 280;
	$values[2]["Jul"] = 285;

	$values[3]["Jan"]	= 295;
	$values[3]["Feb"]	= 130;
	$values[3]["Mar"]	= 240;
	$values[3]["May"]	= 220;
	$values[3]["Jun"] = 180;
	$values[3]["Jul"] = 200;

	$values[4]["Jan"]	= 205;
	$values[4]["Feb"]	= 150;
	$values[4]["Mar"]	= 245;
	$values[4]["Apr"]	= 280;
	$values[4]["May"]	= 310;
	$values[4]["Jun"] = 360;
	$values[4]["Jul"] = 415;

	$baselinevalues[]	= "Jan";
	$baselinevalues[]	= "Feb";
	$baselinevalues[]	= "Mar";
	$baselinevalues[]	= "Apr";
	$baselinevalues[]	= "May";
	$baselinevalues[]	= "Jun";
	$baselinevalues[]	= "Jul";
	$baselinevalues[]	= "Aug";

	$height			= 400;
	$width			= 750;

	$rows				= 5;

	$barwidth		= 15;
	$bargap			= 10;
	$bar3d			= 1;
	$barshadow	= 0;

	$topval			= 600;
	$bottomval	= 100;
	$text = "Line Graph Example 1";
// }
// }
// Preparations {
$maxheight	= 0;
foreach ($values as $divkey=>$divval) {
	foreach ($divval as $barkey=>$barval) {
		if($barval > $maxheight) {
			$maxheight = $barval;
		}
	}
}
// }

// create image {
$im = imagecreate($width+1, $height+1);
// }
// set style (colours, fonts, etc) to be used {
// Font {
	$font = "verdana.ttf";
	if($width < 200) {
		$linefontsize		= 6;
		$headerfontsize	= 10;
	} else if($width < 450) {
		$linefontsize		= 8;
		$headerfontsize	= 14;
	} else {
		$linefontsize		= 10;
		$headerfontsize	= 18;
	}
// }

// Colours {
	$white		= imagecolorallocate($im, 255, 255, 255);
	//$bg = imagecolorallocate($im, hexdec('0x' . $colour{0} . $colour{1}), hexdec('0x' . $colour{2} . $colour{3}), hexdec('0x' . $colour{4} . $colour{5}));
	$bg					= imagecolorallocate($im, 200, 200, 200);
	$black			= imagecolorallocate($im, 0x00, 0x00, 0x00);
	$red				= imagecolorallocate($im, 255, 0, 0);
	$yellow			= imagecolorallocate($im, 255, 255, 0);
	$green			= imagecolorallocate($im, 40, 150, 40);
	$blue				= imagecolorallocate($im, 90, 90, 255);
	$colourlist	= array("1"=>$green, "2"=>$yellow, "3"=>$red, "4"=>$blue, "5"=>$black);
	$barcolor		= imagecolorallocate($im, 0xFF, 0x00, 0x00); // Fore colour
// }
// }

// draw backdrop {
//imagerectangle($im, 0,0,$width+1,$height+1,$black);
imageline($im, 5, 0, $width-5, 0, $black); // Top
imageline($im, 0, 5, 0, $height-5, $black); // Right
imageline($im, $width, 5, $width, $height-5, $black); // Left
imageline($im, 5, $height, $width-5, $height, $black); // Bottom

imagearc($im, 4, 4, 10, 10, 181, 269, $black); // Top left
imagearc($im, $width-4, 4, 10, 10, 271, 359, $black); // Top right
imagearc($im, 4, $height-4, 10, 10, 91, 179, $black); // Bottom left
imagearc($im, $width-4, $height-4, 10, 10, 1, 89, $black); // Top left
// imagearc(resource, x, y, height, width, degree start, degree end, colour)

imagefill($im, 20, 20, $bg);
// }

// Behind-the-scenes data {
/*
imagettftext($im, $linefontsize, 0, 350, 25, $black, $font, "Topval : ".$topval.", Bottomval : ".$bottomval);
imagettftext($im, $linefontsize, 0, 350, 40, $black, $font, "Graph height : ".$height);
*/
$basecount	= count($baselinevalues)+1; // The number of lines, + 1 so a row set is not presented at the final coordinate of x;

  $valrange	 	= $topval - $bottomval; // The range of values, as from least (bottom of graph) to most (top of graph)
$ratio			= $height / $valrange;
$difinc			= round(($width / $basecount), 2); // The pixel increment between each division of $baselinevalues;
// }

// Draw dividing lines {
$valinc = ($topval - $bottomval) / $rows; // The value increment between each line; 
$pixinc = $height / $rows; // The pixel increment between each line;
//imagettftext($im, $linefontsize, 0, 350, 80, $black, $font, "Rows : ".$rows.", Valinc : ".$valinc.", Pixinc : ".$pixinc);
for ($linecount=0; $linecount < $rows; $linecount++) {
	$linepos	= $height - (($linecount * $pixinc));
	$linetext	= ($linecount * $valinc) + $bottomval;
	imagettftext($im, $linefontsize, 0, 5, ($linepos-1), $black, $font, $linetext);
	if($linecount != 0) {
		imageline($im, 0, $linepos, $width, $linepos, $black);
	}
}
// }

// Draw the base values across the graph bottom {
$basecount = 1;
foreach ($baselinevalues as $basekey=>$baseval) {
	imagettftext($im, $linefontsize, 0, $basecount*$difinc, ($height - 1), $black, $font, $baseval);
	$basecount++;
}
// }

// {
if($_REQUEST) {
	imagettftext($im, $linefontsize, 0, 400, 20, $black, $font, "TEST");
	$reqcount = 2;
	foreach ($_REQUEST as $reqkey=>$reqval) {
		imagettftext($im, $linefontsize, 0, 400, ($reqcount*20), $black, $font, "TEST ".$reqkey." ".$reqval);
		$reqcount++;
		if($reqkey=="test") {
			$blahcount = 150;
			//foreach ($reqval as $rkey=>$rval) {
				imagettftext($im, $linefontsize, 0, 400, $blahcount, $black, $font, "TEST");
				$blahcount += 20;
			//}
		}
	}
}
// }

// draw the data lines {
//imagettftext($im, $linefontsize, 0, 600, 20, $black, $font, $basecount." ".$difinc);
$linecount	= 1;
foreach ($values as $linekey=>$lineval) {
	$oldx				= "x";
	$oldy				= "x";
	$pointcount = 1;
	foreach ($baselinevalues as $pointkey=>$pointval) {
		if($lineval[$pointval]) {
			$ypos = $height - (($lineval[$pointval] - $bottomval) * $ratio);
			$xpos = $pointcount*$difinc;
			imagefilledarc($im, $xpos, $ypos, 7, 7, 1, 360 , $colourlist[$linecount], IMG_ARC_PIE);
			if($oldx != "x") {
				imageline($im, $oldx, $oldy, $xpos, $ypos, $colourlist[$linecount]);
			}

			$oldx = $xpos;
			$oldy = $ypos;
		} else {
			$oldx = "x";
			$oldy = "x";
		}
		$pointcount++;
	}
	$linecount++;
}
// }

// Title {
// Add show beneath text
imagettftext($im, $headerfontsize, 0, 11, 21, $black, $font, $text);

// Add text
imagettftext($im, $headerfontsize, 0, 10, 20, $blue, $font, $text);
// }

// send image header and png image {
header("content-type: image/png");
imagepng($im);
imagedestroy($im);
// }
?>

 

It works, but that is the sum of it.  I cannot pass any data to it except via the URL and $_GET, which is too short if I am to pass even a few sets of 12, along with dimensions.

 

It cannot view sessions, as it is not included, nor $_REQUEST.  If I write it to database and pass the reference, the attempt at pulling data throws the code... possibly because it sends a header to the DB.

 

I have run through what I feel I can do, and if anyone can help by making it work for them and explaining how, I would be very grateful.

By wrapping the linegraphdrawer.php in this

<?php
class drawlinegraph {
function drawLineGraph($data) {

 

I am able to pass data, and get the spewed image in ascii by commenting out

//header("content-type: image/png");

 

I feel I'm close, and if I can just work the data and header / image drawing apart into different classes, I might meet with success.  Please, help.

Okay.  I know that I need to prepare or create the gd resource before or outside the headers.

 

I think that classes could be used to do it, but I cannot figure out how to do this via PHP.  I believe it an be allocated to a object resource, but I have not been able to create that effect.

 

What I have is a working piece of code that I want to expand on but cannot.

 

Several tutorials have referenced what to do inside the php file, but not then how to call it leaving me in the same boat I was in when I could call it but not pass data to it.

 

I don't want to give up on developing my own understanding of PHP graphics and graphing, but I cannot make further progress myself.  I would appreciate even a new tutorial that is clearer than the ones I have been able to find as I begin to believe there is a flaw in my understanding that I cannot grasp.

I was able to solve this by creating a .png file and writing all the graphics to it, using a passed name as well.  In the next line of the calling function I presented the <img> with the name that had been sent through to the graphdrawing class functions.

 

I am able to bypass the head issues this way and it's been clean sailing thus far.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.