resizeImage static method

Future<Uint8List> resizeImage(
  1. Uint8List imageBytes, {
  2. int width = 600,
  3. int height = 400,
})

Implementation

static Future<Uint8List> resizeImage(Uint8List imageBytes,
    {int width = 600, int height = 400}) async {
  img.Image? image = img.decodeImage(imageBytes);
  if (image != null) {
    double aspectRatio = image.width / image.height;
    if (width / height > aspectRatio) {
      width = (height * aspectRatio).round();
    } else {
      height = (width / aspectRatio).round();
    }
    img.Image resized = img.copyResize(image, width: width, height: height);
    return Uint8List.fromList(img.encodeJpg(resized));
  } else {
    throw Exception('Failed to resize image');
  }
}