一个django应用程序,使用thumbor服务调整图像大小
hmtvltk2-django-thumbor的Python项目详细描述
一个django应用程序,使用 thumbor服务。在
使用
thumbor_urltemplatetag和generate_url助手都使用相同的方法 参数为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_thumb or_url。在
{% load thumbor_tags %} {% assign_thumbor_url ‘/media/image.jpg’ width=300 as thumb_url %} <img src=”{{ thumb_url }}” width=”300” />
过滤器
拆分filters与 :(或使用list对象):
^{pr2}$代码:
fromdjango_thumborimportgenerate_urlresized=generate_url("/media/image.jpg",width=300)
Re使用参数集(别名)
可以通过全局定义的别名重用参数集。这样可以防止 在代码中重复缩略图参数,可以改进thumbor 因为缩略图也被重复使用。如果你要迁移 从django-easy缩略图中,您会发现模式非常熟悉,而且 应该使移植更加直接。在
在模板上:
{% 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={}
贡献
安装
分叉,克隆,创建一个virtualenv然后运行:
git clone git://github.com/ricobl/django-thumbor.git mkvirtualenv django-thumbor make install
测试
在testproject/tests上添加测试,添加代码并运行:
make test
- 项目
标签: