cropImageAndConvertTo method

Future<XFile?> cropImageAndConvertTo({
  1. ConvertType convertType = ConvertType.jpg,
  2. required double aspectRatio,
  3. double horizontalPadding = 0,
})

Implementation

Future<XFile?> cropImageAndConvertTo({ConvertType convertType = ConvertType.jpg, required double aspectRatio, double horizontalPadding = 0}) async {
  final byte = await readAsBytes();
  final Image? image = decodeImage(byte);
  if (image == null) {
    return null;
  }
  final width = image.width - horizontalPadding;
  final height = width / aspectRatio;
  final x = (horizontalPadding / 2).floor();
  final y = ((image.height / 2) - height / 2).floor();
  final imageCropResult = copyCrop(image, x: x, y: y, width: width.ceil(), height: height.ceil());
  if (convertType == ConvertType.jpg) {
    return XFile.fromData(encodeJpg(imageCropResult));
  } else {
    return XFile.fromData(encodePng(imageCropResult));
  }
}