fix: Set image width and height
Uses the thumbnail generation of Matrix SDK.
This commit is contained in:
parent
fa6a7435d6
commit
c600c74e3e
7 changed files with 47 additions and 124 deletions
|
|
@ -1,9 +1,6 @@
|
|||
import 'dart:io';
|
||||
import 'dart:math' as math;
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import 'package:blurhash_dart/blurhash_dart.dart';
|
||||
import 'package:image/image.dart';
|
||||
import 'package:matrix/matrix.dart';
|
||||
|
|
@ -50,74 +47,15 @@ extension ResizeImage on MatrixFile {
|
|||
return MatrixImageFile(
|
||||
bytes: bytes,
|
||||
name: name,
|
||||
).resizeImage();
|
||||
);
|
||||
} catch (e, s) {
|
||||
SentryController.captureException(e, s);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<MatrixImageFile> 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(
|
||||
bytes: this.bytes,
|
||||
mimeType: mimeType,
|
||||
max: max,
|
||||
quality: quality,
|
||||
));
|
||||
final blurhash = calcBlurhash
|
||||
? await compute<Uint8List, BlurHash>(createBlurHash, bytes)
|
||||
: null;
|
||||
return MatrixImageFile(
|
||||
bytes: bytes,
|
||||
name: '${name.split('.').first}_thumbnail_$max.jpg',
|
||||
blurhash: blurhash?.hash,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<BlurHash> createBlurHash(Uint8List file) async {
|
||||
final image = decodeImage(file)!;
|
||||
return BlurHash.encode(image, numCompX: 4, numCompY: 3);
|
||||
}
|
||||
|
||||
Future<Uint8List> 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) > config.max) {
|
||||
// Use the larger side to resize.
|
||||
final useWidth = image.width >= image.height;
|
||||
image = useWidth
|
||||
? copyResize(image, width: config.max)
|
||||
: copyResize(image, height: config.max);
|
||||
}
|
||||
|
||||
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({
|
||||
required this.bytes,
|
||||
this.max = ResizeImage.max,
|
||||
this.quality = ResizeImage.quality,
|
||||
required this.mimeType,
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,35 +0,0 @@
|
|||
import 'package:matrix/matrix.dart';
|
||||
|
||||
import 'resize_image.dart';
|
||||
|
||||
extension RoomSendFileExtension on Room {
|
||||
Future<Uri> sendFileEventWithThumbnail(
|
||||
MatrixFile file, {
|
||||
String? txid,
|
||||
Event? inReplyTo,
|
||||
String? editEventId,
|
||||
bool? waitUntilSent,
|
||||
Map<String, dynamic>? extraContent,
|
||||
}) async {
|
||||
MatrixImageFile? thumbnail;
|
||||
if (file is MatrixImageFile) {
|
||||
thumbnail = await file.resizeImage();
|
||||
|
||||
if (thumbnail.size > file.size ~/ 2) {
|
||||
thumbnail = null;
|
||||
}
|
||||
} else if (file is MatrixVideoFile) {
|
||||
thumbnail = await file.getVideoThumbnail();
|
||||
}
|
||||
|
||||
return sendFileEvent(
|
||||
file,
|
||||
txid: txid,
|
||||
inReplyTo: inReplyTo,
|
||||
editEventId: editEventId,
|
||||
waitUntilSent: waitUntilSent ?? false,
|
||||
thumbnail: thumbnail,
|
||||
extraContent: extraContent,
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue