Reputation: 47
My php script parses user's profile on other site, takes some info and generates .png image with it for certain user (script.php?username=). Each time page with these images is loaded, script runs again and again. How can I cache images and only run script again if information it outputs was changed? It would save pretty much resources.
Upvotes: 3
Views: 4462
Reputation: 7221
here, you can find how can you cache images using php. You can call these script when you find update from database, otherwise every time image will be load from cache.
// put this above any php image generation code:
session_start();
header("Cache-Control: private, max-age=10800, pre-check=10800");
header("Pragma: private");
header("Expires: " . date(DATE_RFC822,strtotime(" 2 day")));
https://dtbaker.net/blog/web-development/2009/06/how-to-cache-images-generated-by-php/
Upvotes: 2
Reputation: 4042
You will need to parse the user's profile again on each request to find out if something has changed.
You can then throw all the information into some kind of hash like md5($name.$location)
and store this information anywhere. If you now get a request for an image, parse the user's profile, create the hash again and look this hash up. If you have stored it, you previously created the image and can just output it. If the hash is different, the user's information has changed, as well and you will have to recreate the image.
You can also apply some heuristic like the fact that a user might only change his profile once an hour, or even only once a day. With this assumption you can compare the creation date of the user's image and only parse the user's information if the image is older than an hour (or day).
Upvotes: 1
Reputation: 18430
Caching images is probably the easiest caching problem to solve as it is just a matter of saving a local copy of any image to your server after it is generated and checking for a local copy before running the code that generates it.
Something like:-
if(file_exists(image12345.png && !checkIfDataChanged()){
serve cached file;
} else {
generate new file;
save new file to image12345.png;
serve cached file;
}
This pseudo-code ofcourse, but it should be easy enough for you to translate it into PHP.
Upvotes: 3
Reputation: 48887
Cache the image to disk and let Apache take care of the rest.
First, redo your image URI's so they are similar to:
<img src="/images/profiles/johnsmith.png" />
Then, in the /images/profiles/
, place a .htaccess
file with:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.png$ /script.php?username=$1 [QSA,L]
</IfModule>
Then have your script write the resulting png to disk before serving it to the user. Next time the image is requested, it will get it directly from the web server.
When the user's profile info changes, simply delete the existing .png file from the server and your script will be run the next time the image is requested.
If you don't want the web server to be able to write within the web root, write outside of it and have a cron job move them.
Upvotes: 6
Reputation: 41533
set the php headers to inform the browser that the resource is cached :
header("Last-Modified: " . date("D, d M Y H:i:s", getlastmod()));
Upvotes: 2