使用 django 和 nginx 按需创建缩略图
我想动态生成图像的缩略图。
我的网站是用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您所描述的是 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.