Add options for more control over image thumbnailing.

Review Request #13869 — Created May 20, 2024 and submitted — Latest diff uploaded

Information

Djblets
release-5.x

Reviewers

The {% thumbnail %} template filter has been around a long time, and
has remained pretty static in that time. It took in a FileField-backed
file instance, a WIDTHxHEIGHT string or (width, height) tuple (with
an optional height), and generated a thumbnail if one didn't exist.

It wasn't able to work with arbitrary File instances or file paths in
a storage backend, couldn't build an aspect ratio around just a height,
or tell us the path of a thumbnail without first generating it. And the
code was playing it fast and loose with input types.

This change modernizes much of this method. It's now type-safe, capable
of working with a range of input types representing files, and
sanity-checks the files and sizes for validity.

It can now cap to either a width or to a height, maintaining aspect
ratio.

It can be told not to create a thumbnail if it doesn't already exist,
helping to retrieve the URL to a thumbnail in order to, say, delete it,
without doing the work of uselessly creating one first.

Unit tests were added to check all thumbnailing functionality.

Unit tests passed.

Made use of the new create_if_missing and file input types
functionality in an in-progress change.

Diff Revision 2

This is not the most recent revision of the diff. The latest diff is revision 3. See what's changed.

orig
1
2
3

Commits

First Last Summary ID Author
Add options for more control over image thumbnailing.
The `{% thumbnail %}` template filter has been around a long time, and has remained pretty static in that time. It took in a `FileField`-backed file instance, a `WIDTHxHEIGHT` string or `(width, height)` tuple (with an optional `height`), and generated a thumbnail if one didn't exist. It wasn't able to work with arbitrary `File` instances or file paths in a storage backend, couldn't build an aspect ratio around just a height, or tell us the path of a thumbnail without first generating it. And the code was playing it fast and loose with input types. This change modernizes much of this method. It's now type-safe, capable of working with a range of input types representing files, and sanity-checks the files and sizes for validity. It can now cap to either a width or to a height, maintaining aspect ratio. It can be told not to create a thumbnail if it doesn't already exist, helping to retrieve the URL to a thumbnail in order to, say, delete it, without doing the work of uselessly creating one first. Unit tests were added to check all thumbnailing functionality.
9d46b2a50c06a288fb268ed461bcb4a320ac507e Christian Hammond
djblets/util/templatetags/djblets_images.py
djblets/util/tests/test_djblets_image_tags.py
djblets/util/tests/testdata/sample-tall.png
djblets/util/tests/testdata/sample-wide.png
Loading...