djblets.util.templatetags.djblets_images¶
Image-related template tags.
- crop_image(f, x, y, width, height)[source]¶
Crops an image at the specified coordinates and dimensions, returning the resulting URL of the cropped image.
- thumbnail(f: Union[File, str], size: Union[str, tuple[Optional[int], Optional[int]]] = '400x100', *, create_if_missing: bool = True, storage: Optional[Storage] = None) → Optional[str][source]¶
Create a thumbnail of the given image.
This will create a thumbnail of the given file, which may be a file path within storage, a
Fileinstance representing a file in storage, or a file instance retrieved from a :py:class:~django.db.models.FileField`.The thumbnail will be of the given size. This size can either be specified as a string of WIDTHxHEIGHT (in pixels), or a 2-tuple. If the size is a tuple and one of the values is None, that value will be set automatically to preserve the aspect ratio.
If the file format is not supported, then the file path will be returned as-is.
Changed in version 5.0:
Added
create_if_missingandstorageoptions.Added support for working with general
Fileobjects or file paths within storage, when providing thestorageparameter.
- Parameters:
f (
django.db.models.fields.files.FieldFile) –The file path within storage,
Fileinstance, or aFileField-backed file.if not providing a field-backed file, then
storagemust be provided.Changed in version 5.0: This may now be a file path within storage or a
Fileinstance.The thumbnail constraint size.
This can either be a string in
WIDTHxHEIGHTform, or a tuple in(width, height)form. In the latter, the height is optional.create_if_missing (
bool, optional) –Whether to create the thumbnail if one does not already exist.
If
False, the existing thumbnail URL will be returned if it exists, but a new one will not otherwise be created.New in version 5.0.
storage (
django.core.files.storage.Storage, optional) –The storage backend for the file.
This is required if the file does not provide its own
storageattribute, and is ignored if it does.New in version 5.0.
- Returns:
The URL to the thumbnail.
This will be
Noneif the thumbnail does not exist and passingcreate_if_missing=False.- Return type:
- Raises:
ValueError – The thumbnail size was not in a valid format.
- build_srcset(sources)[source]¶
Return the source set attribute value for the given sources.
The resulting sources will be sorted by value, with the pixel density (
x) values coming before width (w) values.
- srcset(sources)[source]¶
Render the source set attribute value for the given sources.
The resulting sources will be sorted by value, with the pixel density (
x) values coming before width (w) values.
- image_source_attrs(context, nodelist, *options)[source]¶
Render source attributes for an image tag.
This will render
src="..." srcset="..."attributes for an<img>tag, based on the sources provided in the tag’s content. There should be one source definition per line (with an optional trailing comma) in the form of:<descriptor> <URL>
These will get turned into a
srcset, and the1xdescriptor (which is required) will be set as thesrcattribute.- Parameters:
block_content (
unicode) – The block content containing image sources.- Returns:
Attributes for the
<img>tag.
Example
<img {% image_source_attrs %} 1x {% static "images/myimage.png" %} 2x {% static "images/myimage@2x.png" %} 3x {% static "images/myimage@3x.png" %} {% end_image_source_attrs %}>