From 00daabf17c8eced47a5394645ee9779b868be3a6 Mon Sep 17 00:00:00 2001 From: Krille Fear Date: Sun, 12 Dec 2021 12:03:58 +0100 Subject: [PATCH] fix: Better thumbnails This checks if the image is png or jpg and therefore doesnt remove transparancy. It also encodes thumbnails with higher resolution to make them less ugly. --- lib/pages/chat/send_file_dialog.dart | 2 +- lib/utils/resize_image.dart | 20 ++++++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/pages/chat/send_file_dialog.dart b/lib/pages/chat/send_file_dialog.dart index 58f1cb13..9d53f2f4 100644 --- a/lib/pages/chat/send_file_dialog.dart +++ b/lib/pages/chat/send_file_dialog.dart @@ -36,7 +36,7 @@ class _SendFileDialogState extends State { if (file is MatrixImageFile && !origImage && file.bytes.length > minSizeToCompress) { - file = await file.resizeImage(quality: 40, max: 1200); + file = await file.resizeImage(); } await widget.room.sendFileEventWithThumbnail(file); } diff --git a/lib/utils/resize_image.dart b/lib/utils/resize_image.dart index b048aa01..543884cb 100644 --- a/lib/utils/resize_image.dart +++ b/lib/utils/resize_image.dart @@ -10,8 +10,8 @@ import 'package:image/image.dart'; import 'package:matrix/matrix.dart'; extension ResizeImage on MatrixFile { - static const int max = 800; - static const int quality = 20; + static const int max = 1200; + static const int quality = 40; Future resizeImage({ bool calcBlurhash = true, @@ -23,7 +23,8 @@ extension ResizeImage on MatrixFile { : await compute<_ResizeBytesConfig, Uint8List>( resizeBytes, _ResizeBytesConfig( - this.bytes, + bytes: this.bytes, + mimeType: mimeType, max: max, quality: quality, )); @@ -55,17 +56,24 @@ Future resizeBytes(_ResizeBytesConfig config) async { : copyResize(image, height: config.max); } - return Uint8List.fromList(encodeJpg(image, quality: config.quality)); + const pngMimeType = 'image/png'; + final encoded = config.mimeType.toLowerCase() == pngMimeType + ? encodePng(image) + : encodeJpg(image, quality: config.quality); + + return Uint8List.fromList(encoded); } class _ResizeBytesConfig { final Uint8List bytes; final int max; final int quality; + final String mimeType; - const _ResizeBytesConfig( - this.bytes, { + const _ResizeBytesConfig({ + required this.bytes, this.max = ResizeImage.max, this.quality = ResizeImage.quality, + required this.mimeType, }); }