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

时间:2013-01-13 05:35:20

标签: php caching mod-rewrite

我一直致力于使用PHP生成低分辨率图像副本的脚本。我正在努力使用mod_write来仅在文件不存在时执行PHP脚本

我目前所拥有的(可能会有所改变,因为我今天已经进行了数千次尝试)如下:

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]

目前文件的位置如下:

/responsive/full/photo-900x500.jpg

存储缓存:

/responsive/cache/

采用以下格式:

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

我想要文件的示例:

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

理想情况下,这将用于像wordpress这样的东西,我会制作这样的结构

高速缓存:

/cache/

原稿:

/wp-content/uploads/*

示例:

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

1 个答案:

答案 0 :(得分:0)

经过几个小时的删除和重新开始,我想出了这个,它似乎工作得很好。如果我需要它将保留完整的文件路径,并允许通过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]

它缺少一个404(这不是很大的交易,我在PHP中有备份,但我需要替换它)。但至少这是一个很好的起点! :)