rn-remove-image-bg 0.0.22 → 0.0.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Helper to normalize React Native URIs for Web consumption.
3
+ * Handles file://, asset://, and data: URIs.
4
+ */
5
+ export async function normalizeUri(uri: string): Promise<string> {
6
+ if (!uri) return '';
7
+
8
+ // Handle data URIs (return as is)
9
+ if (uri.startsWith('data:')) {
10
+ return uri;
11
+ }
12
+
13
+ // Handle http/https (return as is)
14
+ if (uri.startsWith('http')) {
15
+ return uri;
16
+ }
17
+
18
+ // Handle Expo asset:// URIs (convert to http relative path if needed, or pass through)
19
+ // In Expo Web, bundled assets are usually served from /assets/
20
+ if (uri.startsWith('asset://')) {
21
+ // NOTE: complex asset:// handling might require expo-asset,
22
+ // but often on web the uri passed is already resolved or a relative path.
23
+ // For now, we return it. If it fails, we might need 'expo-asset' module.
24
+ return uri.replace('asset://', '/assets/');
25
+ }
26
+
27
+ // Handle file:// URIs
28
+ // On web, file:// is blocked for security unless it's a blob url created by the app
29
+ if (uri.startsWith('file://')) {
30
+ // If it's a local file picked by user, it might be a blob: reference in disguise or invalid.
31
+ // We strip the protocol for relative paths check.
32
+ return uri;
33
+ }
34
+
35
+ // Relative paths
36
+ return uri;
37
+ }
38
+
39
+ /**
40
+ * Loads an image from a URI into an HTMLImageElement for processing.
41
+ * Handles CORS cross-origin issues.
42
+ */
43
+ export function loadImage(uri: string): Promise<HTMLImageElement> {
44
+ return new Promise((resolve, reject) => {
45
+ const img = new Image();
46
+ img.crossOrigin = 'Anonymous'; // Enable CORS
47
+ img.onload = () => resolve(img);
48
+ img.onerror = (e) => reject(new Error(`Failed to load image at ${uri}: ${String(e)}`));
49
+ img.src = uri;
50
+ });
51
+ }