Ticket #18520: patch.diff

File patch.diff, 1.3 KB (added by Greg Brown, 12 years ago)

Initial patch, solves the problem but with a hardcoded maximum which I'm not sure about.

  • django/forms/fields.py

    diff --git a/django/forms/fields.py b/django/forms/fields.py
    index 4668ead..2798341 100644
    a b class ImageField(FileField):  
    559559            # load() is the only method that can spot a truncated JPEG,
    560560            #  but it cannot be called sanely after verify()
    561561            trial_image = Image.open(file)
     562           
     563            MAX_DIMENSIONS = (5000, 5000)
     564            dimensions = trial_image.size
     565            if dimensions[0] > MAX_DIMENSIONS[0] or dimensions[1] > MAX_DIMENSIONS[1]:
     566                raise ValidationError('Maximum image dimensions are %sx%s px. This file is %sx%s px' % (MAX_DIMENSIONS + dimensions))
     567           
    562568            trial_image.load()
    563569
    564570            # Since we're about to use the file again we have to reset the
    class ImageField(FileField):  
    575581            # _imaging C module isn't available, so an ImportError will be
    576582            # raised. Catch and re-raise.
    577583            raise
     584        except ValidationError:
     585            # any validation errors are re-raised as is
     586            raise
    578587        except Exception: # Python Imaging Library doesn't recognize it as an image
    579588            raise ValidationError(self.error_messages['invalid_image'])
    580589        if hasattr(f, 'seek') and callable(f.seek):
Back to Top