resizeImageBytes static method

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

Implementation

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