isImage static method

Future<bool> isImage(
  1. MergeInput input
)

Determines whether the given file path corresponds to an image file.

This method uses the file's magic number (file signature) to detect if the file is a PNG, JPEG/JPG or HEIC image, regardless of its extension.

Currently supported image formats:

  • PNG
  • JPEG/JPG
  • HEIC

Parameters:

Returns: true if the file is a PNG or JPEG image, false otherwise (including when an error occurs during detection)

Implementation

static Future<bool> isImage(MergeInput input) async {
  final FileMagicNumberType fileType;
  switch (input) {
    case PathMergeInput(:final path):
      final bytes = await FileMagicNumber.getBytesFromPathOrBlob(path);
      fileType = FileMagicNumber.detectFileTypeFromBytes(bytes);
    case BytesMergeInput(:final bytes):
      fileType = FileMagicNumber.detectFileTypeFromBytes(bytes);
    case UrlMergeInput(:final url):
      final bytes = await getUrlBytes(url);
      fileType = FileMagicNumber.detectFileTypeFromBytes(bytes);
  }
  return fileType == FileMagicNumberType.png ||
      fileType == FileMagicNumberType.jpg ||
      fileType == FileMagicNumberType.heic;
}