如何使用mod_rewrite和php创建图像缩略图缓存

I have been working on a script to generate lower resolution copies of images using PHP. I am struggling with mod_write to execute the PHP script only when the file does not exist

What i currently have (which may be off by a bit, as i have made thousands of attempts today) is the following:

RewriteCond %{REQUEST_URI} (900x...|...x900)\.(png|jpe?g|gif)
RewriteCond %{HTTP_COOKIE} ^(.*;\ )?(window_resolution=(640))(;\ .*)?$ [NC]
RewriteRule full/(.*) /responsive/cache/480/responsive/$1 [L]

RewriteCond %{REQUEST_URI} (.*)(900x...|...x900)\.(png|jpe?g|gif)
RewriteCond %{HTTP_COOKIE} ^(.*;\ )?(window_resolution=(.*))(;\ .*)?$ [NC]
RewriteCond %{DOCUMENT_ROOT}/responsive/cache/%3%{REQUEST_URI} !-f
RewriteRule .* /responsive/resize.php [L]

Currently the files are located like this:

/responsive/full/photo-900x500.jpg

The cache is stored:

/responsive/cache/

In the following format:

/responsive/cache/<resolution>/<original path>

Example of where i want the files:

/responsive/cache/768/responsive/photo-900x500.jpg

Ideally this would be used with something like wordpress where i would make a structure like this

Cache:

/cache/

Originals:

/wp-content/uploads/*

Example:

/cache/768/wp-content/uploads/2012/01/test.jpg

After hours of deleting and starting over I came up with this and it seems to work just fine. It will reserve the full file path if i want and allows selection of image sizes via cooke:

RewriteCond %{ENV:REDIRECT_STATUS} $^
RewriteCond %{REQUEST_URI} (900x...|...x900)\.(png|jpe?g|gif)
RewriteCond %{HTTP_COOKIE} ^(.*;\ )?(ricache_width=(480|640|768|992|1170))(;\ .*)?$ [NC]
RewriteRule (.*) /responsive/cache/%3/responsive/$1

RewriteCond %{ENV:REDIRECT_STATUS} $^
RewriteCond %{REQUEST_URI} (.*)(900x...|...x900)\.(png|jpe?g|gif)
RewriteCond %{HTTP_COOKIE} ^(.*;\ )?(ricache_width=(480|640|768|992|1170))(;\ .*)?$ [NC]
RewriteCond %{DOCUMENT_ROOT}/responsive/cache/%3%{REQUEST_URI} !-f
RewriteRule .* /responsive/resize.php [L]

RewriteCond %{ENV:REDIRECT_STATUS} $^
RewriteRule .* - [L]

It is missing a 404 (which isnt that big of a deal, i have a backup in PHP but i need to replace it). But at least this is a good starting place that functions! :)