def get_for_resolution(self, resolution): from os import path, makedirs from math import ceil dest_width, dest_height = [int(side) for side in resolution.split('x')] # /path/to/directory file_root = self.file[:self.file.rfind('/')] # filename.ext file_name = self.file[self.file.rfind('/') + 1:] # /path/to/directory/1280x800 dest_root = path.join(file_root, resolution) # /path/to/directory/1280x800/filename.ext dest_path = path.join(dest_root, file_name) if not path.exists(dest_path): if not path.exists(dest_root): makedirs(dest_root) file_orig = Image(self.file) if self.width != dest_width or self.height != dest_height: rule_width = round( (float(dest_width)*float(100))/float(self.width)) rule_height = round( (float(dest_height)*float(100))/float(self.height)) temp_width, temp_height = dest_width, dest_height if rule_height > rule_width: # TODO: # TODO: Testear caso en que ancho sea flotante. temp_width = (rule_height*self.width)/100 file_orig.scale(temp_width, dest_height) else: temp_height = (rule_width*self.height)/100 file_orig.scale(dest_width) if rule_width != rule_height: x_offset = str((temp_width-dest_width)/2) y_offset = str((temp_height-dest_height)/2) file_orig.crop(str(resolution)+'+'+x_offset+'+'+y_offset) wmark_file = settings.WATERMARK_FILE wmark = Image(wmark_file) if wmark.size().width() > dest_width and dest_width > 200: wmark_file = wmark_file[:wmark_file.rfind('.')] + '-small' + \ wmark_file[wmark_file.rfind('.'):] # FIXME: # FIXME: La marca de agua no se ve en sistemas que implementan # FIXME: barras de herramientas inferiores. Hay que colocarla unos # FIXME: 50px mas arriba. # file_orig.watermark(wmark_file) file_orig.comment(settings.COMMENT) file_orig.write(dest_path) return dest_path
def render(self, context): try: # If size is not an int, then it's a Variable, so try to resolve it. if not isinstance(self.size, int): self.size = int(self.size.resolve(context)) self.user = self.get_user(context) except Exception as e: print(e) return '' # just die... if self.size > _settings.DEFAULT_AVATAR_WIDTH: return '' # unacceptable profile = self.get_profile() if not profile: return '' # Avatar's heaven, where all the avatars go. avatars_root = path.join(_settings.AVATARS_DIR, slugify(self.user.username)) file_root, file_name, defaulting = self.get_file(profile) if defaulting: file_root = _settings.AVATARS_DIR if self.size_equals(): return self.as_url(path.join(file_root, file_name)) file_path = path.join(file_root, file_name) # I don't return the default because I have to resize it. if not defaulting: if path.exists(file_path) and self.size_equals(file_path): return self.as_url(file_path) else: if not profile.avatar: file_root = _settings.AVATARS_DIR file_path = path.join(file_root, _settings.DEFAULT_AVATAR) # Oops, I din't find it, let's try to generate it. if path.exists(file_path): orig_file = Image(file_path) dest_root = path.join(avatars_root, str(self.size)) try: makedirs(dest_root) except Exception as e: print(e) # Save the new path for later... dest_path = path.join(dest_root, file_name) else: # Did my best... return '' # fail silently orig_file.scale(self.size) if orig_file.write(dest_path): return self.as_url(dest_path) else: print('=== ERROR ===') return '' # damn! Close but no cigar...
file_path = path.join(file_root, _settings.DEFAULT_AVATAR) # Oops, I din't find it, let's try to generate it. if path.exists(file_path): orig_file = Image(file_path) dest_root = path.join(avatars_root, str(self.size)) try: makedirs(dest_root) except Exception, e: print e # Save the new path for later... dest_path = path.join(dest_root, file_name) else: # Did my best... return '' # fail silently orig_file.scale(self.size) if orig_file.write(dest_path): return self.as_url(dest_path) else: print '=== ERROR ===' return '' # damn! Close but no cigar... @register.tag('avatar') def Thumbnail(parser, token): bits = token.contents.split() username = None if len(bits) > 3: raise TemplateSyntaxError(u_(u"You have to provide only the size as \ an integer (both sides will be equal) and optionally, the \ username.")) elif len(bits) == 3:
file_path = path.join(file_root, _settings.DEFAULT_AVATAR) # Oops, I din't find it, let's try to generate it. if path.exists(file_path): orig_file = Image(file_path) dest_root = path.join(avatars_root, str(self.size)) try: makedirs(dest_root) except Exception, e: print e # Save the new path for later... dest_path = path.join(dest_root, file_name) else: # Did my best... return '' # fail silently orig_file.scale(self.size) if orig_file.write(dest_path): return self.as_url(dest_path) else: print '=== ERROR ===' return '' # damn! Close but no cigar... @register.tag('avatar') def Thumbnail(parser, token): bits = token.contents.split() username = None if len(bits) > 3: raise TemplateSyntaxError( u_(u"You have to provide only the size as \ an integer (both sides will be equal) and optionally, the \ username."))