Skip to content

Instantly share code, notes, and snippets.

@sam-thecoder
Created October 25, 2019 18:40
Show Gist options
  • Select an option

  • Save sam-thecoder/01450a898973a281c163b41fa82cb4c8 to your computer and use it in GitHub Desktop.

Select an option

Save sam-thecoder/01450a898973a281c163b41fa82cb4c8 to your computer and use it in GitHub Desktop.
#inside template tag called app_tags.py
@register.filter
def image_dimensions(dimensions, ratio):
print(dimensions)
width, height = dimensions
if 'x' in ratio:
return ratio
x,y = ratio.split(":")
x,y = int(x),int(y)
ratio = x/y
req_width = int(height*ratio)
if(req_width<width):
print('{0}x{1}'.format(req_width,height))
return '{0}x{1}'.format(req_width,height)
else:
print('{0}x{1}'.format(width,int(width/ratio)))
return '{0}x{1}'.format(width,int(width/ratio))
#models for image
class ModelName(models.Model):
...
def get_dimensions(self):
return [self.image.width, self.image.height]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment