简体   繁体   中英

Caching dynamic image generated on php

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.

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.

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.

设置php标头以通知浏览器资源被缓存:

header("Last-Modified: " . date("D, d M Y H:i:s", getlastmod()));

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/

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM