diff --git a/django/core/files/images.py b/django/core/files/images.py
index 55008b5..ab9a398 100644
a
|
b
|
class ImageFile(File):
|
23 | 23 | if not hasattr(self, '_dimensions_cache'): |
24 | 24 | close = self.closed |
25 | 25 | self.open() |
26 | | self._dimensions_cache = get_image_dimensions(self) |
27 | | if close: |
28 | | self.close() |
| 26 | self._dimensions_cache = get_image_dimensions(self, close=close) |
29 | 27 | return self._dimensions_cache |
30 | 28 | |
31 | | def get_image_dimensions(file_or_path): |
| 29 | def get_image_dimensions(file_or_path, close=False): |
32 | 30 | """Returns the (width, height) of an image, given an open file or a path.""" |
33 | 31 | # Try to import PIL in either of the two ways it can end up installed. |
34 | 32 | try: |
… |
… |
def get_image_dimensions(file_or_path):
|
37 | 35 | import ImageFile as PILImageFile |
38 | 36 | |
39 | 37 | p = PILImageFile.Parser() |
40 | | close = False |
41 | 38 | if hasattr(file_or_path, 'read'): |
42 | 39 | file = file_or_path |
| 40 | file_pos = file.tell() |
| 41 | file.seek(0) |
43 | 42 | else: |
44 | 43 | file = open(file_or_path, 'rb') |
45 | 44 | close = True |
… |
… |
def get_image_dimensions(file_or_path):
|
55 | 54 | finally: |
56 | 55 | if close: |
57 | 56 | file.close() |
| 57 | else: |
| 58 | file.seek(file_pos) |
diff --git a/tests/regressiontests/file_storage/tests.py b/tests/regressiontests/file_storage/tests.py
index 2c5f0f4..a1470f9 100644
a
|
b
|
if Image is not None:
|
230 | 230 | finally: |
231 | 231 | del images.open |
232 | 232 | self.assert_(FileWrapper._closed) |
| 233 | |
| 234 | class InconsistentGetImageDimensionsBug(TestCase): |
| 235 | """ |
| 236 | Test that get_image_dimensions() works properly after various calls using a file handler (#11158) |
| 237 | """ |
| 238 | def test_multiple_calls(self): |
| 239 | """ |
| 240 | Multiple calls of get_image_dimensions() should return the same size. |
| 241 | """ |
| 242 | from django.core.files.images import ImageFile |
| 243 | img_path = os.path.join(os.path.dirname(__file__), "test.png") |
| 244 | image = ImageFile(open(img_path)) |
| 245 | image_pil = Image.open(img_path) |
| 246 | size_1, size_2 = get_image_dimensions(image), get_image_dimensions(image) |
| 247 | self.assertEqual(image_pil.size, size_1) |
| 248 | self.assertEqual(size_1, size_2) |