Skip to content

Instantly share code, notes, and snippets.

@martinkrung
Last active August 29, 2015 14:24
Show Gist options
  • Save martinkrung/dc57f62eb030de47ffe9 to your computer and use it in GitHub Desktop.
Save martinkrung/dc57f62eb030de47ffe9 to your computer and use it in GitHub Desktop.
template filter for django easy-thumbnails image orientation check (is_portrait)
  1. Install easy-thumbnails for django
  2. make folder templatetags in app containing empty __init__.py and is_portrait.py
  3. use filter in django template
# -*- coding: utf-8 -*-
from django import template
register = template.Library()
@register.filter(is_safe=True)
def is_portrait(im):
if im.image.height > im.image.width:
return True
else:
return False
{% load is_portrait %}
{% load thumbnail %}
{% if textmedia.image|is_portrait %}
{% thumbnail textmedia.image "360x2000" upscale=False as thumb %}
<img src="{{ thumb.url }}" alt="{{ textmedia.image.default_caption }}"><br>
{% if textmedia.default_caption %}<span class="acaption">{{ textmedia.image.default_caption }}</span><br>{% endif %}
{% else %}
{% thumbnail textmedia.image "580x2000" upscale=False as thumb %}
<img src="{{ thumb.url }}" alt="{{ textmedia.image.default_caption }}"><br>
{% if textmedia.default_caption %}<span class="acaption">{{ textmedia.image.default_caption }}</span><br>{% endif %}
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment