views:

563

answers:

3

I would like to generate a dynamic image from a script, and then have it load to the browser without being persistent on the server.

However, I cannot call this by setting the image's src="script.php", since that would require running the script that just generated the page and its data all over again, just to get the final data that will generate the graph.

Is there a way to do this that is similar to setting image's src="script.php", but which is called from within another script, and just sends the image without saving it? I need access to the data that is used in the generation of the markup, in order to create this dynamic image.

Or, if not, what is the easiest way to destroy the image once the page is loaded? a quick ajax call?

Is there any way to cache certain data for some limited time frame in order for it to be available to some other script?

Any ideas would be greatly appreciated, as I'm having a really hard time finding the right solution to this...

Thanks!

+5  A: 

You can inline the image into a <img> tag if you need to.
Like

<?php
$final_image_data; // Your image data, generated by GD
$base64_data = base64_encode($final_image_data);
echo "<img src=\"data:image/png;base64,{$base64_data}\" ... />";
?>

That should work on all modern browsers, and IE8. Doesn't work well with some email clients tho (Outlook, for one).

Atli
+1 Did not know about that / never used it. More info on wikipedia as well: http://en.wikipedia.org/wiki/Data_URI_scheme
pygorex1
A: 

Use a rewrite rule.

RewriteRule ^magicimage.jpg$ /myscript.php

Then simply echo your image data from gd, instead of writing it to disk -- which is as simple as not providing a filename to the appropriate image*() function

myscript.php

<?php
$im = imagecreatetruecolor($w, $h);
//...do gd stuff...

header('Content-type: image/jpeg');
//this outputs the content directly to the browser 
//without creating a temporary file or anything
imagejpeg($im);

And finally, utilize the above

display.php

<img src="magicimage.jpg">
Frank Farmer
I'm not sure how that would help... it's not that I can't let people know that I'm running a script to generate the image, but that that script would then generate an image from data that would no longer be existing on the server, since it's just a product of the script that outputs the page, and once the page is output with this reference, that data ceases to be....
msumme
This is a rather redundant thing to do, in any case. You could just as easily have `myscript.php` set the content-type of the response to `image/jpeg` and call the PHP file directly. No decent browser would rely on the extension of the requested file over the content-type of the response.
Atli
Updated to include explicit details.
Frank Farmer
+3  A: 

Also, another solution I found is to store the image in a session variable which is then called from a php script in the image tag. This would allow a user specific image to be served, and then removed from memory by the script... This also avoids messy img src="" tags...

Hopefully that is helpful to someone.

msumme
Good idea. Although you could also save the image into a temporary folder and have the calling script delete it from there. Gives you a bit more control over it, and doesn't bloat the session as much. *(A pet peeve of mine :])*
Atli