Merge branch 'krille/images' into 'main'

chore: Adjust image and thumbnail compression

See merge request famedly/fluffychat!585
This commit is contained in:
Krille Fear 2021-11-26 15:37:53 +00:00
commit 99375a89e8
3 changed files with 38 additions and 22 deletions

View File

@ -29,11 +29,7 @@ class _SendFileDialogState extends State<SendFileDialog> {
Future<void> _send() async { Future<void> _send() async {
var file = widget.file; var file = widget.file;
if (file is MatrixImageFile && !origImage) { if (file is MatrixImageFile && !origImage) {
try { file = await file.resizeImage(quality: 40, max: 1200);
file = await file.resizeImage();
} catch (e) {
// couldn't resize
}
} }
await widget.room.sendFileEventWithThumbnail(file); await widget.room.sendFileEventWithThumbnail(file);
} }

View File

@ -10,11 +10,23 @@ import 'package:image/image.dart';
import 'package:matrix/matrix.dart'; import 'package:matrix/matrix.dart';
extension ResizeImage on MatrixFile { extension ResizeImage on MatrixFile {
static const int max = 1200; static const int max = 800;
static const int quality = 20; static const int quality = 20;
Future<MatrixImageFile> resizeImage({bool calcBlurhash = true}) async { Future<MatrixImageFile> resizeImage({
final bytes = await compute<Uint8List, Uint8List>(resizeBytes, this.bytes); 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 final blurhash = calcBlurhash
? await compute<Uint8List, BlurHash>(createBlurHash, bytes) ? await compute<Uint8List, BlurHash>(createBlurHash, bytes)
: null; : null;
@ -31,17 +43,29 @@ Future<BlurHash> createBlurHash(Uint8List file) async {
return BlurHash.encode(image, numCompX: 4, numCompY: 3); return BlurHash.encode(image, numCompX: 4, numCompY: 3);
} }
Future<Uint8List> resizeBytes(Uint8List file) async { Future<Uint8List> resizeBytes(_ResizeBytesConfig config) async {
var image = decodeImage(file)!; var image = decodeImage(config.bytes)!;
// Is file already smaller than max? Then just return. // 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. // Use the larger side to resize.
final useWidth = image.width >= image.height; final useWidth = image.width >= image.height;
image = useWidth image = useWidth
? copyResize(image, width: ResizeImage.max) ? copyResize(image, width: config.max)
: copyResize(image, height: ResizeImage.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,
});
} }

View File

@ -29,7 +29,6 @@ extension RoomSendFileExtension on Room {
bool waitUntilSent, bool waitUntilSent,
}) async { }) async {
MatrixFile thumbnail; MatrixFile thumbnail;
try {
if (file is MatrixImageFile) { if (file is MatrixImageFile) {
thumbnail = await file.resizeImage(); thumbnail = await file.resizeImage();
@ -37,9 +36,6 @@ extension RoomSendFileExtension on Room {
thumbnail = null; thumbnail = null;
} }
} }
} catch (e) {
// send no thumbnail
}
return sendFileEvent( return sendFileEvent(
file, file,