跳转到主要内容

使用Thumbor服务调整图像大小的django应用程序

项目描述

CI status on Travis CI Latest django-thumbor PyPI version Number of downloads for django-thumbor on PyPI

一个django应用程序,使用thumbor服务调整图像大小。

用法

both thumbor_url templatetag和generate_url辅助函数使用与libthumbor相同的参数,您可以查看wiki获取更多信息。

在模板上

{% load thumbor_tags %}
<img src="{% thumbor_url '/media/image.jpg' width=300 %}" width="300" />

or

{% load thumbor_tags %}
<img src="{% 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” />

过滤器

使用:(或使用list对象)分隔filters

{% load thumbor_tags %}
<img src="{% thumbor_url url filters='watermark(http://domain.com/watermark.png,-10,-10,20):brightness(10)' %}" />
<img src="{% thumbor_url url filters=filter_list %}" />

在代码中

from django_thumbor import generate_url
resized = generate_url("/media/image.jpg", width=300)

重新使用参数集(别名)

您可以通过全局定义别名重新使用参数集。这可以防止在代码中重复缩略图参数,并且可以提高Thumbor性能,因为缩略图也会被重复使用。如果您是从django-easy-thumbnails迁移过来,您会发现这种模式非常熟悉,并且它应该会使迁移更加直接。

在模板上

{% load thumbor_tags %}
<img src="{% thumbor_url '/media/image.jpg' alias="thumb-square" %}" />

在代码中

from django_thumbor import generate_url
resized = 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 %}
<img src="{% thumbor_url '/media/image.jpg' thumbor_server='http://localhost:8888/foo' width=300 %}" width="300" />

在代码中

from django_thumbor import generate_url
custom_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 images
THUMBOR_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 images
THUMBOR_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 statics
THUMBOR_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 images
THUMBOR_STATIC_URL = 'http://localhost:8000/static'

# The same security key used in the thumbor service to
# match the URL construction
THUMBOR_SECURITY_KEY = 'MY_SECURE_KEY'

# Default arguments passed to the `generate_url` helper or
# the `thumbor_url` templatetag
THUMBOR_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 = {}

贡献

安装

分叉、克隆、创建虚拟环境并运行

git clone git://github.com/ricobl/django-thumbor.git
cd django-thumbor
pipenv shell
make install

测试

testproject/tests 中添加测试,添加代码并运行

make test

测试服务器

  • 安装 thumbor 服务器: pip install thumbor

  • 运行 thumbor: thumbor

  • 运行本地服务器: make run

  • 访问 http://127.0.0.1:8000/

项目详情


下载文件

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

源代码分发

django_thumbor-0.6.0.tar.gz (5.4 kB 查看哈希)

上传时间 源代码

构建分发

django_thumbor-0.6.0-py3-none-any.whl (6.4 kB 查看哈希)

上传时间 Python 3