Dynamic Image Generation MIME Types and Caching
Posted: Thu Feb 12, 2009 10:16 pm
Hi,
I have a script that dynamically generates a PNG image based on information in a database. Works fine by setting the src of an img to the php script that generates it <img src="path/myimagegen.php" /> for example.
This works well.
Here are my problems:
1. The image is meant to be used in signatures in forums. Some forum code refuses to render (or maybe even call) the script because it detects the file extension is not an image type file extension (should be JPG or PNG for example, not PHP).
2. The generation of the image is slightly expensive computationally. So I would like to cache the resulting image for all subsequent calls until I programatically clear that cache item (because some specific set of data in the DB changed).
Here are my thoughts:
One approach that comes to mind is: (somehow) writing a custom error handler to "trap" 404's when they come from a specific directory. (The directory where I might store the image files - see below). This will re-direct to the script that will generate the image, place it in the directory for later use, and also return in the output stream the image itself. Then, on subsequent calls, no script is invoked, the image is found and simply fetched. When I wish to clear the cache, I somehow find the appropriate image(s) and delete them. Is this a valid approach? How do I deal with trapping the 404, calling the script and returning the image so the client doesn't even notice?
Is there a better way?
FYI: I am using a typical LAMP setup and have root access to the server.
TIA!
Z.
I have a script that dynamically generates a PNG image based on information in a database. Works fine by setting the src of an img to the php script that generates it <img src="path/myimagegen.php" /> for example.
This works well.
Here are my problems:
1. The image is meant to be used in signatures in forums. Some forum code refuses to render (or maybe even call) the script because it detects the file extension is not an image type file extension (should be JPG or PNG for example, not PHP).
2. The generation of the image is slightly expensive computationally. So I would like to cache the resulting image for all subsequent calls until I programatically clear that cache item (because some specific set of data in the DB changed).
Here are my thoughts:
One approach that comes to mind is: (somehow) writing a custom error handler to "trap" 404's when they come from a specific directory. (The directory where I might store the image files - see below). This will re-direct to the script that will generate the image, place it in the directory for later use, and also return in the output stream the image itself. Then, on subsequent calls, no script is invoked, the image is found and simply fetched. When I wish to clear the cache, I somehow find the appropriate image(s) and delete them. Is this a valid approach? How do I deal with trapping the 404, calling the script and returning the image so the client doesn't even notice?
Is there a better way?
FYI: I am using a typical LAMP setup and have root access to the server.
TIA!
Z.