Cache an Image

GD and GD2 are useful libraries for creating graphics on-the-fly. Discuss your PHP GD and GD2 scripts here.

Moderators: onion2k, General Moderators

Post Reply
derchris
Forum Commoner
Posts: 44
Joined: Sat Jun 10, 2006 6:14 pm

Cache an Image

Post by derchris »

Hi folks,

just something easy, I think.
I have a Script, which retreives some Infos from 3rd party websites, and then generates an Image
out of the data.
Now, it can take some sec. before the Image is created and displayed on the screen.
What I would like to do is cache the Images for like one hour.
Everytime I use the script, it should check if there is a cache version available, check if this is still valid, and display it.
If it isn't vailid anymore, then he should do like always, grab info from Webpage, save it as cache and display it.
How would it be the easiest way to get this working ?

Thanks in advance.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

use a hash of something unique from the request known ahead of time (if the sites vary) or the same filename if they don't and some time reference like filemtime()
derchris
Forum Commoner
Posts: 44
Joined: Sat Jun 10, 2006 6:14 pm

Post by derchris »

I was thinking about doing it with mysql.
Everytime I create the Image, add a date / time in a colum in the DB.
And then on every load check the date / time in the DB against the current date / time.
Depending on the difference, either show the cached Image or create it with new Infos.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Depending what you are doing and the complexity of the image, caching might be more trouble than it is worth. Your biggest overhead in this case is accessing 3rd party websites to collect data. Instead of caching the image itself you might want to consider caching the collected data and making the image on the fly on each access. Whether this is a good idea will depend on your specific case.
derchris
Forum Commoner
Posts: 44
Joined: Sat Jun 10, 2006 6:14 pm

Post by derchris »

Thats the thing.
There are not only Text infos I'm looking at, but Images aswell.
Some of them have to be resized, so I thought I can speed it up by caching the whole thing.
Post Reply