From d2f795b87d94e3c38abf65d5558edede13bd0799 Mon Sep 17 00:00:00 2001 From: tastytea Date: Thu, 27 Oct 2022 18:44:06 +0200 Subject: [PATCH 1/5] thumbnail WebP as WebP Closes: https://github.com/matrix-org/synapse/issues/11840 Signed-off-by: Ronny (tastytea) Gutbrod --- changelog.d/14890.bugfix | 1 + synapse/config/repository.py | 6 +++++- synapse/media/thumbnailer.py | 6 +++--- 3 files changed, 9 insertions(+), 4 deletions(-) create mode 100644 changelog.d/14890.bugfix diff --git a/changelog.d/14890.bugfix b/changelog.d/14890.bugfix new file mode 100644 index 000000000000..fe21277a9ee5 --- /dev/null +++ b/changelog.d/14890.bugfix @@ -0,0 +1 @@ +Thumbnail WebP images as WebP instead of JPEG, preserving transparency. diff --git a/synapse/config/repository.py b/synapse/config/repository.py index ecb3edbe3ade..c728d6948e16 100644 --- a/synapse/config/repository.py +++ b/synapse/config/repository.py @@ -47,7 +47,7 @@ THUMBNAIL_SUPPORTED_MEDIA_FORMAT_MAP = { "image/jpeg": "jpeg", "image/jpg": "jpeg", - "image/webp": "jpeg", + "image/webp": "webp", # Thumbnails can only be jpeg or png. We choose png thumbnails for gif # because it can have transparency. "image/gif": "png", @@ -102,6 +102,10 @@ def parse_thumbnail_requirements( requirement.append( ThumbnailRequirement(width, height, method, "image/png") ) + elif thumbnail_format == "webp": + requirement.append( + ThumbnailRequirement(width, height, method, "image/webp") + ) else: raise Exception( "Unknown thumbnail mapping from %s to %s. This is a Synapse problem, please report!" diff --git a/synapse/media/thumbnailer.py b/synapse/media/thumbnailer.py index f909a4fb9a19..653f3feda44d 100644 --- a/synapse/media/thumbnailer.py +++ b/synapse/media/thumbnailer.py @@ -1,5 +1,5 @@ -# Copyright 2014-2016 OpenMarket Ltd -# Copyright 2020-2021 The Matrix.org Foundation C.I.C. +# Copyright 2014-2016,2023 OpenMarket Ltd +# Copyright 2020-2021,2023 The Matrix.org Foundation C.I.C. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -38,7 +38,7 @@ class ThumbnailError(Exception): class Thumbnailer: - FORMATS = {"image/jpeg": "JPEG", "image/png": "PNG"} + FORMATS = {"image/jpeg": "JPEG", "image/png": "PNG", "image/webp": "WEBP"} @staticmethod def set_limits(max_image_pixels: int) -> None: From ba126f6f1b86aa28878ba93503aa27dba571ed9c Mon Sep 17 00:00:00 2001 From: tastytea Date: Thu, 2 Feb 2023 20:58:29 +0100 Subject: [PATCH 2/5] update installation instructions to include webp Signed-off-by: tastytea --- docs/setup/installation.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/docs/setup/installation.md b/docs/setup/installation.md index d123e339edc9..6052b91b2dcf 100644 --- a/docs/setup/installation.md +++ b/docs/setup/installation.md @@ -278,7 +278,8 @@ Installing prerequisites on Ubuntu or Debian: ```sh sudo apt install build-essential python3-dev libffi-dev \ python3-pip python3-setuptools sqlite3 \ - libssl-dev virtualenv libjpeg-dev libxslt1-dev libicu-dev + libssl-dev virtualenv libjpeg-dev libxslt1-dev libicu-dev \ + libwebp-dev ``` ##### ArchLinux @@ -287,7 +288,7 @@ Installing prerequisites on ArchLinux: ```sh sudo pacman -S base-devel python python-pip \ - python-setuptools python-virtualenv sqlite3 icu + python-setuptools python-virtualenv sqlite3 icu libwebp ``` ##### CentOS/Fedora @@ -298,7 +299,7 @@ Installing prerequisites on CentOS or Fedora Linux: sudo dnf install libtiff-devel libjpeg-devel libzip-devel freetype-devel \ libwebp-devel libxml2-devel libxslt-devel libpq-devel \ python3-virtualenv libffi-devel openssl-devel python3-devel \ - libicu-devel + libicu-devel libwebp-devel sudo dnf groupinstall "Development Tools" ``` @@ -318,7 +319,7 @@ Please follow [the official instructions of PyICU](https://pypi.org/project/PyIC On ARM-based Macs you may also need to install libjpeg and libpq: ```sh - brew install jpeg libpq + brew install jpeg libpq webp ``` On macOS Catalina (10.15) you may need to explicitly install OpenSSL @@ -338,7 +339,7 @@ Installing prerequisites on openSUSE: sudo zypper in -t pattern devel_basis sudo zypper in python-pip python-setuptools sqlite3 python-virtualenv \ python-devel libffi-devel libopenssl-devel libjpeg62-devel \ - libicu-devel + libicu-devel libwebp-devel ``` ##### OpenBSD From d9ade4ba6e47d0f9144104cc701e8897895dd19d Mon Sep 17 00:00:00 2001 From: tastytea Date: Thu, 2 Feb 2023 21:13:26 +0100 Subject: [PATCH 3/5] add check for WebP support Signed-off-by: tastytea --- synapse/rest/media/v1/__init__.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/synapse/rest/media/v1/__init__.py b/synapse/rest/media/v1/__init__.py index d5b74cddf125..995ed89fadf8 100644 --- a/synapse/rest/media/v1/__init__.py +++ b/synapse/rest/media/v1/__init__.py @@ -1,4 +1,4 @@ -# Copyright 2014-2016 OpenMarket Ltd +# Copyright 2014-2016,2023 OpenMarket Ltd # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from PIL.features import check_codec +from PIL.features import check_codec, check_module # check for JPEG support. if not check_codec("jpg"): @@ -30,3 +30,12 @@ " 'sudo apt-get install libjpeg-dev' then 'pip uninstall pillow &&" " pip install pillow --user'" ) + + +# check for WebP support. +if not check_module("webp"): + raise Exception( + "FATAL: webp module not supported. Install pillow correctly! " + " 'sudo apt-get install libwebp-dev' then 'pip uninstall pillow &&" + " pip install pillow --user'" + ) From 334931af21ecabfe17e33cf1df6eff0b16ca9d38 Mon Sep 17 00:00:00 2001 From: tastytea Date: Thu, 2 Feb 2023 21:15:13 +0100 Subject: [PATCH 4/5] fix zlib-check error message Signed-off-by: tastytea --- synapse/rest/media/v1/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/rest/media/v1/__init__.py b/synapse/rest/media/v1/__init__.py index 995ed89fadf8..c9b3ed364ac0 100644 --- a/synapse/rest/media/v1/__init__.py +++ b/synapse/rest/media/v1/__init__.py @@ -27,7 +27,7 @@ if not check_codec("zlib"): raise Exception( "FATAL: zip codec not supported. Install pillow correctly! " - " 'sudo apt-get install libjpeg-dev' then 'pip uninstall pillow &&" + " 'sudo apt-get install zlib1g-dev' then 'pip uninstall pillow &&" " pip install pillow --user'" ) From 71c37cf902ea8c9e5d03d5108029aae7720818bc Mon Sep 17 00:00:00 2001 From: tastytea Date: Thu, 2 Feb 2023 22:23:19 +0100 Subject: [PATCH 5/5] fix copyright header Co-authored-by: Sean Quah <8349537+squahtx@users.noreply.github.com> Signed-off-by: tastytea --- synapse/rest/media/v1/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/synapse/rest/media/v1/__init__.py b/synapse/rest/media/v1/__init__.py index c9b3ed364ac0..60a8de291f18 100644 --- a/synapse/rest/media/v1/__init__.py +++ b/synapse/rest/media/v1/__init__.py @@ -1,4 +1,5 @@ -# Copyright 2014-2016,2023 OpenMarket Ltd +# Copyright 2014-2016 OpenMarket Ltd +# Copyright 2023 The Matrix.org Foundation C.I.C # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License.