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

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

问题描述

我搜索了很多关于这个问题,但是找不到我需要的东西。我会解释我的问题:



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



我看到一些解决方案使用方法,但我想通过覆盖我的Models.py中的save()方法来做到这一点。问题是,我在保存方法中找到的用于调整图像大小的所有解决方案都是在第一次保存它之后(调用super fonction)来实现的,这意味着它会使用带宽(如果我使用CDN)没有(我正确的在这个点击这里)



感谢您的帮助

解决方案

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



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



如果您的关注不是带宽,那么您可以自由地让用户上传该文件,但是在保存之前,可以让Django调整大小并重新格式化。



你是正确的,你将要覆盖照片对象的保存功能。我建议使用图书馆来处理大小和重新格式化,例如 sorl

  from sorl.thumbnail import ImageField,get_thumbnail 

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

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

Sorl只是一个我自信和熟悉的图书馆,但它需要一些调整和配置。您可以检查枕头或其他东西,只需更换覆盖 self.image 的行。



我还发现了类似的问题 here



编辑看到上面对您的评论回复的更新。另请注意,如果您的Web服务器正在处理Django,并且您的文件被保存到某些CDN上的数据库,则此方法将会起作用。在上传到您的CDN之前(假设您的配置正如我所想),图像将在网络服务器上调整大小。



希望这个帮助!


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

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

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?).

Thank you for your help

解决方案

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.

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.

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.

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 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.

Edit: saw the update to your comment response above. Also note that if your webserver is handling Django, and your files are being saved to a database on 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).

Hope this helps!

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

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