isValidImage static method

Future<bool> isValidImage(
  1. String base64String, {
  2. bool checkHeader = true,
  3. bool tryDecode = true,
})

Verify if the Base64 string is a valid image

Implementation

static Future<bool> isValidImage(
  String base64String, {
  bool checkHeader = true,
  bool tryDecode = true,
}) async {
  try {
    // 1. Basic format verification
    if (!_isValidBase64(base64String)) {
      return false;
    }

    // 2. Decoding into bytes
    final Uint8List bytes = _decodeBase64(base64String);

    // 3. Check file header
    if (checkHeader && !_hasValidImageHeader(bytes)) {
      return false;
    }

    // 4. Attempt to decode the image
    if (tryDecode) {
      return await _canDecodeAsImage(bytes);
    }

    return true;
  } catch (e) {
    print(e.toString());
    return false;
  }
}