resizeImage static method

Future<MultipartFile?> resizeImage(
  1. File? imgSrc, {
  2. int w = 400,
  3. int h = 400,
  4. String fieldName = 'file',
})

Implementation

static Future<http.MultipartFile?> resizeImage(File? imgSrc,
    {int w = 400, int h = 400, String fieldName = 'file'}) async {
  try {
    if (imgSrc == null) return null;
    Img.Image? image_temp = Img.decodeImage(imgSrc.readAsBytesSync());
    if (image_temp == null) return null;
    Img.Image resized_img = Img.copyResize(image_temp, width: w);
    var multipartFile = new http.MultipartFile.fromBytes(
      '$fieldName',
      Img.encodeJpg(resized_img),
      filename: '${imgSrc.path}'.replaceAll('/', '_') + '.jpg',
    );
    return multipartFile;
  } catch (e) {
    print('${e}');
    return null;
  }
}