alxgfx Posted December 13, 2010 Report Share Posted December 13, 2010 I have a php file (text.php) that will create text to image. ie domain.com/txt.php?text=hello will outputs an image with hello written on it. I want that image to be cached as it is called very often. So where did this caching occurs? Browser or in server. Is it possible to implement dynamically generated image caching in server. If so please share the headers to achieve this. Thanks in advance. Nb. This is the content of text.php file <?php $txt=$_GET["text"]; $im = imagecreatetruecolor(120, 20); $text_color = imagecolorallocate($im, 255, 255, 255); imagestring($im, 10, 1, 1, $txt, $text_color); header('Content-type: image/jpeg'); imagejpeg($im); imagedestroy($im); ?> Quote Link to comment Share on other sites More sharing options...
administrator Posted December 13, 2010 Report Share Posted December 13, 2010 Hi, This is less a problem of programming and more of strategy: Unless you are generating a unique image every time to page is loaded, why not just use a static image? ... Or maybe have a folder of images and just load those randomly every time the page loads. Images are automatically cached by the web browser regardless. I hope that helps, Stefan Quote Link to comment Share on other sites More sharing options...
alxgfx Posted December 13, 2010 Author Report Share Posted December 13, 2010 Stefan, thank you for the reply. The problem is the image and its contents are user generated. Is there anyway to cache the image generated, sothat if a 2nd user comes with the same querry, the previously generated image will be automatically displayed without executing the code? Quote Link to comment Share on other sites More sharing options...
falkencreative Posted December 13, 2010 Report Share Posted December 13, 2010 Is there anyway to cache the image generated, sothat if a 2nd user comes with the same querry, the previously generated image will be automatically displayed without executing the code? I would think so... What if you placed all of your dynamically generated files in one location, and named them consistently (for example: "img_imagetexthere.jpg"). When the script was called, you could check if the file exists. If so, you return that file using header(), similar to what you are doing now. If not, you create the file, save it, and then return the file. Quote Link to comment Share on other sites More sharing options...
kraxzy Posted January 1, 2011 Report Share Posted January 1, 2011 Stefan, thank you for the reply. The problem is the image and its contents are user generated. Is there anyway to cache the image generated, sothat if a 2nd user comes with the same querry, the previously generated image will be automatically displayed without executing the code? Yes it's called dynamic programming and used a lot in other languages when it comes to reducing the memory and time consumption of recursive methods. Which basically means that you keep a list of operations that you've already excecuted, a easy example would if you already had 2 and 2 and the code is calculating the sum of the provided entries, you check your list if you already had these, and if so fetch the result instead of redoing the calculation ( note that this is a basic example where having a dynamic solution would be useless for something trivial as occasional addition operations ). So you could solve it in a similar fasion, where you store the images for some time, let say if the same image hasn't been generated again within 24 h or something like that you take it away from the list ( as you need to make sure that having a list, and searching in it won't give you a greater time complexity than generating the images in the first place ). Then each time a user specifies the data you check the list if this set of data already has been generated, if it has just smack up the link to the saved image. If not generate it and add it to the list. Then just add a cron job that flushes the unused data at a time where the server isn't busy. 1 Quote Link to comment Share on other sites More sharing options...
alxgfx Posted January 2, 2011 Author Report Share Posted January 2, 2011 Yes it's called dynamic programming and used a lot in other languages when it comes to reducing the memory and time consumption of recursive methods. Which basically means that you keep a list of operations that you've already excecuted, a easy example would if you already had 2 and 2 and the code is calculating the sum of the provided entries, you check your list if you already had these, and if so fetch the result instead of redoing the calculation ( note that this is a basic example where having a dynamic solution would be useless for something trivial as occasional addition operations ). So you could solve it in a similar fasion, where you store the images for some time, let say if the same image hasn't been generated again within 24 h or something like that you take it away from the list ( as you need to make sure that having a list, and searching in it won't give you a greater time complexity than generating the images in the first place ). Then each time a user specifies the data you check the list if this set of data already has been generated, if it has just smack up the link to the saved image. If not generate it and add it to the list. Then just add a cron job that flushes the unused data at a time where the server isn't busy. Thank you for taking much time in explaining. I already got exact solution from another forum. The cron job tip from your part makes things more easier. Thumbs up Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.