compressImage function
Compresses the image picture, passed as File
.
Implementation
Future<File> compressImage(File imageFile) async {
print("Getting tempdir"); //todo: remove debug print
final Directory tempDir = await getTemporaryDirectory();
print("Got tempdir: ${tempDir.path}");
final String path = tempDir.path;
int rand = new Random().nextInt(10000);
Im.Image image = Im.decodeImage(imageFile.readAsBytesSync());
int widthFinal = 0;
//algorithm to decide the image width
if (image.height > image.width) {
widthFinal = 1080;
if (image.width > 1080) {
image = Im.copyResize(image, widthFinal);
}
} else {
widthFinal = ((image.width * 1080) / image.height).round();
if (image.height > 1080) {
image = Im.copyResize(image, widthFinal);
}
}//todo: remove debug print
imageFile = new File('$path/img_$rand.jpg')
..writeAsBytesSync(Im.encodeJpg(image, quality: 50));
print("compressed");
return imageFile;
}