在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:jonasundderwolf/django-image-cropping开源软件地址:https://github.com/jonasundderwolf/django-image-cropping开源编程语言:Python 74.4%开源软件介绍:django-image-croppingdjango-image-cropping is an app for cropping uploaded images via Django's admin backend using Jcrop. Screenshot: django-image-cropping is perfect when you need images with a specific size for your templates but want your users or editors to upload images of any dimension. It presents a selection with a fixed aspect ratio so your users can't break the layout with oddly-sized images. The original images are kept intact and only get cropped when they are displayed. Large images are presented in a small format, so even very big images can easily be cropped. The necessary fields, widgets and a template tag for displaying the cropped image in your templates are provided. Also works with FeinCMS content types! Installation
By default The easy-thumbnails backend requires that you adjust the thumbnail processors in your INSTALLED_APPS = [ ... 'easy_thumbnails', 'image_cropping', ] from easy_thumbnails.conf import Settings as thumbnail_settings THUMBNAIL_PROCESSORS = ( 'image_cropping.thumbnail_processors.crop_corners', ) + thumbnail_settings.THUMBNAIL_PROCESSORS ConfigurationAdd an The The size is passed in as a string and defines the aspect ratio of the selection as well as the minimum size for the final image: from django.db import models from image_cropping import ImageRatioField class MyModel(models.Model): image = models.ImageField(blank=True, upload_to='uploaded_images') # size is "width x height" cropping = ImageRatioField('image', '430x360') You can configure a size warning if users try to crop a selection smaller than the defined minimum. Admin IntegrationAdd the from django.contrib import admin from image_cropping import ImageCroppingMixin class MyModelAdmin(ImageCroppingMixin, admin.ModelAdmin): pass admin.site.register(MyModel, MyModelAdmin) If your setup is correct you should now see the enhanced image widget that provides a selection area. Backendsdjango-image-cropping delegates the cropped image generation to a backend. A backend based on easy-thumbnails is provided, but it's possible to use a custom backend.
The In case you use a custom backend you can provide an optional dict that will be used to populate the backend's constructor params. Default settings: IMAGE_CROPPING_BACKEND = 'image_cropping.backends.easy_thumbs.EasyThumbnailsBackend' IMAGE_CROPPING_BACKEND_PARAMS = {} Frontenddjango-image-cropping provides a templatetag for displaying a cropped thumbnail.
Any other processor parameter (like {% cropped_thumbnail yourmodelinstance "ratiofieldname" [scale=INT|width=INT|height=INT|max_size="INTxINT"] %} Example usage: {% load cropping %} <img src="{% cropped_thumbnail yourmodel "cropping" scale=0.5 %}"> Or generate the URL from Python code in your view: from image_cropping.utils import get_backend thumbnail_url = get_backend().get_thumbnail_url( yourmodel.image, { 'size': (430, 360), 'box': yourmodel.cropping, 'crop': True, 'detail': True, } ) easy_thumbnailsYou can also use the standard {% load thumbnail %} {% thumbnail yourmodel.image 430x360 box=yourmodel.cropping crop detail %} Or generate the URL from Python code in your view: from easy_thumbnails.files import get_thumbnailer thumbnail_url = get_thumbnailer(yourmodel.image).get_thumbnail({ 'size': (430, 360), 'box': yourmodel.cropping, 'crop': True, 'detail': True, }).url ModelFormIf you want to use the cropping widget outside the admin, you'll need to define the from django.db import models from image_cropping import ImageCropField, ImageRatioField class MyModel(models.Model): image = ImageCropField(blank=True, upload_to='uploaded_images') # size is "width x height" cropping = ImageRatioField('image', '430x360') Alternatively, override the widget in your ModelForm (you just need to do one of these two, not both!): from django import forms from image_cropping import ImageCropWidget class MyModelForm(forms.ModelForm): class Meta: widgets = { 'image': ImageCropWidget, } Remember to include the form media in the <html> <head> {{ form.media }} </head> <body> {{ form }} </body> </html> The cropping itself happens in the If you're selectively including or excluding fields from the ModelForm, remember to include the Multiple formatsIf you need the same image in multiple formats, simply specify another from image_cropping import ImageRatioField, ImageCropField image = ImageCropField(blank=True, upload_to='uploaded_images') # size is "width x height" list_page_cropping = ImageRatioField('image', '200x100') detail_page_cropping = ImageRatioField('image', '430x360') In your templates, use the corresponding ratio field: {% load cropping %} {% cropped_thumbnail yourmodel "list_page_cropping" %} Foreign KeysIf you need to crop an image contained within another model, referenced by a ForeignKey, the from django.db import models from image_cropping.fields import ImageRatioField class Image(models.Model): image_field = models.ImageField(upload_to='image/') class NewsItem(models.Model): title = models.CharField(max_length=255) image = models.ForeignKey(Image) cropping = ImageRatioField('image__image_field', '120x100') Cropping foreign keys only works in the admin for now, as it reuses the Free croppingIf you do not need a fixed ratio, you can disable this constraint by setting from image_cropping import ImageRatioField, ImageCropField image = ImageCropField(blank=True, upload_to='uploaded_images') # size is "width x height" so a minimum size of 200px x 100px would look like this: min_free_cropping = ImageRatioField('image', '200x100', free_crop=True) Use the <img src="{% cropped_thumbnail image "cropping_free" max_size="200x200" %}" /> Disabling croppingIf you want cropping to be optional, use Editors can now switch off cropping by unchecking a checkbox next to the image cropping widget: image_with_optional_cropping = ImageRatioField('image', '200x100', allow_fullsize=True) SettingsThumbnail sizeYou can define the maximum size of the admin preview thumbnail in your # size is "width x height" IMAGE_CROPPING_THUMB_SIZE = (300, 300) Size warningYou can warn users about crop selections that are smaller than the size defined in the To use this functionality for a single image add the cropping = ImageRatioField('image', '430x360', size_warning=True) You can enable this functionality project-wide by adding the following line to your IMAGE_CROPPING_SIZE_WARNING = True Custom jQueryBy default the image cropping widget uses the jQuery version vendored with the Django admin. You can point to another version using the You can also set Custom backendYou can define a custom backend: IMAGE_CROPPING_BACKEND = 'image_cropping.backends.easy_thumbs.EasyThumbnailsBackend' You can provide an optional dict that will be used to populate the backend's constructor: IMAGE_CROPPING_BACKEND_PARAMS = {'version_suffix': 'thumb'} See the built-in backends on Backends. Testing
Troubleshooting
Changelog1.71.6.2
1.6.1
1.6
1.5
1.4
1.3
1.2
1.1
1.0.4
1.0"If your software is being used in production, it should probably already be 1.0.0." (http://semver.org) 0.9This release addresses mainly the test coverage and internal stuff. Noteable (breaking) changes and things to be considered when upgrading from an older version:
0.8
0.7
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论