使用thumbor服务调整图像大小的django应用程序
django-thumbor的Python项目详细描述
django应用程序使用 thumbor服务。
用法
thumbor_urltemplateTag和generate_urlhelper都使用相同的 参数为libthumbor,您可以 查看wiki了解更多信息。
在模板上:
{% load thumbor_tags %} <imgsrc="{% thumbor_url '/media/image.jpg' width=300 %}"width="300"/> or {% load thumbor_tags %} <imgsrc="{% thumbor_url model.image_field width=300 %}"width="300"/>
如果需要模板变量中的结果,请改用assign_thumbor_url。
{% load thumbor_tags %} {% assign_thumbor_url ‘/media/image.jpg’ width=300 as thumb_url %} <img src=”{{ thumb_url }}” width=”300” />
过滤器
使用拆分filters:(或使用list对象):
{% load thumbor_tags %} <imgsrc="{% thumbor_url url filters='watermark(http://domain.com/watermark.png,-10,-10,20):brightness(10)' %}"/><imgsrc="{% thumbor_url url filters=filter_list %}"/>
打开代码:
fromdjango_thumborimportgenerate_urlresized=generate_url("/media/image.jpg",width=300)
重新使用参数集(别名)
可以通过全局定义的别名重用参数集。这防止 在代码中重复缩略图参数,可以改进thumbor 性能,因为缩略图也被重复使用。如果你要迁移 从django简单的缩略图中,您会发现模式非常熟悉,而且 会使移植更加直接。
在模板上:
{% load thumbor_tags %} <imgsrc="{% thumbor_url '/media/image.jpg' alias="thumb-square"%}"/>
打开代码:
fromdjango_thumborimportgenerate_urlresized=generate_url("/media/image.jpg",alias="thumb-square")
在你的settings.py:
THUMBOR_ALIASES={'thumb-square':{'width':300,'height':300,'filters':['brightness(10)']}}
覆盖服务器地址
有一个额外的参数指定要使用的自定义服务器,而不是 settings.THUMBOR_SERVER。
在模板上:
{% load thumbor_tags %} <imgsrc="{% thumbor_url '/media/image.jpg' thumbor_server='http://localhost:8888/foo' width=300 %}"width="300"/>
打开代码:
fromdjango_thumborimportgenerate_urlcustom_server="http://localhost:8888/foo"resized=generate_url("/media/image.jpg",thumbor_server=custom_server,width=300)
安装
pip install django-thumbor
配置
将应用程序添加到INSTALLED_APPS:
INSTALLED_APPS=(# ...'django_thumbor',)
以下是您可以覆盖的默认设置:
# The host serving the thumbor resized imagesTHUMBOR_SERVER='http://localhost:8888'# The prefix for the host serving the original images# This must be a resolvable address to allow thumbor to reach the imagesTHUMBOR_MEDIA_URL='http://localhost:8000/media'# If you want the static to be handled by django thumbor# default as False, set True to handle it if you host your staticsTHUMBOR_STATIC_ENABLED=False# The prefix for the host serving the original static images# this must be a resolvable address to allow thumbor to reach the imagesTHUMBOR_STATIC_URL='http://localhost:8000/static'# The same security key used in the thumbor service to# match the URL constructionTHUMBOR_SECURITY_KEY='MY_SECURE_KEY'# Default arguments passed to the `generate_url` helper or# the `thumbor_url` templatetagTHUMBOR_ARGUMENTS={}# An alias represents a named set of arguments to the generate_url function# or thumbor_url template tag. Use it to share general thumbnail# configurations without repeating yourself.THUMBOR_ALIASES={}
贡献
安装
fork,克隆,创建一个virtualenv并运行:
git clone git://github.com/ricobl/django-thumbor.git mkvirtualenv django-thumbor make install
测试
在testproject/tests上添加测试,添加代码并运行:
make test