Jump to content

Embedding a GD graph in <IMG>


uncleronin

Recommended Posts

I'm using the GD library to generate a bunch of graphs. I am trying to display these graphs inside <IMG> tags. The thing is I don't want to have a separate page for each graph. I also don't want to have to store the image anywhere. Temporarily or no. I also can't pass through parameters in the querystring either since they are bad for security and would otherwise be visible.

 

Originally I tried:

$graph = BuildBarGraph(Parameters); //This generates the actual graph image
echo '<IMG src="'.$graph.'" />';

But this is a bugger because of the way GD builds its images. So I got messages saying must be before output, etc.

 

I then tried:

echo '<IMG src="'.BuildBarGraph(Parameters).'" />';

But that was the same thing in any case so that obviously didn't work >_<

 

Then I tried:

echo '<IMG>'.BuildBarGraph(Parameters).'</IMG>';

But got the same error again.

 

I am really at a loss. To do this using the querystring or individual pages for each graph is simple but not very good because then there'll be tons of graph pages. Can anybody help me with or provide some form of alternative? This is driving me bloody nuts!

Link to comment
https://forums.phpfreaks.com/topic/40499-embedding-a-gd-graph-in/
Share on other sites

Example

 

Page containing graphs

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<META http-equiv="content-language" content="en">
<META name="author" content="Barand">
<META name="generator" content="PHPEd 3.1">
<title>Bar sample</title>
</head>
<body>

<?php
  for ($v = 3; $v <= 5; $v += 0.5) {
       echo "<img src='bar.php?val=$v&max=5'> $v<br>";   // DISPLAY GRAPH
  }

?>
</body>
</html>

 

Code to produce graph

<?php
// set dimensions
     $w = 102;
     $h = 6;
// create image
     $im = imagecreate($w, $h);
// set colours to be used
     $bg = imagecolorallocate($im, 0x00, 0xE0, 0x00);
     $black = imagecolorallocate($im, 0x00, 0x00, 0x00);
     $barcolor  = imagecolorallocate($im, 0xFF, 0x00, 0x00);
// draw border
     imagerectangle($im, 0,0,$w-1,$h-1,$black);
// get value and max value from query string
     $val = $_GET['val'];
     $max = $_GET['max'];
// calculate dimensions of inner bar
     $barw = $max ? floor(($w-2) * $val / $max) : 0;
     $barh = $h - 2;
// draw inner bar
 if ($barw)
     	imagefilledrectangle($im, 1, 1, $barw, $barh, $barcolor);
// send image header
     header("content-type: image/png");
// send png image
     imagepng($im);
     imagedestroy($im);
?>

Thanks for the replies. The main issue was trying to use the same graphing.php page to generate all my graph images. I forgot to mention that I'm building my graphs and charts using jpgraph. I tried using basic session variables but because only the last values for the variables was used when the graphs were drawn, the same graph would be drawn multiple times. So then I was forced to use a session variable storing an array of graph values and then it worked using $_GET to get each graphs position in the array.

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.