Created
January 4, 2014 21:21
-
-
Save azhtom/8260885 to your computer and use it in GitHub Desktop.
ImageField Size Validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Meta: | |
model = NameModel | |
def clean_image(self): | |
image = self.cleaned_data.get['image'] | |
if image: | |
from django.core.files.images import get_image_dimensions | |
w, h = get_image_dimensions(image) | |
if not image.content_type in settings.VALID_IMAGE_FORMATS: | |
raise forms.ValidationError(u'Only *.gif, *.jpg and *.png images are allowed.') | |
if w > settings.VALID_IMAGE_WIDTH or h > settings.VALID_IMAGE_HEIGHT: | |
raise forms.ValidationError(u'That image is too big. The image needs to be ' + str(settings.VALID_IMAGE_WIDTH) + 'px * ' + str(settings.VALID_IMAGE_HEIGHT) + 'px (or less).') | |
return image |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment