跳转到主要内容

一个用于生成和提供图像缩略图的wsgi中间件。

项目描述

描述

ìw.thumbs` 用于提供图像缩略图。缩略图缓存在一个目录中,并通过 paste.fileapp 提供,因此返回正确的标题(ETag、If-Modified等)。

iw.thumbs 可高度配置,但如果您使用 Paste 来提供您的应用程序,您可能只需阅读此文档的最后部分。

用法

Thumb》中间件需要四个参数

  • application:要包装的wsgi应用程序。

  • url_parser:一个可调用的函数,用于检索缩略图的大小和相应的尺寸,作为元组(宽度,高度)。默认值:iw.thumbs.url.url_parser 默认值匹配类似 /thumbs/100x100/path/to/image.png 的URL,并返回(‘/path/to/image.png’,(100,100))

  • url_regexp:传递给默认url_parser的正则表达式。正则表达式必须具有名为path、width和height的组。默认值:‘^/+thumbs/+(?P<width>[0-9]{2,3})x(?P<height>[0-9]{2,3})/{0,1}(?P<path>/.+)’

  • image_fetcher:一个可调用的函数,用于检索实际图像路径。默认值:iw.thumbs.image_fetcher 默认值返回 PATH_INFO,因此您可以覆盖它。此可调用函数用于从文件系统检索图像,但您也可以使用它从其他位置检索图像,例如代理、ftp等。

  • image_dir: 存储图像的目录。如果使用默认设置,则由 image_fetcher 使用。

  • cache_dir: 缓存缩略图的目录路径(必需)

  • debug: 调试模式。默认:False

示例

编写一个方法,从 PATH_INFO 中检索您的图像路径

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

将中间件包装到您的应用程序中

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

然后请求一个图像

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

覆盖默认的 URL 正则表达式

>>> regexp = '^/my_thumbs/(?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':'/my_thumbs/50x50/tests/logo.png'}
>>> print app.get_app(environ)
<paste.fileapp.FileApp object at ...>

如果路径不匹配图像,则提供 test_app

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

使用尺寸名称

您可以使用尺寸名称

>>> app = Thumbs(test_app, image_fetcher=image_fetcher, cache_dir=cache_dir,
...              url_regexp='/thumbs/(?P<size>%s)(?P<path>/.*)',
...              url_parser='iw.thumbs.url:size_parser',
...              sizes={'thumb': (100,100), 'large': (600,600)})

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

实现自己的解析器

您需要一个可调用的函数,它返回一个可调用的函数和编译后的正则表达式。

让我们用正则表达式定义我们的策略

>>> re_thumb = '^/thumbs(?P<path>/.*)'

然后使用解析器实现它

>>> import re
>>> def custom_parser(regexp, **kwargs):
...     regexp = re.compile(regexp)
...     def parser(url):
...         m = regexp.match(url)
...         if m:
...             path = m.group('path')
...             return path, (100, 100)
...     return parser, regexp

检查此工作是否正常

>>> app = Thumbs(test_app, image_fetcher=image_fetcher, cache_dir=cache_dir,
...              url_regexp=re_thumb, url_parser=custom_parser)

>>> environ = {'REQUEST_METHOD':'GET', 'PATH_INFO':'/thumbs/tests/logo.png'}
>>> print app.get_app(environ)
<paste.fileapp.FileApp object 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 工厂。

以下是一个示例配置

[app:main]
use = egg:Paste#urlmap
/images = thumbs_app
/ = middlewares

# a standalone application to serve files in image_dir. The application is a
# static application wrapped with the iw.thumbs middleware
[app:thumbs_app]
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

[pipeline:middlewares]
pipeline = thumbs named_thumbs test_app

# an example tho override the default regexp
[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

# another example using sizes names
[filter:named_thumbs]
use = egg:iw.thumbs
url_regexp = ^/(?P<size>%s)(?P<path>/.+)
url_parser = iw.thumbs.url:size_parser
image_dir = %(here)s/images
cache_dir = %(here)s/images_cache
sizes =
    thumb = 200x200
    large = 600x600

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

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

项目详情


下载文件

下载您平台上的文件。如果您不确定选择哪个,请了解有关 安装软件包 的更多信息。

源分布

iw.thumbs-1.2.tar.gz (83.0 kB 查看哈希值)

上传时间

由以下提供支持