generateThumbnail method

Future<MatrixImageFile?> generateThumbnail({
  1. int dimension = Client.defaultThumbnailSize,
  2. Future<MatrixImageFileResizedResponse?> customImageResizer(
    1. MatrixImageFileResizeArguments
    )?,
  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<MatrixImageFile?> generateThumbnail({
  int dimension = Client.defaultThumbnailSize,
  Future<MatrixImageFileResizedResponse?> Function(
          MatrixImageFileResizeArguments)?
      customImageResizer,
  @Deprecated('Use [nativeImplementations] instead') ComputeRunner? compute,
  NativeImplementations nativeImplementations = NativeImplementations.dummy,
}) async {
  if (compute != null) {
    nativeImplementations =
        NativeImplementationsIsolate.fromRunInBackground(compute);
  }
  final arguments = MatrixImageFileResizeArguments(
    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 = MatrixImageFile(
    bytes: resizedData.bytes,
    name: name,
    mimeType: mimeType,
    width: resizedData.width,
    height: resizedData.height,
    blurhash: resizedData.blurhash,
  );
  return thumbnailFile;
}