xref: /expo/packages/expo-asset/src/ImageAssets.ts (revision 5bb0bf35)
1/* eslint-env browser */
2import { Platform } from 'expo-modules-core';
3
4import { getFilename } from './AssetUris';
5
6type ImageInfo = {
7  name: string;
8  width: number;
9  height: number;
10};
11
12export function isImageType(type: string): boolean {
13  return /^(jpeg|jpg|gif|png|bmp|webp|heic)$/i.test(type);
14}
15
16export function getImageInfoAsync(url: string): Promise<ImageInfo> {
17  if (!Platform.isDOMAvailable) {
18    return Promise.resolve({ name: getFilename(url), width: 0, height: 0 });
19  }
20  return new Promise((resolve, reject) => {
21    const img = new Image();
22    img.onerror = reject;
23    img.onload = () => {
24      resolve({
25        name: getFilename(url),
26        width: img.naturalWidth,
27        height: img.naturalHeight,
28      });
29    };
30    img.src = url;
31  });
32}
33