detect static method

ImageFormat detect(
  1. List<int> bytes
)

檢測一個二進位的檔案是否為圖片的各種格式 bytes - 二進位檔案

Implementation

static ImageFormat detect(List<int> bytes) {
  // 判斷檔案的開頭是否為PNG檔案
  if (_isPng(bytes)) {
    // 進一步判斷是否為apng
    if (_isApng(bytes)) {
      return ImageFormat.apng;
    } else {
      return ImageFormat.png;
    }
  }

  // 判斷檔案的開頭是否為JPG檔案
  if (_isJpg(bytes)) {
    return ImageFormat.jpg;
  }

  // 判斷檔案的開頭是否為GIF檔案
  if (_isGif(bytes)) {
    return ImageFormat.gif;
  }

  // 判斷檔案的開頭是否為BMP檔案
  if (_isBmp(bytes)) {
    return ImageFormat.bmp;
  }

  // 判斷檔案的開頭是否為WEBP檔案
  if (_isWebp(bytes)) {
    return ImageFormat.webp;
  }

  // 判斷檔案的開頭是否為TIFF檔案
  if (_isTiff(bytes)) {
    return ImageFormat.tiff;
  }

  // 判斷檔案的開頭是否為ICO檔案
  if (_isIco(bytes)) {
    return ImageFormat.ico;
  }

  // 未知的檔案格式
  return ImageFormat.unknown;
}