You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
fluffychat/lib/utils/shortcut_memory_icon.dart

37 lines
962 B
Dart

import 'dart:convert';
import 'dart:typed_data';
import 'package:image/image.dart';
import 'package:matrix/matrix.dart';
extension ShortcutMemoryIcon on Uint8List {
Future<String?> toShortcutMemoryIcon(
String roomId,
DatabaseApi? database,
) async {
final cacheKey = Uri.parse('im.fluffychat://shortcuts/$roomId');
final cachedFile = await database?.getFile(cacheKey);
if (cachedFile != null) return base64Encode(cachedFile);
final image = decodeImage(this);
if (image == null) return null;
final size = image.width < image.height ? image.width : image.height;
final x = (image.width - size) ~/ 2;
final y = (image.height - size) ~/ 2;
final croppedImage = copyCrop(
image,
x: x,
y: y,
width: size,
height: size,
);
final bytes = croppedImage.toUint8List();
await database?.storeFile(cacheKey, bytes, 0);
return base64Encode(croppedImage.toUint8List());
}
}