1

我使用django-filer来管理图像。为 FilerImageField 选择一个后如何裁剪和图像?

4

1 回答 1

2

这是一个结合 django-filer 和django-image-cropping的片段。只需使用CroppableFilerImageField而不是FilerImageField您的模型。

请注意,您仍然必须在选择图像后按“保存并继续编辑”才能进行裁剪。

from django.conf import settings

from filer import settings as filer_settings
from filer.fields.image import (
    AdminImageWidget, AdminImageFormField, FilerImageField,
)
from filer.models import File


class CroppableImageWidget(AdminImageWidget):
    def render(self, name, value, attrs=None):
        if value:
            file_obj = File.objects.get(pk=value)
            attrs = attrs or {}
            attrs.update({
                'class': 'crop-thumb',
                'data-thumbnail-url':
                    file_obj.thumbnails['admin_sidebar_preview'],
                'data-field-name': name,
                'data-org-width': file_obj.width,
                'data-org-height': file_obj.height,
            })

        return super().render(name, value, attrs)

    class Media:
        js = (
            filer_settings.FILER_STATICMEDIA_PREFIX + 'js/popup_handling.js',
            getattr(settings, 'JQUERY_URL',
                    'https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'),
            "image_cropping/js/jquery.Jcrop.min.js",
            "image_cropping/image_cropping.js",
        )
        css = {'all': ("image_cropping/css/jquery.Jcrop.min.css",)}


class CroppableFormField(AdminImageFormField):
    widget = CroppableImageWidget


class CroppableFilerImageField(FilerImageField):
    default_form_class = CroppableFormField
于 2014-11-20T06:51:10.683 回答