including generated png graph

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
paulmcg
Forum Newbie
Posts: 3
Joined: Fri Oct 05, 2007 6:27 am

including generated png graph

Post by paulmcg »

Hi there, first post with a question. Hope it's not too silly.

I've got a nice frame consisting of a table within a table, links to the left and whatever link is clicked on that php page is included on the right.

This has been working fine so far for a lot of things however I'm using a few pages to gather information via forms from a user which then go and query a db and generate a graph of the users' choice. This graph is a png file.

Now my problem is that i can only make a link to the png file and view solely the graph and nothing else however if i try and include the page which generates and displays the png file all i get is the code which one would get if they tried to open a png file using notepad in the middle of my table.

How do I get the image in the table and not the code "‰PNG ... etc"? Is it because I'm using include?
User avatar
N1gel
Forum Commoner
Posts: 95
Joined: Sun Apr 30, 2006 12:01 pm

image

Post by N1gel »

If you generate a PNG can you not just show it like you would a normal image.

Code: Select all

<img src="graph.png" alt="Graph" />
http://www.w3schools.com/tags/tag_img.asp
paulmcg
Forum Newbie
Posts: 3
Joined: Fri Oct 05, 2007 6:27 am

Post by paulmcg »

that would be great, if i could but my code looks something like this, I've truncated it for simplicity

Code: Select all

$im = imagecreatetruecolor($width,$height );

//then lots of stuff to generate the graph

//part to display the graph

Header('Content-type: image/png');
Imagepng ($im);
echo $im;
Now if i create it so my browser goes to this page it's fine. But all i can see is the image and nothing else.

If I want it so my image is embedded in my webpage all i get is that nasty text i mentioned earlier.
User avatar
N1gel
Forum Commoner
Posts: 95
Joined: Sun Apr 30, 2006 12:01 pm

Post by N1gel »

Ok i've never worked with these image things. But based on what i have just read on the php website could you save the image as a file and reference that.

imagecreatetruecolor

imagepng

Code: Select all

$im = imagecreatetruecolor($width,$height );

//then lots of stuff to generate the graph

//part to display the graph

Imagepng ($im,'TempGraph.png');

echo "<img src='TempGraph.png' alt='Graph' />";
Don't know if that will help
paulmcg
Forum Newbie
Posts: 3
Joined: Fri Oct 05, 2007 6:27 am

Post by paulmcg »

That seems to have solved that problem

Thanks very much!
Post Reply