generateThumbnail method

Future<SDNImageFile?> generateThumbnail({
  1. int dimension = Client.defaultThumbnailSize,
  2. Future<SDNImageFileResizedResponse?> customImageResizer(
    1. SDNImageFileResizeArguments
    )?,
  3. @Deprecated('Use [nativeImplementations] instead') ComputeRunner? compute,
  4. NativeImplementations nativeImplementations = NativeImplementations.dummy,
})

Computes a thumbnail for the image. Also sets height and width on the original image if they were unset.

Implementation

Future<SDNImageFile?> generateThumbnail({
  int dimension = Client.defaultThumbnailSize,
  Future<SDNImageFileResizedResponse?> Function(SDNImageFileResizeArguments)?
      customImageResizer,
  @Deprecated('Use [nativeImplementations] instead') ComputeRunner? compute,
  NativeImplementations nativeImplementations = NativeImplementations.dummy,
}) async {
  if (compute != null) {
    nativeImplementations =
        NativeImplementationsIsolate.fromRunInBackground(compute);
  }
  final arguments = SDNImageFileResizeArguments(
    bytes: bytes,
    maxDimension: dimension,
    fileName: name,
    calcBlurhash: true,
  );
  final resizedData = customImageResizer != null
      ? await customImageResizer(arguments)
      : await nativeImplementations.shrinkImage(arguments);

  if (resizedData == null) {
    return null;
  }

  // we should take the opportunity to update the image dimension
  setImageSizeIfNull(
      width: resizedData.originalWidth, height: resizedData.originalHeight);

  // the thumbnail should rather return null than the enshrined image
  if (resizedData.width > dimension || resizedData.height > dimension) {
    return null;
  }

  final thumbnailFile = SDNImageFile(
    bytes: resizedData.bytes,
    name: name,
    mimeType: mimeType,
    width: resizedData.width,
    height: resizedData.height,
    blurhash: resizedData.blurhash,
  );
  return thumbnailFile;
}