compressImageHelper function
Compress image and return new file
Implementation
Future<File?> compressImageHelper(File file, {int maxSize = 10 * 1024 * 1024}) async {
final dir = await getTemporaryDirectory();
final targetPath = '${dir.path}/${basename(file.path)}_compressed.jpg';
var result = await FlutterImageCompress.compressAndGetFile(
file.absolute.path, // Input file
targetPath, // Output file
quality: 70, // Adjust quality (0 - 100)
);
final compressed = result != null ? File(result.path) : null;
final int size = compressed?.lengthSync() ?? 0;
debugPrint("📏 File Size: ${compressed?.lengthSync()} bytes < $maxSize");
if (result != null && size < maxSize) {
debugPrint("✅ Compressed Image Path: ${result.path}");
return compressed;
} else {
debugPrint("❌ Image selection failed");
return null;
}
}