Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Thumbnail WebP images as WebP #14890

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/14890.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Thumbnail WebP images as WebP instead of JPEG, preserving transparency.
11 changes: 6 additions & 5 deletions docs/setup/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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"
```

Expand All @@ -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
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion synapse/config/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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!"
Expand Down
6 changes: 3 additions & 3 deletions synapse/media/thumbnailer.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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:
Expand Down
14 changes: 12 additions & 2 deletions synapse/rest/media/v1/__init__.py
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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"):
Expand All @@ -27,6 +28,15 @@
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'"
)