On Thu, Mar 19, 2009 at 3:31 PM, Michael Repucci <mich...@repucci.org>wrote:

>
> I'm struggling with what seems like should be a very straightforward
> task. I have a model with a field named image of type ImageField, and
> I'd like to do custom validation via the model form's clean_image
> method based on the height and width (in pixels) of the image.
>
> If I just grab the image object from the cleaned_data dictionary it
> doesn't have attributes height and width, and if I specify the
> height_field and width_field parameters in the ImageField
> instantiation, I don't have access to those fields from the
> clean_image method (or do I?).
>
> Is there a simple way to do this that I'm missing?
> >
>
Yep, here's an example of a method I use:

    def clean_image(self):
        from django.core.files.images import get_image_dimensions
        w, h = get_image_dimensions(self.cleaned_data['image'])
        if h > 500 or w > 800:
            raise forms.ValidationError("The image is too large, the maximum
size is 800x500, your image was %sx%s." % (w, h))
        return self.cleaned_data['image']


-- 
"I disapprove of what you say, but I will defend to the death your right to
say it." --Voltaire
"The people's good is the highest law."--Cicero

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to