Ticket #8817: 8817.diff

File 8817.diff, 1.2 KB (added by Bob Thomas, 15 years ago)

Open a new file handle to read image dimensions

  • django/core/files/images.py

     
    2828    """Returns the (width, height) of an image, given an open file or a path."""
    2929    from PIL import ImageFile as PILImageFile
    3030    p = PILImageFile.Parser()
    31     if hasattr(file_or_path, 'read'):
     31    needs_close = True
     32    # If we have a file name, prefer to open a new file to avoid modifying the
     33    # state of the file object we were given.
     34    if hasattr(file_or_path, 'name'):
     35        # If this is a FileField, use custom storage backend to re-open file
     36        if hasattr(file_or_path, 'storage'):
     37            file = file_or_path.storage.open(file_or_path.name, 'rb')
     38        else:
     39            file = open(file_or_path.name, 'rb')
     40    elif hasattr(file_or_path, 'read'):
    3241        file = file_or_path
     42        needs_close = False
    3343    else:
    3444        file = open(file_or_path, 'rb')
    3545    while 1:
     
    3848            break
    3949        p.feed(data)
    4050        if p.image:
     51            if needs_close:
     52                file.close()
    4153            return p.image.size
     54    if needs_close:
     55        file.close()
    4256    return None
Back to Top