Giter Site home page Giter Site logo

django-stdimage's Introduction

image

Iontinuous Integration

Code Health

Test Coverage

image

MIT License

Join the chat at https://gitter.im/codingjoe/django-stdimage

Django Standarized Image Field

Django Field that implement the following features:

  • Django-Storages compatible (S3)
  • Python 2, 3 and PyPy support
  • Resize images to different sizes
  • Access thumbnails on model level, no template tags required
  • Preserves original image
  • Asynchronous rendering (Celery & Co)
  • Multi threading and processing for optimum performance
  • Restrict accepted image dimensions
  • Rename files to a standardized name (using a callable upload_to)

Installation

Simply install the latest stable package using the command

easy_install django-stdimage or pip django-stdimage

and add 'stdimage' to INSTALLED_APPs in your settings.py, that's it!

Usage

StdImageField works just like Django's own ImageField except that you can specify different sized variations.

Variations

Variations are specified withing a dictionary. The key will will be the attribute referencing the resized image. A variation can be defined both as a tuple or a dictionary.

Example

For using generated variations in templates use "myimagefield.variation_name".

Example
Utils

By default StdImageField stores images without modifying the file name. If you want to use more consistent file names you can use the build in upload callables.

Example
Validators

The StdImageField doesn't implement any size validation. Validation can be specified using the validator attribute and using a set of validators shipped with this package. Validators can be used for both Forms and Models.

Example

CAUTION: The MaxSizeValidator should be used with caution. As storage isn't expensive, you shouldn't restrict upload dimensions. If you seek prevent users form overflowing your memory you should restrict the HTTP upload body size.

Deleting images

Django dropped support. for automated deletions in version 1.3. Implementing file deletion should be done. inside your own applications using the post_delete or pre_delete signal. Clearing the field if blank is true, does not delete the file. This can also be achieved using pre_save and post_save signals. This packages contains two signal callback methods that handle file deletion for all SdtImageFields of a model. .. code :: python

from stdimage import pre_delete_delete_callback, pre_save_delete_callback

post_delete.connect(pre_delete_delete_callback, sender=MyModel) pre_save.connect(pre_save_delete_callback, sender=MyModel)

Warning: You should not use the signal callbacks in production. They may result in data loss.

Async image processing

Tools like celery allow to execute time-consuming tasks outside of the request. If you don't want to wait for your variations to be rendered in request, StdImage provides your the option to pass a async keyword and a util. Note that the callback is not transaction save, but the file will be there. This example is based on celery.

tasks.py

models.py

Re-rendering variations

You might want to add new variations to a field. That means you need to render new variations for missing fields. This can be accomplished using a management command. .. code :

python manage.py rendervariations 'app_name.model_name.field_name' [--replace]

The replace option will replace all existing files.

Multi processing

Since version 2 stdImage supports multiprocessing. Every image is rendered in separate process. It not only increased performance but the garbage collection and therefore the huge memory footprint from previous versions.

Note: PyPy seems to have some problems regarding multiprocessing, for that matter all multiprocessing is disabled in PyPy.

Contributing

Getting started is easy. After setting up your env, just install:

pip install -r requirements-dev.txt; pre-commit install

To make contributing even easier, make sure your editor's or IDE's [EditorConfig] support is enabled.

Testing

To run the tests simply run python setup.py test

django-stdimage's People

Contributors

5p4k avatar anih avatar annefly avatar bitdeli-chef avatar bltravis avatar brycedarling avatar codingjoe avatar epowers avatar georgewhewell avatar gitter-badger avatar hcarvalhoalves avatar katomaso avatar louiz avatar m4l avatar martinmaillard avatar mazzly avatar mcnemesis avatar mitja avatar requires avatar tomscytale avatar vosi avatar

Watchers

 avatar  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.