Django 图像在上传前调整大小和转换 [英] Django image resizing and convert before upload

查看:42
本文介绍了Django 图像在上传前调整大小和转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个主题上搜索了很多,但无法真正找到我需要的东西.我会解释我的问题:

I searched a lot on this subject but couldn't really find what I need. I'll explain my problem :

在我的网站上,用户可以上传图片.我需要调整此图像的大小并将其转换为 JPEG 上传之前.

On my website, the user can upload an image. I need to resize this image and convert it to JPEG before uploading it.

我看到了一些使用 Views.py 中的方法的解决方案,但我想通过覆盖我的 Models.py 中的 save() 方法来实现.问题是,我在 save 方法中找到的用于调整图像大小的所有解决方案都是在第一次保存(调用超级函数)后执行的,这意味着它会毫无用处地使用带宽(如果我使用 CDN)(我对此是否正确)点?).

I saw some solutions using a method in the Views.py, but I'd like to do it by overriding the save() method in my Models.py. The problem is, all solutions I found for resizing image in save method do it after saving it a first time (calling super fonction), which means it'd use bandwidth (if I use a CDN) for nothing (am I right on this point?).

感谢您的帮助

推荐答案

首先,最好建立正确的语言.Django 和 Python 只存在于服务器端.因此,他们操作、保存或以其他方式使用的任何内容都必须首先发送到服务器.如果要使用 Django 或 Python 来管理照片,则用户必须先将这张照片上传到服务器.上传照片后,Django 可以在存储文件之前自由进行更改.

First, it's best to establish the correct language. Django and Python exist only on the server side. Therefore, anything they manipulate, save, or otherwise use, has to be first sent to the server. If Django or Python is to manage the photo, the user MUST upload this photo to the server first. Once the photo is uploaded, Django is free to make changes before storing the file.

如果您担心上传带宽,并且不希望上传大文件,则必须在客户端调整照片大小并重新格式化.如果这是一个 Web 应用程序,这可以使用 Javascript 来完成,但不能用 Python 来完成,因为 Python 不会在客户端运行像您这样的应用程序.

If your concern is with upload bandwidth, and you don't want large files being uploaded, you will have to resize and reformat the photo on the client side. If this is a web application, this can be done using Javascript, but can not be done with Python, since Python does not operate on the client side for an application like yours.

如果您不关心带宽,那么您可以随意让用户上传"文件,但在保存之前让 Django 调整大小并重新格式化.

If your concern is not with bandwidth, then you're free to have the user "upload" the file, but then have Django resize and reformat it before saving.

您想覆盖照片对象的保存功能是正确的.我建议使用库来处理调整大小和重新格式化,例如 sorl.

You are correct that you will want to override your save function for the photo object. I would recommend using a library to handle the resizing and reformatting, such as sorl.

from sorl.thumbnail import ImageField, get_thumbnail

class MyPhoto(models.Model):
    image = ImageField()

    def save(self, *args, **kwargs):
        if self.image:
            self.image = get_thumbnail(self.image, '500x600', quality=99, format='JPEG')
        super(MyPhoto, self).save(*args, **kwargs)

Sorl 只是一个我有信心和熟悉的库,但它需要一些调整和配置.您可以查看 Pillow 或其他东西,只需替换覆盖 self.image 的行.

Sorl is just a library I am confident and familiar with, but it takes some tuning and configuration. You can check out Pillow or something instead, and just replace the line overriding self.image.

我也在这里发现了一个类似的问题.

I also found a similar question here.

在上面看到了对您的评论回复的更新.另请注意,如果您的网络服务器正在处理 Django,并且您的文件正在保存到某些 CDN,则此方法将起作用.图像将在网络服务器上调整大小,然后再上传到您的 CDN(假设您的配置与我假设的一样).

saw the update to your comment response above. Also note that if your webserver is handling Django, and your files are being saved to some CDN, this method will work. The image will be resized on the webserver before being uploaded to your CDN (assuming your configuration is as I'm assuming).

希望这有帮助!

这篇关于Django 图像在上传前调整大小和转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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