根据文件是否写入文件系统动态提供文件服务

发布于 2024-10-03 11:06:13 字数 422 浏览 3 评论 0原文

标题很糟糕,但不知道如何很好地描述情况。 我想动态调整/裁剪图像。我希望将我的 apache 配置为执行以下操作:

  • 收到 /img/profile.jpg?crop&p=50 的请求
  • Apache 检查文件 /img/profile.c.50.jpg 是否存在
  • 如果它确实存在,它静态地提供该服务,而无需访问我的 php 服务器
  • 如果它不存在,它会访问 /cropper.php?path=/img/profile.jpg&p=50
  • 然后该文件将图像写入文件,并提供服务它
  • 请求再次出现(参见步骤 1)

我觉得我们可以使用 .htaccess 文件中的 FilesMatch 指令,但我真的不知道从哪里开始。任何链接或任何想法将不胜感激。

谢谢大家。

Terrible title, but don't really know how to describe the situation too well.
I would like to dynamically resize/crop images. I'm hoping to config my apache to do the following:

  • A request for /img/profile.jpg?crop&p=50 is received
  • Apache checks if the file /img/profile.c.50.jpg exists
  • If it does exist, it serves that up statically without having to hit my php server
  • If it doesn't exist, it hits /cropper.php?path=/img/profile.jpg&p=50
  • This file then writes the image to file, and serves it
  • Request comes in again (see step 1)

I feel like making use of the FilesMatch directive in the .htaccess file could be of us but I really don't know where to start. Any links would be appreciated, or any ideas.

Thanks all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伪装你 2024-10-10 11:06:13

\就我个人而言,我会将这个任务委托给脚本(可能使用 X-Sendfile),而不是重写混乱。

在这里,您可能需要调整您的文档根目录并使您的“配置文件”部分更加通用:

RewriteEngine on
RewriteCond %{QUERY_STRING} "(?:^|&)crop&p=50"
RewriteCond %{REQUEST_URI} ^(/img/profile)(\.png)$
RewriteCond /your/document/root%1.c.50%2 -f
RewriteRule ^ %1.c.50%2 [L]

# hand over to crop script if the above doesn't match
RewriteCond %{REQUEST_URI} =/img/profile.png
RewriteRule ^ /cropper.php?path=%{REQUEST_URI}&p=50 [L]

始终涉及脚本的解决方案:
(为了清楚起见,通过 /img/profiles/... 访问)

  • $1 捕获配置文件名称(在本例中假设为“az”)
  • $2 捕获所需的宽度
  • $3 文件扩展名

这确实简化了重写:

RewriteEngine on
RewriteRule ^/img/profiles/([a-z]+)\.([1-9][0-9]+)\.(jpe?g|gif|png)$ /cropper.php?profile=$1.$3&p=$2 [L]

该脚本将检查是否文件存在,然后先交付它或生成它

从请求的 URL 获取图像名称的解决方案:

(正如您在评论中提到的)

RewriteEngine on
RewriteCond %{REQUEST_URI} ^(/img/profile)\.([1-9][0-9]+)\.(png|gif|jpe?g)$
RewriteCond /your/document/root%{REQUEST_URI} -f
RewriteRule ^ %{REQUEST_URI} [L]

# hand over to crop script if the above doesn't match
RewriteCond %{REQUEST_URI} ^(/img/profile)\.([1-9][0-9]+)\.(png|gif|jpe?g)$
RewriteRule ^ /cropper.php?path=$1.$3&p=$2 [L]

如果存储了图像,则可以将其重写为以下内容。直接在文档根目录中:

RewriteCond %{REQUEST_URI} ^(/img/profile)\.([1-9][0-9]+)\.(png|gif|jpe?g)$
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule ^ /cropper.php?path=$1.$3&p=$2 [L]

\Personally, I'd delegate this task to a script (maybe using X-Sendfile), rather than a rewrite mess.

Here you go, you may have to tweak your document root and make your "profile" part more general:

RewriteEngine on
RewriteCond %{QUERY_STRING} "(?:^|&)crop&p=50"
RewriteCond %{REQUEST_URI} ^(/img/profile)(\.png)$
RewriteCond /your/document/root%1.c.50%2 -f
RewriteRule ^ %1.c.50%2 [L]

# hand over to crop script if the above doesn't match
RewriteCond %{REQUEST_URI} =/img/profile.png
RewriteRule ^ /cropper.php?path=%{REQUEST_URI}&p=50 [L]

Solution always involving a script:
(accessed through /img/profiles/... for clarity)

  • $1 captures the profile name (in this case assumed to be 'a-z' only
  • $2 captures the desired width
  • $3 the file extension

Which really simplifies the rewrite:

RewriteEngine on
RewriteRule ^/img/profiles/([a-z]+)\.([1-9][0-9]+)\.(jpe?g|gif|png)$ /cropper.php?profile=$1.$3&p=$2 [L]

The script would check whether the file exists and either deliver it or generate it first.

Solution taking the image name from the requested URL:

(As you mentionned in your comment)

RewriteEngine on
RewriteCond %{REQUEST_URI} ^(/img/profile)\.([1-9][0-9]+)\.(png|gif|jpe?g)$
RewriteCond /your/document/root%{REQUEST_URI} -f
RewriteRule ^ %{REQUEST_URI} [L]

# hand over to crop script if the above doesn't match
RewriteCond %{REQUEST_URI} ^(/img/profile)\.([1-9][0-9]+)\.(png|gif|jpe?g)$
RewriteRule ^ /cropper.php?path=$1.$3&p=$2 [L]

which may be rewritten to the following, if the images are stored directly within your document root:

RewriteCond %{REQUEST_URI} ^(/img/profile)\.([1-9][0-9]+)\.(png|gif|jpe?g)$
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule ^ /cropper.php?path=$1.$3&p=$2 [L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文