From e6c14d14ee750b16eabca6b077c67e4fa1012bac Mon Sep 17 00:00:00 2001 From: Studying Date: Tue, 9 Nov 2021 23:10:53 +0100 Subject: [PATCH 1/4] Fix: Return a minimum width or height of 1 when trying to aspect a thumbnail. --- synapse/rest/media/v1/thumbnailer.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/synapse/rest/media/v1/thumbnailer.py b/synapse/rest/media/v1/thumbnailer.py index 46701a8b8364..5e17664b5b0e 100644 --- a/synapse/rest/media/v1/thumbnailer.py +++ b/synapse/rest/media/v1/thumbnailer.py @@ -101,8 +101,8 @@ def aspect(self, max_width: int, max_height: int) -> Tuple[int, int]: fits within the given rectangle:: (w_in / h_in) = (w_out / h_out) - w_out = min(w_max, h_max * (w_in / h_in)) - h_out = min(h_max, w_max * (h_in / w_in)) + w_out = max(min(w_max, h_max * (w_in / h_in)), 1) + h_out = max(min(h_max, w_max * (h_in / w_in)), 1) Args: max_width: The largest possible width. @@ -110,9 +110,9 @@ def aspect(self, max_width: int, max_height: int) -> Tuple[int, int]: """ if max_width * self.height < max_height * self.width: - return max_width, (max_width * self.height) // self.width + return max_width, max((max_width * self.height) // self.width, 1) else: - return (max_height * self.width) // self.height, max_height + return max((max_height * self.width) // self.height, 1), max_height def _resize(self, width: int, height: int) -> Image.Image: # 1-bit or 8-bit color palette images need converting to RGB From 142c2a7a6beb97ce380594bdc844861446193f14 Mon Sep 17 00:00:00 2001 From: Studying Date: Tue, 9 Nov 2021 23:10:53 +0100 Subject: [PATCH 2/4] Fix: Return a minimum width or height of 1 when trying to aspect a thumbnail. Signed-off-by: Jonas Zeunert --- synapse/rest/media/v1/thumbnailer.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/synapse/rest/media/v1/thumbnailer.py b/synapse/rest/media/v1/thumbnailer.py index 46701a8b8364..5e17664b5b0e 100644 --- a/synapse/rest/media/v1/thumbnailer.py +++ b/synapse/rest/media/v1/thumbnailer.py @@ -101,8 +101,8 @@ def aspect(self, max_width: int, max_height: int) -> Tuple[int, int]: fits within the given rectangle:: (w_in / h_in) = (w_out / h_out) - w_out = min(w_max, h_max * (w_in / h_in)) - h_out = min(h_max, w_max * (h_in / w_in)) + w_out = max(min(w_max, h_max * (w_in / h_in)), 1) + h_out = max(min(h_max, w_max * (h_in / w_in)), 1) Args: max_width: The largest possible width. @@ -110,9 +110,9 @@ def aspect(self, max_width: int, max_height: int) -> Tuple[int, int]: """ if max_width * self.height < max_height * self.width: - return max_width, (max_width * self.height) // self.width + return max_width, max((max_width * self.height) // self.width, 1) else: - return (max_height * self.width) // self.height, max_height + return max((max_height * self.width) // self.height, 1), max_height def _resize(self, width: int, height: int) -> Image.Image: # 1-bit or 8-bit color palette images need converting to RGB From 15bd06ee8be8a3df1d6f799207d0735178a88b7c Mon Sep 17 00:00:00 2001 From: Johannes Wendel Date: Tue, 9 Nov 2021 23:49:20 +0100 Subject: [PATCH 3/4] Adds changelog entry --- changelog.d/11288.bugfix | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/11288.bugfix diff --git a/changelog.d/11288.bugfix b/changelog.d/11288.bugfix new file mode 100644 index 000000000000..e5b8d9fe5afc --- /dev/null +++ b/changelog.d/11288.bugfix @@ -0,0 +1 @@ +Changed the aspect() function to return at least size 1 in width and height. From 57618ba610f48e9f0bb395bc5781c56e1d9939eb Mon Sep 17 00:00:00 2001 From: Neeeflix <35348173+Neeeflix@users.noreply.github.com> Date: Wed, 10 Nov 2021 15:19:41 +0100 Subject: [PATCH 4/4] Update changelog.d/11288.bugfix Co-authored-by: David Robertson --- changelog.d/11288.bugfix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.d/11288.bugfix b/changelog.d/11288.bugfix index e5b8d9fe5afc..d85b1779ba1e 100644 --- a/changelog.d/11288.bugfix +++ b/changelog.d/11288.bugfix @@ -1 +1 @@ -Changed the aspect() function to return at least size 1 in width and height. +Fix a long-standing bug where uploading extremely thin images (e.g. 1000x1) would fail. Contributed by @Neeeflix.