Reading image dimensions before it is loaded from a web form using python.

Evan Klitzke evan at yelp.com
Sat Jun 30 23:53:15 EDT 2007


On 6/30/07, Cameron Laird <claird at lairds.us> wrote:
> In article <mailman.235.1183241587.22759.python-list at python.org>,
> Evan Klitzke <evan at yelp.com> wrote:
> >On 6/30/07, Norman Khine <norman at khine.net> wrote:
> >> Hello,
> >> I am writing an application using python that allows the user to upload
> >> an image to a folder on the server.
> >>
> >> Is there a way to get the size of the file before it has been uploaded
> >> onto the server and give an error if the size does not comply to the
> >> maximum size.
> >
> >The easiest way to do this is to simply restrict the maximum file
> >upload size. When the client makes the POST request you can see how
> >big it will be. This won't really give you the image dimensions of
> >course, but there's no simple way to find out that information without
> >uploading the entire image.
>                         .
>                         .
>                         .
> Maybe we disagree about "simple", but there *are* definite
> ways to do so.  It's a bit involved--I fear that when Mr.
> Khine writes "image", he might have in mind not just .gif,
> .jpg, and .png, but perhaps also SVG, Flash, PDF, ...--but,
> yes, it's possible with most image formats to upload just a
> small portion of the header and test whether what follows
> meets the size requirements.

Without running some special client-side application, I think you'd
have to do some trickery with the web server involving artificially
stopping the upload after receiving enough bytes to constitute the
image header, and then deciding whether or not to proceed with the
upload at that point. I think that this is definitely nontrivial since
it would most likely involve some hacking of Apache (or whatever web
server you are using).

-- 
Evan Klitzke <evan at yelp.com>



More information about the Python-list mailing list