Giter VIP home page Giter VIP logo

django-thumbor's Introduction

django-thumbor

Test Build Status Latest django-thumbor PyPI version Number of downloads for django-thumbor on PyPI Documentation Status

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.

On templates:

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

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

{% load thumbor_tags %}
{% assign_thumbor_url '/media/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 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 = {}

Contributing

Install

Fork, clone, create a virtualenv and run:

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

Test

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

make test

Test Server

  • Instal thumbor server: pip install thumbor
  • Run thumbor: thumbor
  • Run local server: make run
  • visit http://127.0.0.1:8000/:

Releasing

Refer to the .pypirc reference for details on setting up API tokens.

Install build to build a package and twine to upload:

make setup_build

Upload to the test server:

make upload_test

If everything goes well, release to the real PyPI server:

make release

django-thumbor's People

Contributors

avelino avatar dependabot[bot] avatar hakanw avatar henriquechehad avatar nuschk avatar ricobl avatar starou avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

django-thumbor's Issues

How to apply filters in template tag?

Hi, I can use generate_url from shell and view correctly

>>> from django_thumbor import generate_url
>>> resized = generate_url("/some/image.jpg", width=300, filters=["watermark(/images/watermark.png,-10,-10,20)"])
>>> resized
'http://example.com/LZe05kULGfyrG6RSeiXXpj3es3I=/300x0/filters:watermark(/images/watermark.png,-10,-10,20)/some/image.jpg'

but I can't find the way to use the filters for in template with template tag

Thumbor 7

@ricobl I'm compiling an awesome thumbor page and would like to feature this extension as thumbor 7 compliant. Do you know if that's the case?

Templatetag thumbor_url accept image instance

When access model.image.url to get image URL string and pass it to thumbor_url templatetag, if the image field doesn't have an associated file, returns the error:

ValueError: The 'image' attribute has no file associated with it.

I added a support with this validation for directly image param in thumbor_url templatetag in this PR:
#11

Can I use this with public and private remote storage on S3?

We use django-storages to access media from S3. Some objects are private, requiring a signed URL to access.

Looking at django-thumbor, it seems that it does a pretty naive concatenation of THUMBOR_MEDIA_URL (an absolute version of MEDIA_URL) and the requested object.

I think we could construct a value for THUMBOR_MEDIA_URL that should work for public objects, but won't work for private objects.

I notice that there's https://github.com/thumbor-community/aws which supports loading private objects from S3.

Does django-thumbor have any support (or plans to support) loading public and private images from S3, either via the AWS loader above or otherwise?

Add assignment template tag

In some situations it's useful to have both, a normal template tag as well as an assignment tag. It leads to less repetition and easier to read code. Also, you'll be able to pass the thumb url down to a partial html template.

{% assign_thumbor_url '/media/test.jpn' alias="thumb" as thumb_url%}
<img src="{{thumb_url}}">

'NoneType' object has no attribute 'startswith' if no images

Hi, django thumbor raises an exception if there is no image given. here is the traceback
AttributeError: 'NoneType' object has no attribute 'startswith'
if url.startswith(settings.MEDIA_URL):
he is testing directly the url. Would make more sense if he tested first if there is an url

Add thumbnail aliases

I think it would be great to have thumbnail aliases in django-thumbor. An alias is basically a string you would pass to generate_url() instead of the usual parameters. This facilitates reuse of thumbor settings and thus positively affecting thumbor overall performance.

Another use case is for people (like in my case) migrating over from easy-thumbnails, where this is used in all cases, and who already have defined their thumbnails application-wide.

Alias definitions would need to go into your django settings file and could look something like this:

THUMBOR_ALIASES = {
    'thumb_64x64': dict(width=64, height=64, filters=["quality(95)"]),
    'avatar': dict(width=600, height=360, filters=["quality(70)"]),
}

Then using it could be made using the generate_url() function as well:

generate_url(my_img.url, alias="avatar")

And very similarly in templates:

{% thumbor_url my_img.url alias="avatar" %}

I'm happy to create a PR if people think this idea is worthwile.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.