重定向丢失图像文件的最快方法 [英] Fastest way to redirect missing image files

查看:21
本文介绍了重定向丢失图像文件的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动态缩略图系统,我正在努力寻找最好的方法来确保在提供图像时尽可能快.这是当前的流程:

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:

  • 用户请求缩略图thumbnails/this-is-the-image-name.gif?w=200&h=100&c=true
  • 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 中进行存在检查?那会更快吗?我更喜欢查询字符串语法,但目前所有请求都转到 PHP 文件,无论图像是否存在,该文件都会返回图像.

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]

这里的扭曲是我想你是根据进来的参数命名文件——所以上面的例子可能是 thumbnails/this-is-the-image-name-200-100.gif.如果是这种情况,您需要生成一个文件名以进行动态测试,并检查该文件名而不是 REQUEST_FILENAME —— 详细信息确实特定于您的设置.如果可以,我会推荐一些不需要太多努力的系统.例如,您可以将缩略图存储到文件系统中的目录结构中,例如 /width/height/filename,这比 modified-filename- 在重写规则中更容易检查宽高.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 设置为高级别.重写规则的日志可能非常复杂,但绝对可以让您了解正在发生的事情.您需要访问服务器才能执行此操作 - 如果我记得,您不能将日志记录配置放在 .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.

这篇关于重定向丢失图像文件的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆