Add options for more control over image thumbnailing.

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

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.

Summary ID
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.
0d442d81493acc071ee9b4277372ef6c77f5ad3c
Description From Last Updated

'django.contrib.staticfiles.storage.staticfiles_storage' imported but unused Column: 1 Error code: F401

reviewbotreviewbot

"second part" is a little weird. Perhaps "one of the values is None"?

daviddavid
Checks run (1 failed, 1 succeeded)
flake8 failed.
JSHint passed.

flake8

chipx86
david
  1. 
      
  2. Show all issues

    "second part" is a little weird. Perhaps "one of the values is None"?

  3. 
      
chipx86
david
  1. Ship It!
  2. 
      
chipx86
Review request changed
Status:
Completed
Change Summary:
Pushed to release-5.x (504c1a2)