Creating an image and sending to the browser

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
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Creating an image and sending to the browser

Post by andym01480 »

I have a script that does a number of things. Other scripts include it and then call functions which output things to the browser.
I'd like to be able to have a function like display_graph() which then sends a graph to the broswer!

That will work if the display graph is in its own file and one does say

Code: Select all

<img src="display_graph.php"/>
But I cannot get a function like say display_graph(); to output the image call and the image

eg

Code: Select all

function display_graph()
{
echo '<img src="'.graph();.'"/>';
}

function graph()
{
// simple working image for now... to demonstarte principle I would like to achieve.
header ("Content-type: image/png");
$newImg = ImageCreate(250,250);
$skyblue = ImageColorAllocate($newImg,136,193,255);
ImageFill($newImg,0,0,$skyblue);
ImagePNG($newImg);
ImageDestroy($newImg);  
}
Any ideas???
DaiLaughing
Forum Commoner
Posts: 76
Joined: Thu Jul 16, 2009 8:03 am

Re: Creating an image and sending to the browser

Post by DaiLaughing »

Is there any reason not to use PHP to create the graph and save it as a file? Then all your script needs to do is put the IMG SRC into the HTML which is the normal way.
Post Reply