Skip to content

ThumbnailFile.width and ThumbnailFile.height not accessible in template #213

Closed
jacksont opened this Issue Mar 25, 2013 · 3 comments

2 participants

@jacksont

The following template code does not return height and width values due to a "VariableDoesNotExist" error:

    {% thumbnail image 791x527 as im %}
    {{ im.height }} {{ im.width }}

The very first time after saving an image, im.height and im.width return the correct value, but fail thereafter.

This works in Django 1.4 but fails in Django 1.5.

@jacksont

This is the workaround I'm using, a simple template filter to calculate the scaled values. (It could be more complex to accomodate, cropping etc...).

{% for image in images %}
    {% thumbnail image 791x527 upscale="True" as im %}
    {{ image|scaledheight:"791x527" }} - {{ image|scaledwidth:"791x527" }}
{% endfor %}

With corresponding template filters:

@register.filter()
def scaledheight(image, arg):

    source_y = image.height
    source_x = image.width

    target_x = float(arg.split('x')[0])
    target_y = float(arg.split('x')[1])

    scale = min(target_x / source_x, target_y / source_y)

    return int(round(source_y * scale))
scaledheight.is_safe = False


@register.filter()
def scaledwidth(image, arg):

    source_y = image.height
    source_x = image.width

    target_x = float(arg.split('x')[0])
    target_y = float(arg.split('x')[1])

    scale = min(target_x / source_x, target_y / source_y)

    return int(round(source_x * scale))
scaledwidth.is_safe = False

I would write a patch to fix the problem, but I have idea what the root cause is, or how to go about diagnosing it.

@SmileyChris
Owner

Apologies for never replying to this.
If we could get a test that proves this failure it'd be helpful! We're using tox now so you can use it to test against multiple Django / Python versions.

@SmileyChris
Owner

I did some tests on this (testing that dimensions are still accessible of an already-cached thumbnail) and couldn't see any problem.
Requires more information to be able to debug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.