最快重定向丢失的图像文件的方法重定向、图像文件、最快、方法

2023-09-02 20:40:36 作者:隱痛

我有一个在即时缩图系统,我试图寻找,以确保提供了图像时,它是尽可能快的最佳途径。这里是电流流动:

I have an on-the-fly thumbnailing system and am trying to find the best way to make sure it's as fast as possible when serving up images. Here is the current flow:

在用户请求缩略图缩略图/本,是最图像name.gif W = 200安培; H = 100和C =真 htaccess文件使用modrewrite发送从该文件夹请求到PHP文件 在PHP文件检查 file_exists()的基础上的查询字符串值所要求的图片 User requests thumbnail thumbnails/this-is-the-image-name.gif?w=200&h=100&c=true htaccess file uses modrewrite to send requests from this folder to a PHP file PHP file checks file_exists() for the requested image based on the query string values

如果它:

header('content-type: image/jpeg'); 
echo file_get_contents($file_check_path); 
die();

如果它不它创建缩略图并将其返回。

If it doesn't it creates the thumbnail and returns it.

我的问题是,是否有一种方法来优化这种进入速度更快?理想的情况是我的htaccess文件会做一个file_exists,只送你到PHP文件时,它不...但因为我使用的查询字符串存在的没有办法建立一个动态的URL来检查。是否值得从查询字符串转换为实际的文件请求,然后做htaccess的存在检查?那将是任何更快吗?我preFER查询字符串语法,但目前所有的请求去他们中是否存在与否。

My question is whether there is a way to optimize this into being faster? Ideally my htaccess file would do a file_exists and only send you to the PHP file when it doesn't... but since I am using query strings there is no way to build a dynamic URL to check. Is it worth switching from query strings to an actual file request and then doing the existence check in htaccess? Will that be any faster? I prefer the query string syntax, but currently all requests go to the PHP file which returns images whether they exist or not.

感谢您的任何输入事先!

Thank you for any input in advance!

推荐答案

您应该能够做到这一点在理论上。该的RewriteCond 命令有一个标志 -f 可用于检查一个文件是否存在。你应该能有一个这样的规则:

You should be able to do this in theory. The RewriteCond command has a flag -f which can be used to check for the existence of a file. You should be able to have a rule like this:

# If the file doesn't exist
RewriteCond %{REQUEST_FILENAME} !-f

# off to PHP we go
RewriteRule (.*)   your-code.php [L,QSA]

这里的转折是,我想你是根据该进来的参数文件命名 - 因此上面的例子可能是缩略图/本,是最形象的名字 - 200- 100.gif 。如果是这样的话,你需要生成一个文件名,以测试在运行,并检查该代替 REQUEST_FILENAME - 这个细节实在​​具体您的设置。如果可以的话,我会推荐某种系统,不涉及太多精力。例如,你可以存储你的缩略图到文件系统中的目录结构像 /宽/高/文件名,这将是更容易为您在比,一个重写规则修改文件名的宽度height.gif

The twist here is that I imagine you're naming files according to the parameters that come in -- so the example above might be thumbnails/this-is-the-image-name-200-100.gif. If that is the case, you'll need to generate a filename to test on the fly, and check for that instead of the REQUEST_FILENAME -- the details of this are really specific to your setup. If you can, I would recommend some sort of system that doesn't involve too much effort. For example, you could store your thumbnails to the filesystem in a directory structure like /width/height/filename, which would be easier to check for in a rewrite rule than, modified-filename-width-height.gif.

如果您还没有检查出来, Apache的mod_rewrite指南有一帮不错的例子。

If you haven't checked it out, Apache's mod_rewrite guide has a bunch of decent examples.

更新:那么,你实际上需要检查,从外观上来看动态文件名。我觉得做这样的事情的最简单的方法将坚持你生成到一个环境变量的文件名,这样的(我从您的其他问题借来充实了这一点):

UPDATE: so, you'll actually need to check for the dynamic filename from the looks of it. I think that the easiest way to do something like this will be to stick the filename you generate into an environment variable, like this (I've borrowed from your other question to flesh this out):

# generate potential thumbnail filename
RewriteCond %{SCRIPT_FILENAME}%{QUERY_STRING} /([a-zA-Z0-9-]+).(jpg|gif|png)w=([0-9]+)&h=([0-9]+)(&c=(true|false))

# store it in a variable
RewriteRule .* - [E=thumbnail:%1-%2-%3-%4-%6.jpg]

# check to see if it exists
RewriteCond %{DOCUMENT_ROOT}/path/%{ENV:thumbnail} !-f 

# off to PHP we go
RewriteRule (.*)   thumbnail.php?file_name=%1&type=%2&w=%3&h=%4&c=%6 [L,QSA]

这是没有经过充分测试,并受到不工作的肯定。我会建议夫妇的其他东西:

This is completely untested, and subject to not working for sure. I would recommend a couple other things:

同样的,一个巨大的建议,我对你的是,如果可能的话,打开日志记录,并设置 RewriteLogLevel 来较高水平。该日志重写规则可以是pretty的令人费解,但绝对给你是怎么回事的想法。您需要服务器访问要做到这一点 - 你不能把日志记录配置的.htaccess文件,如果我还记得

Also, one huge recommendation I have for you is that if possible, turn on logging and set RewriteLogLevel to a high level. The log for rewrite rules can be pretty convoluted, but definitely gives you an idea of what is going on. You need server access to do this -- you can't put the logging config in an .htaccess file if I recall.