用PHP调整图像大小和缓存图像,并使用mod_rewrite来访问已兑现的图像


resize and cache images with php and use mod_rewrite to acces the cashed ones

我想用url中的大小来调整图像的大小,像这样:http://www.domain.nl/images/chached/200x200/name.jpg如果图像不存在,我想生成并缓存它。

之后我如何检查文件是否存在,而不必在/images/chached/目录中生成文件夹'200x200'并且不使用PHP(较慢)。

是否有办法做到这一点与mod_rewrite(更快)?还是这根本不可能?

我正在使用Kohana 3女巫使用这个mod_rewite代码:

# Turn on URL rewriting
RewriteEngine On
# Installation directory
# RewriteBase /kohana/
RewriteBase /
# Protect hidden files from being viewed
<Files .*>
    Order Deny,Allow
Deny From All
</Files>
# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)'b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]

使用Kohana的路由设置一个新的路由/images/cached/<size>/<name>(或类似的),并让它由您选择的操作来处理。然后,在该操作的代码中,从这些参数生成图像,将结果保存在该路径下,并将图像提供给客户端。

所以第一个请求(文件不存在)直接到index.php(因为RewriteRule .* index.php/$0 [PT]),第二个和每个后续请求将由Apache直接服务(因为现在RewriteCond %{REQUEST_FILENAME} !-f不再匹配)。