Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch for 1.2.1 #2

Open
wants to merge 1 commit into
base: origin-1.2.1-1733458787
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion django/core/files/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,18 @@ def get_image_dimensions(file_or_path):
file = open(file_or_path, 'rb')
close = True
try:
# Most of the time PIL only needs a small chunk to parse the image and
# get the dimensions, but with some TIFF files PIL needs to parse the
# whole file.
chunk_size = 1024
while 1:
data = file.read(1024)
data = file.read(chunk_size)
if not data:
break
p.feed(data)
if p.image:
return p.image.size
chunk_size = chunk_size*2
return None
finally:
if close:
Expand Down
57 changes: 57 additions & 0 deletions django/core/files/images.py.orig
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""
Utility functions for handling images.

Requires PIL, as you might imagine.
"""

from django.core.files import File

class ImageFile(File):
"""
A mixin for use alongside django.core.files.base.File, which provides
additional features for dealing with images.
"""
def _get_width(self):
return self._get_image_dimensions()[0]
width = property(_get_width)

def _get_height(self):
return self._get_image_dimensions()[1]
height = property(_get_height)

def _get_image_dimensions(self):
if not hasattr(self, '_dimensions_cache'):
close = self.closed
self.open()
self._dimensions_cache = get_image_dimensions(self)
if close:
self.close()
return self._dimensions_cache

def get_image_dimensions(file_or_path):
"""Returns the (width, height) of an image, given an open file or a path."""
# Try to import PIL in either of the two ways it can end up installed.
try:
from PIL import ImageFile as PILImageFile
except ImportError:
import ImageFile as PILImageFile

p = PILImageFile.Parser()
close = False
if hasattr(file_or_path, 'read'):
file = file_or_path
else:
file = open(file_or_path, 'rb')
close = True
try:
while 1:
data = file.read(1024)
if not data:
break
p.feed(data)
if p.image:
return p.image.size
return None
finally:
if close:
file.close()