Created
October 25, 2019 18:40
-
-
Save sam-thecoder/01450a898973a281c163b41fa82cb4c8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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