Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

upload: Fix resizing non-animated images.

Alex Vandiver96a5fa9d7825
diff --git a/zerver/lib/upload.py b/zerver/lib/upload.py
index d07cbef0ddd..cacae7f6d9e 100644
--- a/zerver/lib/upload.py
+++ b/zerver/lib/upload.py
@@ -169,7 +169,7 @@ def resize_gif(im: GifImageFile, size: int = DEFAULT_EMOJI_SIZE) -> bytes:
format="GIF",
append_images=frames[1:],
duration=duration_info,
- disposal=disposals,
+ disposal=disposals if len(frames) > 1 else disposals[0],
loop=loop,
)
return out.getvalue()
@@ -211,7 +211,10 @@ def resize_emoji(
if should_resize:
image_data = resize_gif(im, size)
- return image_data, True, still_image_data
+ if im.n_frames > 1:
+ return image_data, True, still_image_data
+ else:
+ return image_data, False, None
else:
# Note that this is essentially duplicated in the
# still_image code path, above.
diff --git a/zerver/tests/images/still_large_img.gif b/zerver/tests/images/still_large_img.gif
new file mode 100644
index 00000000000..de3bf7d3bea
Binary files /dev/null and b/zerver/tests/images/still_large_img.gif differ
diff --git a/zerver/tests/test_realm_emoji.py b/zerver/tests/test_realm_emoji.py
index a8f2cb82b8b..a56a5e8c6a1 100644
--- a/zerver/tests/test_realm_emoji.py
+++ b/zerver/tests/test_realm_emoji.py
@@ -271,6 +271,18 @@ class RealmEmojiTest(ZulipTestCase):
result = self.client_post("/json/realm/emoji/my_emoji", {"f1": fp1, "f2": fp2})
self.assert_json_error(result, "You must upload exactly one file.")
+ def test_emoji_upload_success(self) -> None:
+ self.login("iago")
+ with get_test_image_file("img.gif") as fp:
+ result = self.client_post("/json/realm/emoji/my_emoji", {"file": fp})
+ self.assert_json_success(result)
+
+ def test_emoji_upload_resize_success(self) -> None:
+ self.login("iago")
+ with get_test_image_file("still_large_img.gif") as fp:
+ result = self.client_post("/json/realm/emoji/my_emoji", {"file": fp})
+ self.assert_json_success(result)
+
def test_emoji_upload_file_size_error(self) -> None:
self.login("iago")
with get_test_image_file("img.png") as fp:
diff --git a/zerver/tests/test_upload.py b/zerver/tests/test_upload.py
index 1ffe6cf0ca4..1ee21ee14ff 100644
--- a/zerver/tests/test_upload.py
+++ b/zerver/tests/test_upload.py
@@ -1343,6 +1343,14 @@ class EmojiTest(UploadSerializeMixin, ZulipTestCase):
still_image = Image.open(io.BytesIO(still_img_data))
self.assertEqual((50, 50), still_image.size)
+ # Test a non-animated 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))
+ self.assertEqual((50, 50), im.size)
+ self.assertFalse(is_animated)
+ assert no_still_data is None
+
def tearDown(self) -> None:
destroy_uploads()
super().tearDown()