Dynamic Image Generation MIME Types and Caching

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
M4ngo
Forum Newbie
Posts: 6
Joined: Fri Jul 18, 2008 8:27 pm

Dynamic Image Generation MIME Types and Caching

Post by M4ngo »

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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Dynamic Image Generation MIME Types and Caching

Post by John Cartwright »

A common way to circumvend the forum file extension check is to run your png (or whatever image type) through the php interpreter. By adding:

Code: Select all

 
AddType application/x-httpd-php .png
in your .htaccess file, then all requests to .png will transparently be processed by php.

I'm not exactly sure how you wanted to determine whether the image has already been generated or invalidated, so I will leave that for you to implement in the example. My point being, is you should save the file on the filesystem until it has been invalidated. Until that happens, simply re-read the cached version as follows.

Code: Select all

header("Content-Type: image/png");
 
if ($is_cached_flag === false) {
   //generate the file and save it on the filesystem
} 
 
readfile($path_to_your_cached_file);
exit();
M4ngo
Forum Newbie
Posts: 6
Joined: Fri Jul 18, 2008 8:27 pm

Re: Dynamic Image Generation MIME Types and Caching

Post by M4ngo »

Thanks John! Read below for more questions.
John Cartwright wrote:A common way to circumvend the forum file extension check is to run your png (or whatever image type) through the php interpreter. By adding:

Code: Select all

 
AddType application/x-httpd-php .png
in your .htaccess file, then all requests to .png will transparently be processed by php.
[...]
Questions:

The png must be generated based on 2 input parameters. So, since the url to the PNG can't contain a query string (or at least the forum wont like that) I propose to encode the two parameters in the png file name.

So, for example, if I really needed to call:
/images/generate.php?p1=1&p2=2
I will instead call:
/images/image-p1_1-p2_2.png

I see how adding the AddType .htaccess directive will cause png files to be transparently processed by PHP - but this assumes the PNG file (which actually contains a php script) is present. If I did the above - it would not be.

Also, this approach requires the script to parse out the two parameters from the file name. I guess that's acceptable given the restrictions I am under...

This is why I was originally thinking a 404 trap - because the file names will vary wildly and be impossible to pre-determine.

Any better ideas?

TIA!
Z.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Dynamic Image Generation MIME Types and Caching

Post by John Cartwright »

Assuming you have the mod_rewrite extension installed, you can use mod_rewrite to transparantly rewrite the url (which is exactly what you want).

Again, in your .htaccess, you will need a rule like

Code: Select all

RewriteEngine On
RewriteRule ^/images/image-p1_([0-9]+)-p2_([0-9]+)\.png$ /images/generate.php?p1=$1&p2=$2
 
If you are using this technique, you won't need to use my previous suggestion either. In fact, it would be redundant to do so.
M4ngo
Forum Newbie
Posts: 6
Joined: Fri Jul 18, 2008 8:27 pm

Re: Dynamic Image Generation MIME Types and Caching

Post by M4ngo »

John Cartwright wrote:Assuming you have the mod_rewrite extension installed, you can use mod_rewrite to transparantly rewrite the url (which is exactly what you want).

Again, in your .htaccess, you will need a rule like

Code: Select all

RewriteEngine On
RewriteRule ^/images/image-p1_([0-9]+)-p2_([0-9]+)\.png$ /images/generate.php?p1=$1&p2=$2
 
If you are using this technique, you won't need to use my previous suggestion either. In fact, it would be redundant to do so.
NICE!

Ah, the land of mod_rewrite... Its so powerful and I am a mod_rewrite rookie... It is so complex... This might not be that bad I guess... Your example seems clear enough... I will try it.

Thanks!!
Z.
Post Reply