diff --git a/changelog.d/14890.bugfix b/changelog.d/14890.bugfix new file mode 100644 index 0000000000..fe21277a9e --- /dev/null +++ b/changelog.d/14890.bugfix @@ -0,0 +1 @@ +Thumbnail WebP images as WebP instead of JPEG, preserving transparency. diff --git a/docs/setup/installation.md b/docs/setup/installation.md index 1f13864a8f..02aaa196b2 100644 --- a/docs/setup/installation.md +++ b/docs/setup/installation.md @@ -286,7 +286,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 @@ -295,7 +296,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 @@ -306,7 +307,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" ``` @@ -326,7 +327,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 @@ -346,7 +347,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 diff --git a/synapse/config/repository.py b/synapse/config/repository.py index 839c026d70..ed53735c01 100644 --- a/synapse/config/repository.py +++ b/synapse/config/repository.py @@ -47,7 +47,7 @@ THUMBNAIL_SIZE_YAML = """\ 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 d8979813b3..e5d6477a7f 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. @@ -40,7 +40,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: diff --git a/synapse/rest/media/v1/__init__.py b/synapse/rest/media/v1/__init__.py index d5b74cddf1..60a8de291f 100644 --- a/synapse/rest/media/v1/__init__.py +++ b/synapse/rest/media/v1/__init__.py @@ -1,4 +1,5 @@ # 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. @@ -12,7 +13,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"): @@ -27,6 +28,15 @@ if not check_codec("jpg"): 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'" + ) + + +# 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'" )