Git Product home page Git Product logo

django-nailbiter's Introduction

django-nailbiter - a storage-agnostic thumbnail generator

nailbiter is a simple thumbnail generation field for Django, modeled after sorl-thumbnail.

Usage

First, define a model using a nailbiter thumbnail field:

In models.py:

from django.contrib.auth.models import User
from django.db import models
from nailbiter.fields import ImageWithThumbsField

class Gallery(models.Model):
    name = models.CharField(max_length=150)

class Photo(models.Model):
    uploader = models.ForeignKey(User, related_name="photos")
    gallery = models.ForeignKey(Gallery, related_name="photos")
    title = models.CharField(max_length=150)
    image_file = ImageWithThumbsField(
        upload_to = photo_upload_path,
        generate_on_save = True,
        thumbnail = {'size': (150, 150), 'options': ['detail']},
        quality: 90,
        extra_thumbnails = {
            'headline': {'size': (300, 300), 'options': ['upscale', 'detail'], 'quality':95},
            'avatar': {'size': (64, 64), 'options': ['crop', 'upscale', 'detail']},
            'gallery_icon': {'size': (150, 150), 'options': ['crop', 'upscale', 'detail'], 'quality':75},
        })
    created_date = models.DateTimeField(default=datetime.utcnow)

To display the thumbnail in a template:

<img src="{{ object.image_file.thumbnail.url }}" />

To display a thumbnail defined in extra_thumbnails, just refer to it by the name you defined:

<img src="{{ object.image_file.extra_thumbnails.headline.url }}" />

Quality

Modifying the quality of generated thumbnails has been also added (see the example above). This can be done on a field scope (high-level) or thumbnail-size scope (detailed-level). Both are optional; if neither is defined, then the default PIL save quality (75) will be used. Quality defined for a thumbnail size will override field-level quality.

Cleanup

If you want to make sure that previously uploaded files are removed when you re-upload, handle the pre_save signal for your model. For example:

# remove previous photo files and thumbnails!
def cleanup_photo(sender, **kwargs):
    instance = kwargs["instance"]
    try:
        original = Photo.image_file.get(id=instance.id)
        if original.photo:
            original.image_file.delete(False)
    except:
        pass

pre_save.connect(cleanup_photo, sender=Photo)

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.