From 08601b351c82942ea820ec8c2811b94c580e4bbb Mon Sep 17 00:00:00 2001 From: Krille Fear Date: Fri, 26 Nov 2021 16:12:52 +0100 Subject: [PATCH] chore: Adjust image and thumbnail compression --- .../new_private_chat/send_file_dialog.dart | 6 +-- lib/utils/resize_image.dart | 42 +++++++++++++++---- lib/utils/room_send_file_extension.dart | 12 ++---- 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/lib/pages/new_private_chat/send_file_dialog.dart b/lib/pages/new_private_chat/send_file_dialog.dart index f67ef7d2..37ea7476 100644 --- a/lib/pages/new_private_chat/send_file_dialog.dart +++ b/lib/pages/new_private_chat/send_file_dialog.dart @@ -29,11 +29,7 @@ class _SendFileDialogState extends State { Future _send() async { var file = widget.file; if (file is MatrixImageFile && !origImage) { - try { - file = await file.resizeImage(); - } catch (e) { - // couldn't resize - } + file = await file.resizeImage(quality: 40, max: 1200); } await widget.room.sendFileEventWithThumbnail(file); } diff --git a/lib/utils/resize_image.dart b/lib/utils/resize_image.dart index 917a448e..b048aa01 100644 --- a/lib/utils/resize_image.dart +++ b/lib/utils/resize_image.dart @@ -10,11 +10,23 @@ import 'package:image/image.dart'; import 'package:matrix/matrix.dart'; extension ResizeImage on MatrixFile { - static const int max = 1200; + static const int max = 800; static const int quality = 20; - Future resizeImage({bool calcBlurhash = true}) async { - final bytes = await compute(resizeBytes, this.bytes); + Future resizeImage({ + bool calcBlurhash = true, + int max = ResizeImage.max, + int quality = ResizeImage.quality, + }) async { + final bytes = mimeType == 'image/gif' + ? this.bytes + : await compute<_ResizeBytesConfig, Uint8List>( + resizeBytes, + _ResizeBytesConfig( + this.bytes, + max: max, + quality: quality, + )); final blurhash = calcBlurhash ? await compute(createBlurHash, bytes) : null; @@ -31,17 +43,29 @@ Future createBlurHash(Uint8List file) async { return BlurHash.encode(image, numCompX: 4, numCompY: 3); } -Future resizeBytes(Uint8List file) async { - var image = decodeImage(file)!; +Future resizeBytes(_ResizeBytesConfig config) async { + var image = decodeImage(config.bytes)!; // Is file already smaller than max? Then just return. - if (math.max(image.width, image.height) > ResizeImage.max) { + if (math.max(image.width, image.height) > config.max) { // Use the larger side to resize. final useWidth = image.width >= image.height; image = useWidth - ? copyResize(image, width: ResizeImage.max) - : copyResize(image, height: ResizeImage.max); + ? copyResize(image, width: config.max) + : copyResize(image, height: config.max); } - return Uint8List.fromList(encodeJpg(image, quality: ResizeImage.quality)); + return Uint8List.fromList(encodeJpg(image, quality: config.quality)); +} + +class _ResizeBytesConfig { + final Uint8List bytes; + final int max; + final int quality; + + const _ResizeBytesConfig( + this.bytes, { + this.max = ResizeImage.max, + this.quality = ResizeImage.quality, + }); } diff --git a/lib/utils/room_send_file_extension.dart b/lib/utils/room_send_file_extension.dart index c425843d..22f225e2 100644 --- a/lib/utils/room_send_file_extension.dart +++ b/lib/utils/room_send_file_extension.dart @@ -29,16 +29,12 @@ extension RoomSendFileExtension on Room { bool waitUntilSent, }) async { MatrixFile thumbnail; - try { - if (file is MatrixImageFile) { - thumbnail = await file.resizeImage(); + if (file is MatrixImageFile) { + thumbnail = await file.resizeImage(); - if (thumbnail.size > file.size ~/ 2) { - thumbnail = null; - } + if (thumbnail.size > file.size ~/ 2) { + thumbnail = null; } - } catch (e) { - // send no thumbnail } return sendFileEvent(