使用 django 和 nginx 按需创建缩略图

发布于 2024-09-03 12:09:37 字数 648 浏览 3 评论 0原文

我想动态生成图像的缩略图。

我的网站是用 django 构建的,并使用 nginx 进行部署,nginx 提供所有静态内容并使用反向代理与 django/apache 进行通信。

现在,对于我网站上的每张图像,我都会生成所有所需尺寸的缩略图,并在需要时提供它们。问题是,每当我更改缩略图的大小时,我都必须重新生成所有缩略图(而且它们非常多)。

但是现在我想在第一次访问时生成缩略图,稍后 nginx 将多次传递相同的文件。如果我因为访问较少而删除该缩略图文件,则下次应该会自动生成它。在我的例子中,缩略图也有水印,这需要我的应用程序的一些计算逻辑,因此网络服务器缩略图模块可能无法很好地工作。

缩略图的大小可以嵌入到 URL 中。因此 http://www.example.com/thumbnail/abc_320x240.jpg 得到缩略图大小为 320x240。

我现在正在寻找的方法是让 nginx 查找文件,如果它不存在,则将查询转发到我的 django 应用程序,该应用程序将创建缩略图并发送响应或重定向字符串。但是我不确定并发问题以及稍后可能出现的任何其他问题。

实现这一目标的适当方法是什么?

I want to generate thumbnails of images on the fly.

My site is built with django and deployed using nginx which serves all the static content and communicates with django/apache using reverse proxy.

Right now, for every image in my site, I generate all required sizes of thumbnails on-hand and deliver them when required. The problem is that whenever I change the size of a thumbnail, I have to regenerate all of them (and they are tons).

However now I'd like to generate the thumbnail the first time it is accessed and later on nginx would deliver the same file over n over. If I delete that thumbnail file because of lesser accesses, it should get generated automatically the next time. Thumbnails in my case also have watermarks which require some computation logic of my application so a webserver thumbnail module might not work very well.

The size of the thumbnail can be embedded in the URL. So http://www.example.com/thumbnail/abc_320x240.jpg gets the 320x240 size of the thumbnail.

The approach I'm looking right now is to let nginx lookup the file and if it doesn't exist, forward the query to my django application which would create the thumbnail and send either the response or a redirect string. However I'm not sure about the concurrency issues and any other issues which might pop up later.

What is the appropriate way to achieve this?

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

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

发布评论

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

评论(1

难得心□动 2024-09-10 12:09:37

您所描述的是 sorl.thumbnail 的工作原理,减去水印。但是您可能可以很容易地修改 sorl 以将水印添加到它所做的基本缩略图中。

What you are describing is how sorl.thumbnail works minus the watermarking. But you could probably modify sorl pretty easily to add your watermarking to the basic thumbnailing that it does.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文