How to load a dynamic image library w / php gd without saving it to the server or using src = "script.php"?

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

However, I can’t call it by setting the image src = "script.php", because for this you will need to run the script that just generated the page and its data again, just to get the final data that the graph will generate.

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

Or, if not, what is the easiest way to destroy an image after loading a page? ajax quick call?

Is there a way to cache certain data for a limited period of time so that it can be accessed by some other script?

Any ideas would be highly appreciated, since it is very difficult for me to find the right solution for this ...

Thanks!

+4
source share
3 answers

You can embed the image in <img> if you need to.
how

<?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}\" ... />"; ?> 

This should work on all modern browsers and IE8. Does not work with some tho email clients (Outlook, for one).

+8
source

Also, I found another solution - save the image in a session variable, which is then called from the php script in the image tag. This will allow serving a specific user image, and then it will be deleted from memory using a script ... It will also avoid messy img src = "" tags ...

Hope this helps someone.

+3
source

Use a rewrite rule.

 RewriteRule ^magicimage.jpg$ /myscript.php 

Then just echo your image data from gd, instead of writing it to disk - it's as simple as not specifying the file name for the corresponding image * ()

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 use the above

display.php

 <img src="magicimage.jpg"> 
-one
source

All Articles