Skip to main content

A wsgi middleware to generate and serve image thumbnails.

Project description

Usage

The Thumb middleware take four arguments:

  • application: the wsgi application to wrap.

  • url_parser: a callable to retrieve the size of the thumbnail and the attending size as a tuple (width, height) Default: iw.thumbs.url.url_parser The default value match urls like /thumbs/100x100/path/to/image.png and return (‘/path/to/image.png’, (100, 100))

  • url_regexp: a regexp passed as argument to the default url_parser. The regexp must have groups named path, width and height. Default: ‘^/+thumbs/+(?P<width>[0-9]+)x(?P<height>[0-9]+)(?P<path>/.+)’

  • image_fetcher: a callable to retrieve the real image path Default: iw.thumbs.image_fetcher The default value return the PATH_INFO so you may overide it. This callable is used to retrieve image from file system but you can also use it to retrieve image from other location like proxy, ftp, etc.

  • cache_dir: a path to the directory used to cache thumbnails (required)

  • debug: debug mode. Default: False

Example

Write a method to retrieve your image path from the PATH_INFO:

>>> def image_fetcher(path_info):
...     return os.path.join(package_dir, *path_info.split('/')[1:])

Wrap your application with the middleware:

>>> from iw.thumbs.middleware import Thumbs
>>> app = Thumbs(test_app, image_fetcher=image_fetcher, cache_dir=cache_dir)

Then ask for an image:

>>> environ = {'REQUEST_METHOD':'GET',
...            'PATH_INFO':'/thumbs/50x50/tests/logo.png'}
>>> print app.get_app(environ)
<paste.fileapp.FileApp object at ...>

Override the default url regexp:

>>> regexp = '^/thumb/(?P<width>[0-9]+)x(?P<height>[0-9]+)(?P<path>/.+)'
>>> app = Thumbs(test_app, image_fetcher=image_fetcher, cache_dir=cache_dir,
...              url_regexp=regexp)

>>> environ = {'REQUEST_METHOD':'GET', 'PATH_INFO':'/thumb/50x50/tests/logo.png'}
>>> print app.get_app(environ)
<paste.fileapp.FileApp object at ...>

If the path does not match an image, the test_app is served:

>>> environ = {'REQUEST_METHOD':'GET', 'PATH_INFO':'/index.html'}
>>> print app.get_app(environ)
<function test_app at ...>

API

Url parser:

>>> from iw.thumbs.url import default_parser
>>> default_parser('/thumbs/50x50/path/image.jpg')
('/path/image.jpg', (50, 50))

Resize an image file to the specified destination at size:

>>> from iw.thumbs.image import resize
>>> thumbnail = os.path.join(cache_dir, 'thumb.png')
>>> print resize(image_path, thumbnail, size=(50, 50))
/..._cache/thumb.png
>>> os.path.isfile(thumbnail)
True

Paste factory

The package provide a Paste factory.

Here is a sample configuration:

[pipeline:main]
pipeline = thumbs test_app

[filter:thumbs]
use = egg:iw.thumbs
url_regexp = ^/+(?P<width>[0-9]+)x(?P<height>[0-9]+)/{0,1}(?P<path>/.+)
image_dir = %(here)s/images
cache_dir = %(here)s/images_cache

[app:test_app]
use = egg:Paste#static
document_root = %(here)s/

[server:main]
use = egg:Paste#http
host = 0.0.0.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

iw.thumbs-1.0.tar.gz (81.3 kB view hashes)

Uploaded Source

Built Distribution

iw.thumbs-1.0-py2.4.egg (16.3 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page