cropIfNeeded static method

Future<BackgroundCropResult> cropIfNeeded({
  1. required String fullPath,
  2. required int rotationDegrees,
  3. required int cropX1,
  4. required int cropY1,
  5. required int cropX2,
  6. required int cropY2,
  7. required bool forceCompression,
  8. required List<double>? eraserCoordinates,
})

Returns the file path of a crop operation.

Returns directly the original fullPath if no crop operation was needed. Returns the path of the cropped file if relevant. Returns null if the image (cropped or not) is too small. maxSize is the max pixel size for width or height (e.g. 2000)

Implementation

static Future<BackgroundCropResult> cropIfNeeded({
  required String fullPath,
  required int rotationDegrees,
  required int cropX1,
  required int cropY1,
  required int cropX2,
  required int cropY2,
  required bool forceCompression,
  required List<double>? eraserCoordinates,
}) async {
  final String croppedPath = await getCroppedPath(fullPath);

  final CustomPainter? overlayPainter =
      eraserCoordinates == null || eraserCoordinates.isEmpty
      ? null
      : EraserPainter(
          eraserModel: EraserModel(
            rotation: CropRotationExtension.fromDegrees(rotationDegrees)!,
            offsets: CropHelper.getOffsets(eraserCoordinates),
          ),
          cropRect: BackgroundTaskImage.getDownsizedRect(
            cropX1,
            cropY1,
            cropX2,
            cropY2,
          ),
        );
  final File fullFile = await BackgroundTaskUpload.getFile(fullPath);
  // TODO(monsieurtanuki): send full image width and height instead of recomputing them
  final ui.Image full = await loadUiImage(await fullFile.readAsBytes());
  final int fullWidth = full.width;
  final int fullHeight = full.height;
  full.dispose();
  if (!forceCompression) {
    if (cropX1 == 0 &&
        cropY1 == 0 &&
        cropX2 == _cropConversionFactor &&
        cropY2 == _cropConversionFactor &&
        rotationDegrees == 0) {
      if (!isPictureBigEnough(fullWidth, fullHeight)) {
        return BackgroundCropResult.error(
          'Picture too small ($fullWidth x $fullHeight)',
        );
      }
      // in that case, no need to crop
      if (overlayPainter == null) {
        return BackgroundCropResult.success(
          filePath: fullPath,
          fileSize: await fullFile.length(),
          width: fullWidth,
          height: fullHeight,
          message: 'Full image "as-is"',
        );
      }
    }
  }

  Size getCroppedSize() {
    final Rect cropRect = getDownsizedRect(cropX1, cropY1, cropX2, cropY2);
    switch (CropRotationExtension.fromDegrees(rotationDegrees)!) {
      case CropRotation.up:
      case CropRotation.down:
        return Size(cropRect.width * fullHeight, cropRect.height * fullWidth);
      case CropRotation.left:
      case CropRotation.right:
        return Size(cropRect.width * fullWidth, cropRect.height * fullHeight);
    }
  }

  final Size croppedSize = getCroppedSize();
  if (!isPictureBigEnough(croppedSize.width, croppedSize.height)) {
    return BackgroundCropResult.error(
      'Cropped picture too small (${croppedSize.width} x ${croppedSize.height})',
    );
  }
  final File croppedFile = await BackgroundTaskUpload.getFile(croppedPath);
  final ImageCropper imageCropper = ImageCropper(
    inputFile: fullFile,
    outputFile: croppedFile,
    crop: getDownsizedRect(cropX1, cropY1, cropX2, cropY2),
    rotation: CropRotationExtension.fromDegrees(rotationDegrees)!,
    overlayPainter: overlayPainter,
  );
  final (int, int) size = await imageCropper.saveCroppedJpeg();
  return BackgroundCropResult.success(
    filePath: croppedPath,
    fileSize: await croppedFile.length(),
    width: size.$1,
    height: size.$2,
    message: 'Cropped',
  );
}