isImage static method

Future<bool> isImage(
  1. String filePath
)

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 or JPEG/JPG image, regardless of its extension.

Currently supported image formats:

  • PNG
  • JPEG/JPG

Parameters:

  • filePath: The absolute path to the file to check

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(String filePath) async {
  try {
    final fileType =
        await FileMagicNumber.detectFileTypeFromPathOrBlob(filePath);
    return fileType == FileMagicNumberType.png ||
        fileType == FileMagicNumberType.jpg;
  } catch (e) {
    return false;
  }
}