Giter Site home page Giter Site logo

neokya / sorl-thumbnail-async Goto Github PK

View Code? Open in Web Editor NEW

This project forked from chhantyal/sorl-thumbnail-async

0.0 2.0 1.0 91 KB

Asynchronous thumbnailing app in django with remote storages like S3

License: BSD 3-Clause "New" or "Revised" License

sorl-thumbnail-async's Introduction

sorl-thumbnail-async

Asynchronous thumbnailing app in django with remote storages like S3. This is modifications of some parts of sorl-thumbnail, which is a bit slow when used with remote storages.

  • Celery is used to create thumbnail asynchronously.
  • Thumbnails are pregenerated and cached.
  • Thumbnail sizes and options are specified in one place (your settings file).

Install

pip install sorl-thumbnail-async

Add 'thumbnail' to your INSTALLED_APPS.

Dependencies

pip install django
pip install django-celery
pip install PIL
pip install sorl-thumbnail

Usage

In your settings.py add an option called THUMBNAIL_OPTIONS_DICT, defining all your thumbnail sizes:

THUMBNAIL_OPTIONS_DICT = {
        'small': {
                'geometry': '140x140',
                'crop': 'center'
        }
    }

In your models, use thumbnail.models.AsyncThumbnailMixin as a baseclass. Make sure that your model inherits from AsyncThumbnailMixin first. This will call celery task on save(), and create one or more thumbnails from the specified image field. Use class variable image_field_name to configure the field that contains the image. Defaults to picture.

Example:

from django.db import models

from sorl import thumbnail
from thumbnail.models import AsyncThumbnailMixin


class Book(AsyncThumbnailMixin, models.Model):
    image_field_name = 'cover_image'

	title = models.CharField(blank=False, max_length=255, db_index=True)
    cover_image = thumbnail.ImageField(upload_to='books/')

In templates:

{% load thumbnail_tags %}
{% thumbnail book.cover_image small as im %}
<img src"{{ im.url }}">
{% endthumbnail %}

In python code:

from thumbnail import get_thumbnail

book = Book.objects.get(title='Life of Pi')
thumbnail_url = get_thumbnail(book.cover_image, 'small').url

Settings

You can add as many sizes and option as needed. It is a python dictionary.

THUMBNAIL_OPTIONS_DICT = {
        'small': {
                'geometry': '140x140',
                'crop': 'center'
        }
    }

NOTE: sorl-thumbnail-async registers its own THUMBNAIL_BACKEND:

THUMBNAIL_BACKEND = 'sorl-thumbnail-async.thumbnail.backend.AsyncThumbnailBackend'

sorl-thumbnail-async's People

Contributors

shanx avatar

Watchers

Neo avatar  avatar

Forkers

un33k

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.