1

I'm trying to write a wrapper around Image_Graph bar charts. I actually wrote the whole thing but in the end didn't work, so now I'm back to the basics. This code below is taken directly from the ImageGraph website as a bar chart example. I just wrapped it in a function (where $data is ignored for now).

This function works fine if I call it from the same file it's defined in (say, function.php). However, if I make a second file, caller.php where I just include_once or require_once this file function.php and then just call the function, it breaks.

Can anyone help me? I'm probably doing some basic mistake but I just can't figure it out. It might also be something specific to Image_Graph because if I make a simple function that just prints Hello World and call that from another file, it works fine.

Thanks, Daniel

<?php
include_once('Image/Graph.php');

function DrawBarChart($width, $height, $data)
{
 // create the graph
 $graph = Image_Graph::factory('graph', array($width, $height));

 // create the plotarea
 $plotArea =& $graph->addNew('plotarea');

 // create a dataset
 $dataset =& Image_Graph::factory('dataset');

 // add points
 $dataset->addPoint('Denmark', 10); 
 $dataset->addPoint('Norway', 3); 
 $dataset->addPoint('Sweden', 8); 
 $dataset->addPoint('Finland', 5); 

 $plot =& $plotArea->addNew('bar', &$dataset); 

 $graph->done();
}
?> 
3
  • 3
    Daniel - first thing, could you be more specific than "it breaks"? :) Pasting the error message or how it's broken would be useful in coming up with a good answer. Commented Mar 5, 2010 at 1:56
  • When I say it breaks it actually means different things depending on what I'm trying to do :) - If I try an empty script that only calls that function - On IE I get an empty page with the broken-link image on the top-left corner. - On Firefox I get an error message "The image “localhost/test.php” cannot be displayed, because it contains errors." - If I include this function call in another page I have that already shows some graphs I get what looks like a binary output converted to text: �PNG ��� IHDR��,�������ݽK Commented Mar 5, 2010 at 2:32
  • Do you have PHP errors turned on? The PHP errors would be useful to see. Commented Mar 5, 2010 at 4:09

2 Answers 2

2

It seems you are trying to output the image through your php file itself. Do you have a line in your original script which reads like header('Content-Type:image/jpeg') ?

You might want to just write the image to a directory and then load it via img src='path' on html instead of just loading it via php.

Sign up to request clarification or add additional context in comments.

1 Comment

That's it. I just added an <img> tag in my caller script and it worked perfectly. <img src="function.php"> Thanks s1d :)
0

Check to make sure the files are both in the same location. If they are in different directories, that can cause a problem if your included file isn't picking up the library from an include path.

1 Comment

They are in different directories but I even tried setting absolute paths on my includes. Thing is, it seems to find the files alright, because if I change the function to something simpler, like printing Hello World, it works fine.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.