compressImage function
Implementation
Future<File?> compressImage(File imageFile, {int maxSize = 1}) async {
int maxSizeInBytes = maxSize * 1024 * 1024; // Convert MB to bytes
// check if image size is less than maxSize allowed before compression
if (imageFile.lengthSync() < maxSizeInBytes) {
return imageFile;
}
var compressedBytes = await FlutterImageCompress.compressWithFile(
imageFile.absolute.path,
minWidth: 800, // Adjust based on your requirements
minHeight: 800,
quality: 90, // Adjust quality (1-100)
);
if (compressedBytes == null) {
return null; // Compression failed
}
// Check size and iterate if needed
while (compressedBytes!.length > maxSizeInBytes) {
// Reduce quality further if necessary
compressedBytes = await FlutterImageCompress.compressWithList(compressedBytes, quality: 75);
}
// Convert to file: Uint8List to File
return File(imageFile.path).writeAsBytes(compressedBytes);
}