在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:xarg/django-stdimage开源软件地址:https://github.com/codingjoe/django-stdimage开源编程语言:Python 100.0%开源软件介绍:Django Standardized Image FieldDjango Field that implement the following features:
InstallationSimply install the latest stable package using the command pip install django-stdimage
# or
pipenv install django-stdimage and add Usage
The VariationsVariations 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: from django.db import models
from stdimage import StdImageField, JPEGField
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)})
# variations are converted to JPEGs
jpeg = JPEGField(
upload_to='path/to/img',
variations={'full': (None, None), '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),
}, delete_orphans=True) For using generated variations in templates use Example: <a href="{{ object.myimage.url }}"><img alt="" src="{{ object.myimage.thumbnail.url }}"/></a> UtilsSince version 4 the custom ValidatorsThe Example 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 imagesDjango dropped support for automated deletions in version 1.3. Since version 5, this package supports a from django.db import models
from stdimage.models import StdImageField
class MyModel(models.Model):
image = StdImageField(
upload_to='path/to/files',
variations={'thumbnail': (100, 75)},
delete_orphans=True,
blank=True,
) Async image processingTools 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.
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()
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 variationsYou 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. python manage.py rendervariations 'app_name.model_name.field_name' [--replace] [-i/--ignore-missing] The |
2022-08-15
2022-08-17
2022-09-23
2023-10-27
2022-08-18
请发表评论