Some of the information here may be outdated, please check the book instead
[edit]

New IS_IMAGE validator can be used to check if file uploaded through file input was saved in one of selected image formats and has dimensions (width and height) within given boundaries. Everything is done in pure Python (no PIL or other libraries required).

Note that this does not check for image size - use IS_LENGTH for that.

Supported file formats: BMP, GIF, JPEG, PNG.

INPUT(_type='file', _name='name', requires=IS_IMAGE(extensions=('bmp', 'gif',
                                                                'jpeg', 'png'),
                                                    maxsize=(10000, 10000),
                                                    minsize=(0, 0),
                                                    error_message='invalid
                                                                   image!'))
  1. extensions: iterable containing allowed lowercase image file extensions ('jpg' extension of uploaded file counts as 'jpeg')
  2. maxsize: iterable containing maximum width and height of the image
  3. minsize: iterable containing minimum width and height of the image

Use (-1, -1) as minsize to pass image size check.

Examples:

Check if uploaded file is in any of supported image formats:

INPUT(_type='file', _name='name', requires=IS_IMAGE())

Check if uploaded file is either JPEG or PNG:

INPUT(_type='file', _name='name', requires=IS_IMAGE(extensions=('jpeg', 'png')))

Check if uploade file is PNG with maximum size of 200x200 pixels:

INPUT(_type='file', _name='name', requires=IS_IMAGE(extensions=('png'),
                                                    maxsize=(200, 200)))
© 2008-2010 by Massimo Di Pierro - All rights reserved - Powered by web2py - design derived from a theme by the earlybird
The content of this book is released under the Artistic License 2.0 - Modified content cannot be reproduced.