emoji: Support animated PNGs.

This commit is contained in:
Alex Vandiver
2022-02-17 00:07:58 +00:00
committed by Tim Abbott
parent fc793c10fa
commit 95892a5ed3
3 changed files with 53 additions and 40 deletions

View File

@@ -1303,32 +1303,33 @@ class EmojiTest(UploadSerializeMixin, ZulipTestCase):
with self.assertRaises(BadImageError):
resize_emoji(corrupted_img_data)
animated_large_img_data = read_test_image_file(f"animated_large_img.gif")
for img_format in ("gif", "png"):
animated_large_img_data = read_test_image_file(f"animated_large_img.{img_format}")
def test_resize(size: int = 50) -> None:
resized_img_data, is_animated, still_img_data = resize_emoji(
animated_large_img_data, size=50
)
im = Image.open(io.BytesIO(resized_img_data))
self.assertEqual((size, size), im.size)
self.assertTrue(is_animated)
assert still_img_data
still_image = Image.open(io.BytesIO(still_img_data))
self.assertEqual((50, 50), still_image.size)
def test_resize(size: int = 50) -> None:
resized_img_data, is_animated, still_img_data = resize_emoji(
animated_large_img_data, size=50
)
im = Image.open(io.BytesIO(resized_img_data))
self.assertEqual((size, size), im.size)
self.assertTrue(is_animated)
assert still_img_data
still_image = Image.open(io.BytesIO(still_img_data))
self.assertEqual((50, 50), still_image.size)
# Test an image larger than max is resized
with patch("zerver.lib.upload.MAX_EMOJI_GIF_SIZE", 128):
test_resize()
# Test an image larger than max is resized
with patch("zerver.lib.upload.MAX_EMOJI_GIF_SIZE", 128):
test_resize()
# Test an image file larger than max is resized
with patch("zerver.lib.upload.MAX_EMOJI_GIF_FILE_SIZE_BYTES", 3 * 1024 * 1024):
test_resize()
# Test an image file larger than max is resized
with patch("zerver.lib.upload.MAX_EMOJI_GIF_FILE_SIZE_BYTES", 3 * 1024 * 1024):
test_resize()
# Test an image smaller than max and smaller than file size max is not resized
with patch("zerver.lib.upload.MAX_EMOJI_GIF_SIZE", 512):
test_resize(size=256)
# Test an image smaller than max and smaller than file size max is not resized
with patch("zerver.lib.upload.MAX_EMOJI_GIF_SIZE", 512):
test_resize(size=256)
# Test a non-animated image which does need to be resized
# Test a non-animated GIF image which does need to be resized
still_large_img_data = read_test_image_file("still_large_img.gif")
resized_img_data, is_animated, no_still_data = resize_emoji(still_large_img_data, size=50)
im = Image.open(io.BytesIO(resized_img_data))
@@ -1336,6 +1337,14 @@ class EmojiTest(UploadSerializeMixin, ZulipTestCase):
self.assertFalse(is_animated)
assert no_still_data is None
# Test a non-animated and non-animatable image format which needs to be resized
still_large_img_data = read_test_image_file("img.jpg")
resized_img_data, is_animated, no_still_data = resize_emoji(still_large_img_data, size=50)
im = Image.open(io.BytesIO(resized_img_data))
self.assertEqual((50, 50), im.size)
self.assertFalse(is_animated)
assert no_still_data is None
def tearDown(self) -> None:
destroy_uploads()
super().tearDown()