Champs de modèle
ResizedImageFieldFile
¶
Bases: ImageFieldFile
get_resized_dimensions(image)
¶
Get the dimensions of the resized image.
If the width and height are given, they are used. If only one is given, the other is calculated to keep the same ratio.
Returns:
Type | Description |
---|---|
tuple[int, int]
|
Tuple of width and height |
Source code in core/fields.py
get_name()
¶
Get the name of the resized image.
If the field has a force_format attribute, the extension of the file will be changed to match it. Otherwise, the name is left unchanged.
Raises:
Type | Description |
---|---|
ValueError
|
If the image format is unknown |
Source code in core/fields.py
ResizedImageField(width=None, height=None, force_format=None, **kwargs)
¶
Bases: ImageField
A field that automatically resizes images to a given size.
This field is useful for profile pictures or product icons, for example.
The final size of the image is determined by the width and height parameters :
- If both are given, the image will be resized to fit in a rectangle of width x height
- If only one is given, the other will be calculated to keep the same ratio
If the force_format parameter is given, the image will be converted to this format.
Examples:
To resize an image with a height of 100px, without changing the ratio, and a format of WEBP :
To explicitly resize an image to 100x100px (but possibly change the ratio) :
Raises:
Type | Description |
---|---|
FieldError
|
If neither width nor height is given |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
width
|
int | None
|
If given, the width of the resized image |
None
|
height
|
int | None
|
If given, the height of the resized image |
None
|
force_format
|
str | None
|
If given, the image will be converted to this format |
None
|