Django-Filebrowser cannot create a version with auto width and constrained height?

148 views Asked by At

I've been using Django-Filebrowser and setting up my versions as specified in the docs, which indicate you specify image versions like so:

VERSIONS = getattr(settings, "FILEBROWSER_VERSIONS", {
    'thumbnail': {'verbose_name': 'Thumbnail (1 col)', 'width': 60, 'height': 60, 'opts': 'crop'},
    'small': {'verbose_name': 'Small (2 col)', 'width': 140, 'height': '', 'opts': ''},
})

...note the 'small' version size with an unspecified height, which successfully generates an image version with a constrained width and an auto height. This works successfully for me, however the reverse does not (constrained height but auto width):

'auto_width': {'verbose_name': 'Auto Width', 'width': '', 'height': 140, 'opts': ''},

Is there a trick here that I'm missing, or does the library not have this ability?

1

There are 1 answers

0
Tobias On

I just ran into the same issue and resolved it: https://github.com/sehmaschine/django-filebrowser/issues/278

There is a bug in the scale_and_crop function in utils.py when trying to generate versions with fixed height and automatic width (the other way around it works like a charm). The versions image will be created but with the original size.

Example VERSION definition:

FILEBROWSER_VERSIONS = { 
  'medium': {'verbose_name': 'Medium (4col )', 'width': '', 'height': 250, 'opts': ''},
}

The reason for it is that the width variable is an empty string which gets compared to a float, therefore the if statement returns False and the image won't get resized.

One way to fix it would be to convert the width string into a float for comparing in utils.py:

66c65
<     if 'upscale' not in opts and x < width:
---
>     if 'upscale' not in opts and x < float(width or 0):