This repository has been archived by the owner on Nov 25, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 672
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mediaapi: Add thumbnail support (#132)
* vendor: Add bimg image processing library bimg is MIT licensed. It depends on the C library libvips which is LGPL v2.1+ licensed. libvips must be installed separately. * mediaapi: Add YAML config file support * mediaapi: Add thumbnail support * mediaapi: Add missing thumbnail files * travis: Add ppa and install libvips-dev * travis: Another ppa and install libvips-dev attempt * travis: Add sudo: required for sudo apt* usage * mediaapi/thumbnailer: Make comparison code more readable * mediaapi: Simplify logging of thumbnail properties * mediaapi/thumbnailer: Rename metrics to fitness Metrics is used in the context of monitoring with Prometheus so renaming to avoid confusion. * mediaapi/thumbnailer: Use math.Inf() for max aspect and size * mediaapi/thumbnailer: Limit number of parallel generators Fall back to selecting from already-/pre-generated thumbnails or serving the original. * mediaapi/thumbnailer: Split bimg code into separate file * vendor: Add github.com/nfnt/resize pure go image scaler * mediaapi/thumbnailer: Add nfnt/resize thumbnailer * travis: Don't install libvips-dev via ppa * mediaapi: Add notes to README about resizers * mediaapi: Elaborate on scaling libs in README
- Loading branch information
Showing
73 changed files
with
10,025 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# The name of the server. This is usually the domain name, e.g 'matrix.org', 'localhost'. | ||
server_name: "example.com" | ||
|
||
# The base path to where the media files will be stored. May be relative or absolute. | ||
base_path: /var/dendrite/media | ||
|
||
# The maximum file size in bytes that is allowed to be stored on this server. | ||
# Note: if max_file_size_bytes is set to 0, the size is unlimited. | ||
# Note: if max_file_size_bytes is not set, it will default to 10485760 (10MB) | ||
max_file_size_bytes: 10485760 | ||
|
||
# The postgres connection config for connecting to the database e.g a postgres:// URI | ||
database: "postgres://dendrite:itsasecret@localhost/mediaapi?sslmode=disable" | ||
|
||
# Whether to dynamically generate thumbnails on-the-fly if the requested resolution is not already generated | ||
# NOTE: This is a possible denial-of-service attack vector - use at your own risk | ||
dynamic_thumbnails: false | ||
|
||
# A list of thumbnail sizes to be pre-generated for downloaded remote / uploaded content | ||
# method is one of crop or scale. If omitted, it will default to scale. | ||
# crop scales to fill the requested dimensions and crops the excess. | ||
# scale scales to fit the requested dimensions and one dimension may be smaller than requested. | ||
thumbnail_sizes: | ||
- width: 32 | ||
height: 32 | ||
method: crop | ||
- width: 96 | ||
height: 96 | ||
method: crop | ||
- width: 320 | ||
height: 240 | ||
method: scale | ||
- width: 640 | ||
height: 480 | ||
method: scale | ||
- width: 800 | ||
height: 600 | ||
method: scale |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/github.com/matrix-org/dendrite/mediaapi/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Media API | ||
|
||
This server is responsible for serving `/media` requests as per: | ||
|
||
http://matrix.org/docs/spec/client_server/r0.2.0.html#id43 | ||
|
||
## Scaling libraries | ||
|
||
### nfnt/resize (default) | ||
|
||
Thumbnailing uses https://github.com/nfnt/resize by default which is a pure golang image scaling library relying on image codecs from the standard library. It is ISC-licensed. | ||
|
||
It is multi-threaded and uses Lanczos3 so produces sharp images. Using Lanczos3 all the way makes it slower than some other approaches like bimg. (~845ms in total for pre-generating 32x32-crop, 96x96-crop, 320x240-scale, 640x480-scale and 800x600-scale from a given JPEG image on a given machine.) | ||
|
||
See the sample below for image quality with nfnt/resize: | ||
|
||
![](nfnt-96x96-crop.jpg) | ||
|
||
### bimg (uses libvips C library) | ||
|
||
Alternatively one can use `gb build -tags bimg` to use bimg from https://github.com/h2non/bimg (MIT-licensed) which uses libvips from https://github.com/jcupitt/libvips (LGPL v2.1+ -licensed). libvips is a C library and must be installed/built separately. See the github page for details. Also note that libvips in turn has dependencies with a selection of FOSS licenses. | ||
|
||
bimg and libvips have significantly better performance than nfnt/resize but produce slightly less-sharp images. bimg uses a box filter for downscaling to within about 200% of the target scale and then uses Lanczos3 for the last bit. This is a much faster approach but comes at the expense of sharpness. (~295ms in total for pre-generating 32x32-crop, 96x96-crop, 320x240-scale, 640x480-scale and 800x600-scale from a given JPEG image on a given machine.) | ||
|
||
See the sample below for image quality with bimg: | ||
|
||
![](bimg-96x96-crop.jpg) |
Binary file added
BIN
+4.12 KB
src/github.com/matrix-org/dendrite/mediaapi/bimg-96x96-crop.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+4.78 KB
src/github.com/matrix-org/dendrite/mediaapi/nfnt-96x96-crop.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.