Skip to main content

Django Standarized Image Field

Project description

[![Django-CC](https://img.shields.io/badge/Django-CC-ee66dd.svg)](https://github.com/codingjoe/django-cc)
[![version](https://img.shields.io/pypi/v/django-stdimage.svg)](https://pypi-hypernode.com/pypi/django-stdimage/)
[![ci](https://api.travis-ci.org/codingjoe/django-stdimage.svg?branch=master)](https://travis-ci.org/codingjoe/django-stdimage)
[![codecov](https://codecov.io/gh/codingjoe/django-stdimage/branch/master/graph/badge.svg)](https://codecov.io/gh/codingjoe/django-stdimage)
[![code-health](https://landscape.io/github/codingjoe/django-stdimage/master/landscape.svg?style=flat)](https://landscape.io/github/codingjoe/django-stdimage/master)
[![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)

# Django Standardized Image Field

Django Field that implement the following features:

* Django-Storages compatible (S3)
* 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

`pip install django-stdimage`

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

## Usage


``StdImageField`` works just like Django's own
[ImageField](https://docs.djangoproject.com/en/dev/ref/models/fields/#imagefield)
except that you can specify different sized variations.

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

Example:
```python
from django.db import models
from stdimage.models import StdImageField


class MyModel(models.Model):
# works just like django's ImageField
image = StdImageField(upload_to='path/to/img')

# creates a thumbnail resized to maximum size to fit a 100x75 area
image = StdImageField(upload_to='path/to/img',
variations={'thumbnail': {'width': 100, 'height': 75}})

# is the same as dictionary-style call
image = StdImageField(upload_to='path/to/img', variations={'thumbnail': (100, 75)})

# creates a thumbnail resized to 100x100 croping if necessary
image = StdImageField(upload_to='path/to/img', variations={
'thumbnail': {"width": 100, "height": 100, "crop": True}
})

## Full ammo here. Please note all the definitions below are equal
image = StdImageField(upload_to='path/to/img', blank=True, variations={
'large': (600, 400),
'thumbnail': (100, 100, True),
'medium': (300, 200),
})
```

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

Example:
```html
<a href="{{ object.myimage.url }}"><img alt="" src="{{ object.myimage.thumbnail.url }}"/></a>
```

### Utils

Since version 4 the custom `upload_to` utils have been dropped in favor of
[Django Dynamic Filenames][dynamic_filenames].

[dynamic_filenames]: https://github.com/codingjoe/django-dynamic-filenames

### 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
```python
from django.db import models
from stdimage.validators import MinSizeValidator, MaxSizeValidator
from stdimage.models import StdImageField


class MyClass(models.Model):
image1 = StdImageField(validators=[MinSizeValidator(800, 600)])
image2 = StdImageField(validators=[MaxSizeValidator(1028, 768)])
```

**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](https://docs.djangoproject.com/en/dev/releases/1.3/#deleting-a-model-doesn-t-delete-associated-files)
for automated deletions in version 1.3.
Implementing file deletion [should be done](http://stackoverflow.com/questions/5372934/how-do-i-get-django-admin-to-delete-files-when-i-remove-an-object-from-the-data)
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.

```python
from django.db.models.signals import pre_delete, pre_save
from stdimage.utils import pre_delete_delete_callback, pre_save_delete_callback

from . import models


pre_delete.connect(pre_delete_delete_callback, sender=models.MyModel)
pre_save.connect(pre_save_delete_callback, sender=models.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`:
```python
try:
from django.apps import apps
get_model = apps.get_model
except ImportError:
from django.apps import apps

from celery import shared_task

from stdimage.utils import render_variations


@shared_task
def process_photo_image(file_name, variations, storage):
render_variations(file_name, variations, replace=True, storage=storage)
obj = apps.get_model('myapp', 'Photo').objects.get(image=file_name)
obj.processed = True
obj.save()
```

`models.py`:
```python
from django.db import models
from stdimage.models import StdImageField

from tasks import process_photo_image

def image_processor(file_name, variations, storage):
process_photo_image.delay(file_name, variations, storage)
return False # prevent default rendering

class AsyncImageModel(models.Model):
image = StdImageField(
# above task definition can only handle one model object per image filename
upload_to='path/to/file/',
render_variations=image_processor # pass boolean or callable
)
processed = models.BooleanField(default=False) # flag that could be used for view querysets
```

### 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.
```bash
python manage.py rendervariations 'app_name.model_name.field_name' [--replace] [-i/--ignore-missing]
```
The `replace` option will replace all existing files.
The `ignore-missing` option will suspend missing source file errors and keep
rendering variations for other files. Othervise command will stop on first
missing file.

### 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](CONTRIBUTING.md)

## [License](LICENSE)



Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-stdimage-4.0.0.tar.gz (39.8 kB view details)

Uploaded Source

Built Distribution

django_stdimage-4.0.0-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file django-stdimage-4.0.0.tar.gz.

File metadata

  • Download URL: django-stdimage-4.0.0.tar.gz
  • Upload date:
  • Size: 39.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.3

File hashes

Hashes for django-stdimage-4.0.0.tar.gz
Algorithm Hash digest
SHA256 84ab389d35a03d2cfebe756c553cb847dde8afd6b74279be725d708bb8d8ac67
MD5 4d689954b3ae19bbb0718dca19b733a8
BLAKE2b-256 48e3e9a46d132557a02159f4f27c5636080d4d6595decf8659b5c7dd766c7c14

See more details on using hashes here.

Provenance

File details

Details for the file django_stdimage-4.0.0-py3-none-any.whl.

File metadata

  • Download URL: django_stdimage-4.0.0-py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.3

File hashes

Hashes for django_stdimage-4.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d13c06c5785510f8ba996f477e8fc38c67234a68476dcde3de0d173558c0a7e6
MD5 017fb8c95085f5b1e05eeb36c79d3c27
BLAKE2b-256 5636ad77ba2f389afbe2ebd45041c1f960daf565b511f7bc83fbdaf7d001c81e

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page