Jump to >

This documentation covers the in-development release of Djblets. You can see the latest stable docs or all previous versions.

djblets.util.templatetags.djblets_images

save_image_to_storage(image, storage, filename)[source]

Save an image to storage.

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, size=u'400x100')[source]

Create a thumbnail of the given image.

This will create a thumbnail of the given file (a Django FileField or ImageField) with the given size. Size can either be a string of WxH (in pixels), or a 2-tuple. If the size is a tuple and the second part is None, it will be calculated to preserve the aspect ratio.

If the image format is not registered in PIL the thumbnail is not generated and returned as-is.

This will return the URL to the stored thumbnail.

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.

Parameters:sources (dict) – A mapping of descriptors (e.g., '2x' or '512w') that describe the requirement for the source to be shown to URLs.
Returns:The returned srcset attribute value.
Return type:unicode
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.

Parameters:sources (dict) – A mapping of descriptors (e.g., '2x' or '512w') that describe the requirement for the source to be shown to URLs.
Returns:The rendered srcset attribute value.
Return type:unicode
image_source_attrs(parser, token)[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 the 1x descriptor (which is required) will be set as the src attribute.

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 %}>