getImageInfo method

Future<ImageInfoResult> getImageInfo(
  1. File file
)

Extracts metadata from an image file.

Implementation

Future<ImageInfoResult> getImageInfo(File file) async {
  try {
    final bytes = await file.readAsBytes();
    final image = img.decodeImage(bytes);

    if (image == null) {
      return ImageInfoResult.unsupported();
    }

    return ImageInfoResult.success(ImageInfo(
      width: image.width,
      height: image.height,
      format: image.format.name,
      hasAlpha: image.hasAlpha,
    ));
  } on FileSystemException catch (e) {
    return ImageInfoResult.error('Cannot read file: ${e.message}');
  } on img.ImageException catch (e) {
    return ImageInfoResult.error('Invalid image: $e');
  } catch (e) {
    return ImageInfoResult.error('Failed to analyze: $e');
  }
}