Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

ixc/django-thumbor

 
 

Repository files navigation

django-thumbor

CI status on Travis CI Latest django-thumbor PyPI version Number of downloads for django-thumbor on PyPI Code coverage on Coveralls Dependency Status on Gemnasium

A django application to resize images using the thumbor service.

Usage

Both thumbor_url templatetag and the generate_url helper uses the same arguments as libthumbor, you can check the wiki for more info.

You can pass an absolute URL, protocol relative URL (which Thumbor will attempt to load over HTTP), a path relative URL (beginning with /, which will be prefixed with the value of THUMBOR_ROOT_URL), or a name to be used with the default storage class.

On templates:

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

or

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

or

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

or

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

or

{% load thumbor_tags %}
<img src="{% thumbor_url model.image_field width=300 %}" width="300" />

If you need a thumbnail for a static file:

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

is equivalent to

{% load staticfiles thumbor_tags %}
<img src="{% static "image.jpg" as image_url %}{% thumbor_url image_url width=300 %}" width="300" />

If you need the result in a template variable, use assign_thumbor_url or assign_static_thumbor_url, instead.

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

or

{% load thumbor_tags %}
{% assign_static_thumbor_url 'image.jpg' width=300 as thumb_url %}
<img src="{{ thumb_url }}" width="300" />

Filters

Split filters with : (or use a list object):

{% 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 %}" />

On code:

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

Re-using argument sets (aliases)

You can re-use argument sets through globally defined aliases. This prevents repeating thumbnail parameters all over the code and can improve thumbor performance because thumbnails are re-used as well. If you're migrating from django-easy-thumbnails, you'll find the pattern very familiar, and it should make porting much more straight-forward.

On templates:

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

On code:

from django_thumbor import generate_url
resized = generate_url("/media/image.jpg", alias="thumb-square")

And in your settings.py:

THUMBOR_ALIASES = {
    'thumb-square': {
        'width': 300,
        'height': 300,
        'filters': ['brightness(10)']}
}

Override server address

There is an extra parameter to specify a custom server to be used instead of settings.THUMBOR_SERVER.

On templates:

{% load thumbor_tags %}
<img src="{% thumbor_url '/media/image.jpg' thumbor_server='http://localhost:8888/foo' width=300 %}" width="300" />

On code:

from django_thumbor import generate_url
custom_server = "http://localhost:8888/foo"
resized = generate_url(
    "/media/image.jpg", thumbor_server=custom_server, width=300)

Installation

pip install django-thumbor

Configuration

Add the app to the INSTALLED_APPS:

INSTALLED_APPS = (
    # ...
    'django_thumbor',
)

Here are the default settings that you can override:

# The host serving the thumbor resized images
THUMBOR_SERVER = 'http://localhost:8888'

# The prefix for the host serving images with relative URLs.
# This must be a resolvable address to allow thumbor to reach the images.
THUMBOR_ROOT_URL = getattr(
    settings, 'THUMBOR_ROOT_URL', 'http://localhost:8000').rstrip('/')

# 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 = {}

# Strip `http://` prefix for prettier URLs. Thumbor's HTTP loader will
# add these back in, but this will break HTTP loading via the
# `TC_AWS_ENABLE_HTTP_LOADER=True` setting for `thumbor-community/aws`.
THUMBOR_STRIP_HTTP = True

Contributing

Install

Fork, clone, create a virtualenv and run:

git clone git://github.com/ricobl/django-thumbor.git
mkvirtualenv django-thumbor
make install

Test

Add tests on testproject/tests, add code and run:

make test

About

A django app with templatetags for resizing images with thumbor

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 98.0%
  • Makefile 2.0%