Caching a dynamic image generated in php

My PHP script parses a user profile on another site, takes some information and generates a .png image with it for a specific user (script.php? Username =). Each time the page with these images is loaded, the script is run over and over. How can I cache the images and run the script again if the information it outputs has changed? This will save tons of resources.


source to share

5 answers

You will need to analyze the user profile again for each request to see if something has changed.

Then you can pass all the information into some kind of hash, for example md5($name.$location)

, and store that information anywhere. If you now receive a request for an image, parse the user profile, generate the hash again and see that hash. If you saved it, you created an image earlier and can simply output it. If the hash is different, the user's information has also changed and you will have to recreate the image.

You can also apply some heuristics, such as the fact that a user can only change their profile once an hour, or even 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 a day).



Download the image to disk and let Apache do the rest.

First, change your image URI so that they look like:

<img src="/images/profiles/johnsmith.png" />


Then in /images/profiles/

place the file .htaccess


<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.png$ /script.php?username=$1 [QSA,L]


Then ask the script to write the resulting png to disk before serving it to the user. The next time the image is requested, it will receive it directly from the web server.

When the user's profile information changes, just delete the existing .png file from the server and your script will run on the next image request.

If you don't want the web server to be able to write to the network root, write outside of it and run a cron job.



Image caching is probably the simplest caching problem as it is simply a matter of saving a local copy of any image to your server after it has been created, and checking the 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 is pseudocode, but it should be easy enough for you to translate it into PHP.



set php headers to tell the browser that the resource is cached:

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




here you can find a way to cache images using php. You can call these script when you find an update from the database, otherwise the image will be loaded from the cache every time.

// put this above any php image generation code:
header("Cache-Control: private, max-age=10800, pre-check=10800");
header("Pragma: private");
header("Expires: " . date(DATE_RFC822,strtotime(" 2 day")));



All Articles