Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load dynamic image w/php gd library, w/o saving it on server or having src="script.php"?

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!

like image 252
msumme Avatar asked Dec 23 '22 07:12

msumme


1 Answers

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).

like image 140
Atli Avatar answered Mar 01 '23 14:03

Atli