detect animated images (bug 550641)

This commit is contained in:
Andy McKay 2010-11-10 10:45:15 -08:00
Родитель e06830ed8e
Коммит 48712e3ce8
6 изменённых файлов: 40 добавлений и 0 удалений

Двоичные данные
apps/amo/tests/images/animated.gif Normal file

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 329 KiB

Двоичные данные
apps/amo/tests/images/animated.png Normal file

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 70 KiB

Двоичные данные
apps/amo/tests/images/non-animated.gif Normal file

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 56 KiB

Двоичные данные
apps/amo/tests/images/non-animated.png Normal file

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 37 KiB

Просмотреть файл

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
from datetime import datetime
import os
from django.conf import settings
from django.core import mail
@ -14,6 +15,7 @@ import test_utils
import amo
from amo import urlresolvers, utils, helpers
from amo.utils import is_animated_image
from versions.models import License
@ -269,3 +271,17 @@ class AbuseDisabledBase:
res = self.client.post(self.full_page)
eq_(res.status_code, 404)
def get_image_path(name):
return os.path.join(settings.ROOT, 'apps', 'amo', 'tests', 'images', name)
class TestAnimatedImages(test_utils.TestCase):
def test_animated_images(self):
assert is_animated_image(open(get_image_path('animated.png')))
assert not is_animated_image(open(get_image_path('non-animated.png')))
assert is_animated_image(open(get_image_path('animated.gif')))
assert not is_animated_image(open(get_image_path('non-animated.gif')))

Просмотреть файл

@ -254,6 +254,30 @@ def resize_image(src, dst, size):
os.remove(src)
def is_animated_image(photo, size=100000):
img = Image.open(photo)
if img.format == 'PNG':
photo.seek(0)
data = ''
while True:
chunk = photo.read(size)
if not chunk:
break
data += chunk
acTL, IDAT = data.find('acTL'), data.find('IDAT')
if acTL > -1 and acTL < IDAT:
return True
return False
elif img.format == 'GIF':
# See the PIL docs for how this works:
# http://www.pythonware.com/library/pil/handbook/introduction.htm
try:
img.seek(1)
except EOFError:
return False
return True
class MenuItem():
"""Refinement item with nestable children for use in menus."""
url, text, selected, children = ('', '', False, [])