Code

Ticket #11158: 11158.diff

File 11158.diff, 1.3 KB (added by SmileyChris, 4 years ago)
Line 
1diff --git a/django/core/files/images.py b/django/core/files/images.py
2index 55008b5..ab9a398 100644
3--- a/django/core/files/images.py
4+++ b/django/core/files/images.py
5@@ -23,12 +23,10 @@ class ImageFile(File):
6         if not hasattr(self, '_dimensions_cache'):
7             close = self.closed
8             self.open()
9-            self._dimensions_cache = get_image_dimensions(self)
10-            if close:
11-                self.close()
12+            self._dimensions_cache = get_image_dimensions(self, close=close)
13         return self._dimensions_cache
14 
15-def get_image_dimensions(file_or_path):
16+def get_image_dimensions(file_or_path, close=False):
17     """Returns the (width, height) of an image, given an open file or a path."""
18     # Try to import PIL in either of the two ways it can end up installed.
19     try:
20@@ -37,9 +35,10 @@ def get_image_dimensions(file_or_path):
21         import ImageFile as PILImageFile
22         
23     p = PILImageFile.Parser()
24-    close = False
25     if hasattr(file_or_path, 'read'):
26         file = file_or_path
27+        file_pos = file.tell()
28+        file.seek(0)
29     else:
30         file = open(file_or_path, 'rb')
31         close = True
32@@ -55,3 +54,5 @@ def get_image_dimensions(file_or_path):
33     finally:
34         if close:
35             file.close()
36+        else:
37+            file.seek(file_pos)