#NEWLINE# def database_get_image_dimensions( file , close = False , dimensions = None ) : #NEWLINE# #INDENT# "\n    Returns the (width, height) of an image, given ThumbnailFile.  Set\n    'close' to True to close the file at the end if it is initially in an open\n    state.\n\n    Will attempt to get the dimensions from the file itself if they aren't\n    in the db.\n    " #NEWLINE# storage_hash = utils . get_storage_hash ( file . storage ) #NEWLINE# dimensions = None #NEWLINE# dimensions_cache = None #NEWLINE# try : #NEWLINE# #INDENT# thumbnail = models . Thumbnail . objects . select_related ( 'dimensions' ) . get ( storage_hash = storage_hash , name = file . name ) #NEWLINE# #UNINDENT# except models . Thumbnail . DoesNotExist : #NEWLINE# #INDENT# thumbnail = None #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# dimensions_cache = thumbnail . dimensions #NEWLINE# #UNINDENT# except models . ThumbnailDimensions . DoesNotExist : #NEWLINE# #INDENT# dimensions_cache = None #NEWLINE# #UNINDENT# if dimensions_cache : #NEWLINE# #INDENT# return ( dimensions_cache . width , dimensions_cache . height ) #NEWLINE# #UNINDENT# #UNINDENT# dimensions = get_image_dimensions ( storage_hash , close = close ) #NEWLINE# if ( settings . THUMBNAIL_CACHE_DIMENSIONS and thumbnail ) : #NEWLINE# #INDENT# models . ThumbnailDimensions . objects . get_or_create ( thumbnail = thumbnail , defaults = { #NEWLINE# #INDENT# 'width' : dimensions [ 0 ] , #NEWLINE# 'height' : dimensions [ 1 ] , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# return dimensions