Skip to main content

django-client-side-image-cropping

Widget for the Django ImageField that provides an interface for cropping the image client-side (Using the Croppie Javascript library) to a specific size. Compatible with any Django form, including django-admin sites.

This widget differs from django-image-cropping because it performs the cropping operation in the browser instead of on the server.

  • Use django-image-cropping if you want to save a copy of the original image on your server.
  • Use django-client-side-image-cropping (this widget) if you don't need the original image. It will save you storage space and bandwidth.

Installation and setup

Install django-client-side-image-cropping (DCSIC) using pip :

pip install django-client-side-image-cropping

Make sure that 'django.contrib.staticfiles' is set up properly and add 'client_side_image_cropping' to your INSTALLED_APPS setting :

INSTALLED_APPS = [
    # ...
    'django.contrib.staticfiles',
    # ...
    'client_side_image_cropping',
]

STATIC_URL = '/static/'

Including static JS and CSS files

DCSIC needs a few Javascript and CSS files to function. If you use the widget in a django-admin site, you can use the first method to include these files. For all other sites, you should read the "For generic forms" section below.

For admin sites

Let your Admin classes inherit from DcsicAdminMixin, which will instruct the admin interface to include the necessary files:

from client_side_image_cropping import DcsicAdminMixin

class EbookAdmin(admin.ModelAdmin, DcsicAdminMixin):
    form = EbookForm

For generic forms

Every page containing a form using the ClientsideCroppingWidget should include jQuery 1.9 (or newer) and two JS and CSS files in the head section:

{% load static %}

<head>
    <link rel="stylesheet" href="{% static "client_side_image_cropping/croppie.css" %}">
    <link rel="stylesheet" href="{% static "client_side_image_cropping/cropping_widget.css" %}">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="{% static "client_side_image_cropping/croppie.min.js" %}"></script>
    <script src="{% static "client_side_image_cropping/cropping_widget.js" %}"></script>
</head>

Usage

Use the ClientsideCroppingWidget for any django.forms.ImageField in a Form :

from client_side_image_cropping import ClientsideCroppingWidget

class EbookForm(forms.ModelForm):
    class Meta:
        model = Ebook
        fields = ['title', 'cover_photo']
        widgets = {
            'cover_photo': ClientsideCroppingWidget(
                width=400,
                height=600,
                preview_width=100,
                preview_height=150,
            )
        }

Constructor parameters for the ClientsideCroppingWidget:

  • width and height (int, required): Dimensions of the resulting image (after cropping)
  • preview_width and preview_height (int): Dimensions of the preview of the cropped image that will appear in the form. Defaults to the width and height paramters if omitted.
  • clearable (bool): Add a "Delete image" button to the widget. If omitted, the button will be visible only if the required parameter of the ImageField is set ot False.
  • format (str): The file format of the resulting image. Must be either 'jpeg' (default), 'png' or 'webp'.
  • quality (int): The quality of the image as a percentage. Only applicable if format is set to 'jpeg'. Set to a low value for high compression, or 100 for lossless compression.
  • file_name (str): File name of the cropped image. If omitted, a random file name will be generated to avoid name collisions.

Metadata

Release files for django-client-side-image-cropping 0.1.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-client-side-image-cropping 0.1.10
File Size Uploaded
django-client-side-image-cropping-0.1.10.tar.gz 21.8 kB Details

Release files / django-client-side-image-cropping-0.1.10.tar.gz

Download URL django-client-side-image-cropping-0.1.10.tar.gz
Size 21.8 kB
Tags Source
SHA-256 checksum
How to use checksums
9f05d543fa5445f090c2bb790a6e2f578624b178e556852f26f19a1a9dce3384
BLAKE2b-256 checksum
How to use checksums
8596193f95e396ed960eec33dda3c7da864277789ab875d85cec0a1b638a7d71
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.2

Release history Release notifications | RSS feed

This release

0.1.10 This release

1 release file

0.1.9

1 release file

0.1.8

1 release file

0.1.7

1 release file

0.1.6

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page