Giter Site home page Giter Site logo

easy-thumbnails's Introduction

===============
Easy Thumbnails
===============

The powerful, yet easy to implement thumbnailing application for Django.

To install this application into your project, just add it to your
``INSTALLED_APPS`` setting (and run ``manage.py syncdb``)::

    INSTALLED_APPS = (
        ...
        'easy_thumbnails',
    )


Template usage
==============

To generate thumbnails in your template, use the ``{% thumbnail %}`` tag. To
make this tag available for use in your template, use::
    
    {% load thumbnail %}

Basic tag Syntax::

    {% thumbnail [source] [size] [options] %}

*source* must be a ``File`` object, usually an Image/FileField of a model
instance.

*size* can either be:

* the size in the format ``[width]x[height]`` (for example,
  ``{% thumbnail person.photo 100x50 %}``) or

* a variable containing a valid size (i.e. either a string in the
  ``[width]x[height]`` format or a tuple containing two integers):
  ``{% thumbnail person.photo size_var %}``.

*options* are a space separated list of options which are used when processing
the image to a thumbnail such as ``sharpen``, ``crop`` and ``quality=90``.


Model usage
===========

You can use the ``ThumbnailerField`` or ``ThumbnailerImageField`` fields (based
on ``FileField`` and ``ImageField``, respectively) for easier access to
retrieve (or generate) thumbnail images.

By passing a ``resize_source`` argument to the ``ThumbnailerImageField``, you
can resize the source image before it is saved::

    class Profile(models.Model):
        user = models.ForeignKey('auth.User')
        avatar = ThumbnailerImageField(
            upload_to='avatars',
            resize_source=dict(size=(50, 50), crop='smart'),
        )


Lower level usage
=================

Thumbnails are generated with a ``Thumbnailer`` instance. For example::

    from easy_thumbnails.files import get_thumbnailer

    def square_thumbnail(source):
        thumbnail_options = dict(size=(100, 100), crop=True, bw=True)
        return get_thumbnailer(source).get_thumbnail(thumbnail_options)

By default, ``get_thumbnail`` saves the file (using file storage). The source
file used to instanciate the ``Thumbnailer`` must be one of the following:

* ``Thumbnailer`` instance

* ``FieldFile`` instance (i.e. a model instance file/image field
  property)

* ``File`` or ``Storage`` instance, and for both of these cases the
  ``relative_name`` argument must also be provided

* A string, which will be used as the relative name (the source will be
  set to the default storage)


The ``ThumbnailFile`` object provided makes this easy::

	from easy_thumbnails import ThumbnailFile

	# For an existing file in storage:
	source = ThumbnailFile('animals/aarvark.jpg')
	square_thumbnail(source)
	
	# For a new file:
	picture = open('/home/zookeeper/pictures/my_anteater.jpg')
	source = ThumbnailFile('animals/anteater.jpg', file=picture)
	square_thumbnail(source)

easy-thumbnails's People

Contributors

smileychris avatar stefanfoulis avatar fabiant7t avatar silvertab avatar

Watchers

Rob Boyle avatar James Cloos avatar

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.