在Django管理后台验证图片大小

27 投票
5 回答
22657 浏览
提问于 2025-04-15 16:50

我看到很多使用Django的应用程序在用户上传图片后,会自动调整图片的大小。这在某些情况下是不错的做法,但我不想这样做。我希望用户上传的文件本身就已经是合适的大小。

我想要一个ImageField,强制用户上传尺寸为100x200的图片。如果用户上传的图片不是这个确切的大小,我希望管理后台的表单显示为无效。我还想对图片的长宽比做同样的事情。我希望用户上传的图片是16:9的比例,任何不符合这个比例的上传都要被拒绝。

我已经知道如何获取图片的宽度和高度,但我无法在服务器端进行检查,直到图片上传完成并且表单成功提交。有没有办法在更早的时候检查这些信息呢?

5 个回答

3

我们还可以使用一个class,里面定义一个__call__()方法,来提供一些参数

正如在编写验证器 - Django文档中提到的:

你也可以使用一个带有__call__()方法的类,来创建更复杂或可配置的验证器。例如,RegexValidator就是使用这种方法。如果在validators模型字段选项中使用了基于类的验证器,你需要确保它可以被迁移框架序列化,这就需要添加deconstruct()__eq__()方法。

下面是一个可用的示例:

from collections.abc import Mapping

from django.utils.translation import gettext_lazy as _
from django.utils.deconstruct import deconstructible


@deconstructible
class ImageValidator(object):
    messages = {
        "dimensions": _(
            "Allowed dimensions are: %(width)s x %(height)s."
        ),
        "size": _(
            "File is larger than > %(size)sKiB."
        )
    }

    def __init__(self, size=None, width=None, height=None, messages=None):
        self.size = size
        self.width = width
        self.height = height
        if messages is not None and isinstance(messages, Mapping):
            self.messages = messages

    def __call__(self, value):
        # _get_image_dimensions is a method of ImageFile
        # https://docs.djangoproject.com/en/1.11/_modules/django/core/files/images/
        if self.size is not None and value.size > self.size:
            raise ValidationError(
                self.messages['size'],
                code='invalid_size',
                params={
                    'size': float(self.size) / 1024,
                    'value': value,
                }
            )
        if (self.width is not None and self.height is not None and
                (value.width != self.width or value.height != self.height)):
            raise ValidationError(
                self.messages['dimensions'],
                code='invalid_dimensions',
                params={
                    'width': self.width,
                    'height': self.height,
                    'value': value,
                }
            )

    def __eq__(self, other):
        return (
            isinstance(other, self.__class__) and
            self.size == other.size and
            self.width == other.width and
            self.height == other.height
        )

然后在模型中:


class MyModel(models.Model):

    ...

    banner = models.ImageField(
        upload_to='uploads/', verbose_name=_("Banner"),
        max_length=255, null=True, blank=True,
        validators=[ImageValidator(size=256000, width=1140, height=425)],
        help_text=_("Please use our recommended dimensions: 1140 x 425 PX, 250 KB MAX")
    )
6

在你的模型文件中使用这个函数,

from django.core.exceptions import ValidationError

def validate_image(fieldfile_obj):
    filesize = fieldfile_obj.file.size
    megabyte_limit = 2.0
    if filesize > megabyte_limit*1024*1024:
        raise ValidationError("Max file size is %sMB" % str(megabyte_limit))

class Company(models.Model):
    logo = models.ImageField("Logo", upload_to=upload_logo_to,validators=[validate_image], blank=True, null=True,help_text='Maximum file size allowed is 2Mb')
61

做这个的最佳时机是在表单验证的时候。
这里有一个简单的例子(稍后会添加更多信息):

from django.core.files.images import get_image_dimensions
from django.contrib import admin
from django import forms

class myForm(forms.ModelForm):
   class Meta:
       model = myModel
   def clean_picture(self):
       picture = self.cleaned_data.get("picture")
       if not picture:
           raise forms.ValidationError("No image!")
       else:
           w, h = get_image_dimensions(picture)
           if w != 100:
               raise forms.ValidationError("The image is %i pixel wide. It's supposed to be 100px" % w)
           if h != 200:
               raise forms.ValidationError("The image is %i pixel high. It's supposed to be 200px" % h)
       return picture

class MyAdmin(admin.ModelAdmin):
    form = myForm

admin.site.register(Example, MyAdmin)

撰写回答