Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reduce size image from bytes Uint8List

I am using the file_picker package to import images before uploading them to Firebase Storage, because it allows me to choose the formats what is not possible with image_picker.

I would like to be able to reduce the size of these images to reduce their weight. The flutter_image_compress package allows to compress images but it is not available for Flutter Web.

How can I reduce the size of an image imported in Flutter Web knowing that the format is Uint8List?

like image 495
Lab Avatar asked Sep 01 '25 17:09

Lab


1 Answers

if you know the image type you can convert it to jpeg and then compress it using the flutter image package.

import 'package:image/image.dart' as img;

Future<List<int>> _pngToJpg(Uint8List pngBytes) async {
  final pngImage = img.decodePng(pngBytes);
  final pngImageResized = img.copyResize(pngImage!, width: 800);
  return img.encodeJpg(pngImageResized, quality: 90);
}

you can use any other decoder depending on your image type. Expect a freeze in UI. You can use compute to minimize it.

like image 193
Elie Saad Avatar answered Sep 04 '25 07:09

Elie Saad