Not allowing images small than certain dimensions

Not allowing images small than certain dimensions

I have a model that saves user profile images. If the image that is uploaded is greater than 200x200 pixels, then we resize to 200x200. If the image is right at 200x200, then we return that image. What I want now is to throw an error to the user saying that this image is too small and is not allowed. Here's what I have:

I have a model that saves user profile images. If the image that is uploaded is greater than 200x200 pixels, then we resize to 200x200. If the image is right at 200x200, then we return that image. What I want now is to throw an error to the user saying that this image is too small and is not allowed. Here's what I have:

class Profile(models.Model):
    GENDER_CHOICES = (
        ('M', 'Male'),
        ('F', 'Female'),
    )
    user    = models.OneToOneField(User, null=True, on_delete=models.CASCADE)
    bio     = models.CharField(max_length=200, null=True)
    avatar  = models.ImageField(upload_to="img/path")
    gender  = models.CharField(max_length=1, choices=GENDER_CHOICES, null=True)

def save(self, *args, **kwargs):
    super(Profile, self).save(*args, **kwargs)
    if self.avatar:
        image = Image.open(self.avatar)
        height, width = image.size
        if height == 200 and width == 200:
            image.close()
            return

        if height < 200 or width < 200:
            return ValidationError("Image size must be greater than 200")
        image = image.resize((200, 200), Image.ANTIALIAS)
        image.save(self.avatar.path)
        image.close()

When an image is smaller than 200px in width or height, the image should not be uploaded. However, the image is being uploaded. How can I stop this from happening?

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Python Django Tutorial | Django Course

🔥Intellipaat Django course: https://intellipaat.com/python-django-training/ 👉This Python Django tutorial will help you learn what is django web development &...

How to Integrate Django to any Javascript Front End // Django to React // Django to Angular

Learn the basic technique to Integrate Django to any Javascript Front End // Django to React // Django to Angular

Django

Well i used django ... i want to know after completing all the coding stuff, can i make all the codes online ?