rn-remove-image-bg 0.0.18 → 0.0.20
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.
- package/README.md +15 -13
- package/lib/ImageProcessing.web.js +13 -12
- package/package.json +1 -1
- package/src/ImageProcessing.web.ts +13 -12
package/README.md
CHANGED
|
@@ -325,15 +325,18 @@ try {
|
|
|
325
325
|
> **First-time Use**: On Android, the ML Kit model downloads automatically on first use. The library waits for the download to complete (up to ~15 seconds with retries) before processing. Subsequent calls are instant.
|
|
326
326
|
|
|
327
327
|
### Web
|
|
328
|
-
|
|
329
|
-
- **
|
|
330
|
-
- **
|
|
331
|
-
-
|
|
332
|
-
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
328
|
+
- **Technology**: [@imgly/background-removal](https://github.com/imgly/background-removal-js) running in a **Web Worker**.
|
|
329
|
+
- **Performance**: Zero UI blocking. All processing happens in a background thread.
|
|
330
|
+
- **Implementation**:
|
|
331
|
+
- Uses an **Inline Worker** architecture.
|
|
332
|
+
- Loads the library from CDN (`jsdelivr`) automatically on first use.
|
|
333
|
+
- **No bundler configuration required**. Works with Metro, Webpack, Vite, etc. out of the box.
|
|
334
|
+
- **Requirements**:
|
|
335
|
+
- **Internet Connection**: Required for the first run to download the library and assets (~10MB) from CDN.
|
|
336
|
+
- **CORS**: Images loaded from external URLs must be CORS-enabled (`Access-Control-Allow-Origin: *`).
|
|
337
|
+
- **Output**: Data URL (`data:image/png;base64,...`) or WEBP Blob.
|
|
338
|
+
|
|
339
|
+
> **Note on Local Development**: If you see CSP errors, ensure your development server allows loading scripts from `cdn.jsdelivr.net`.
|
|
337
340
|
|
|
338
341
|
---
|
|
339
342
|
|
|
@@ -416,10 +419,9 @@ If you don't need web support, you can tree-shake the `@imgly/background-removal
|
|
|
416
419
|
|
|
417
420
|
### Web Not Working?
|
|
418
421
|
|
|
419
|
-
1.
|
|
420
|
-
2.
|
|
421
|
-
3.
|
|
422
|
-
4. CORS may block model download - check network tab
|
|
422
|
+
1. **CSP Errors**: If you see "Content Security Policy" errors, ensure your app allows loading scripts/workers from `https://cdn.jsdelivr.net` and `blob:` URLs.
|
|
423
|
+
2. **CORS Errors**: If loading an image from a URL, the server *must* return `Access-Control-Allow-Origin: *`. If not, download the image to a Blob first or use a proxy.
|
|
424
|
+
3. **Data URL**: Remember that the web result is a `data:` URL. You can use it directly in `<Image source={{ uri: result }} />`.
|
|
423
425
|
|
|
424
426
|
### Native Module Not Found?
|
|
425
427
|
|
|
@@ -14,15 +14,15 @@ const WORKER_CODE = `
|
|
|
14
14
|
|
|
15
15
|
try {
|
|
16
16
|
if (type === 'init') {
|
|
17
|
-
if (!removeBackground) {
|
|
18
|
-
//
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
17
|
+
if (typeof self.imglyBackgroundRemoval === 'undefined' || !removeBackground) {
|
|
18
|
+
// Use importScripts (Classic Worker) - most robust for Blob URLs
|
|
19
|
+
importScripts('https://cdn.jsdelivr.net/npm/@imgly/background-removal@1.7.0/dist/imgly-background-removal.min.js');
|
|
20
|
+
|
|
21
|
+
if (!self.imglyBackgroundRemoval) {
|
|
22
|
+
throw new Error('imglyBackgroundRemoval not loaded via importScripts');
|
|
23
|
+
}
|
|
22
24
|
|
|
23
|
-
|
|
24
|
-
// Unless we want to preload the wasm.
|
|
25
|
-
// self.postMessage({ id, type: 'progress', payload: { progress: 0.1 } });
|
|
25
|
+
removeBackground = self.imglyBackgroundRemoval.removeBackground;
|
|
26
26
|
}
|
|
27
27
|
self.postMessage({ id, type: 'success', payload: true });
|
|
28
28
|
return;
|
|
@@ -32,15 +32,16 @@ const WORKER_CODE = `
|
|
|
32
32
|
const { uri, config } = payload;
|
|
33
33
|
|
|
34
34
|
if (!removeBackground) {
|
|
35
|
-
|
|
36
|
-
removeBackground =
|
|
35
|
+
importScripts('https://cdn.jsdelivr.net/npm/@imgly/background-removal@1.7.0/dist/imgly-background-removal.min.js');
|
|
36
|
+
removeBackground = self.imglyBackgroundRemoval.removeBackground;
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
// Run Inference
|
|
40
|
-
//
|
|
40
|
+
// CRITICAL: Must point publicPath to CDN so it finds WASM/Models
|
|
41
|
+
// otherwise it tries to load from blob: URL
|
|
41
42
|
const blob = await removeBackground(uri, {
|
|
43
|
+
publicPath: 'https://cdn.jsdelivr.net/npm/@imgly/background-removal@1.7.0/dist/',
|
|
42
44
|
progress: (key, current, total) => {
|
|
43
|
-
// Map progress roughly
|
|
44
45
|
const p = current / total;
|
|
45
46
|
self.postMessage({ id, type: 'progress', payload: { progress: p } });
|
|
46
47
|
},
|
package/package.json
CHANGED
|
@@ -24,15 +24,15 @@ const WORKER_CODE = `
|
|
|
24
24
|
|
|
25
25
|
try {
|
|
26
26
|
if (type === 'init') {
|
|
27
|
-
if (!removeBackground) {
|
|
28
|
-
//
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
27
|
+
if (typeof self.imglyBackgroundRemoval === 'undefined' || !removeBackground) {
|
|
28
|
+
// Use importScripts (Classic Worker) - most robust for Blob URLs
|
|
29
|
+
importScripts('https://cdn.jsdelivr.net/npm/@imgly/background-removal@1.7.0/dist/imgly-background-removal.min.js');
|
|
30
|
+
|
|
31
|
+
if (!self.imglyBackgroundRemoval) {
|
|
32
|
+
throw new Error('imglyBackgroundRemoval not loaded via importScripts');
|
|
33
|
+
}
|
|
32
34
|
|
|
33
|
-
|
|
34
|
-
// Unless we want to preload the wasm.
|
|
35
|
-
// self.postMessage({ id, type: 'progress', payload: { progress: 0.1 } });
|
|
35
|
+
removeBackground = self.imglyBackgroundRemoval.removeBackground;
|
|
36
36
|
}
|
|
37
37
|
self.postMessage({ id, type: 'success', payload: true });
|
|
38
38
|
return;
|
|
@@ -42,15 +42,16 @@ const WORKER_CODE = `
|
|
|
42
42
|
const { uri, config } = payload;
|
|
43
43
|
|
|
44
44
|
if (!removeBackground) {
|
|
45
|
-
|
|
46
|
-
removeBackground =
|
|
45
|
+
importScripts('https://cdn.jsdelivr.net/npm/@imgly/background-removal@1.7.0/dist/imgly-background-removal.min.js');
|
|
46
|
+
removeBackground = self.imglyBackgroundRemoval.removeBackground;
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
// Run Inference
|
|
50
|
-
//
|
|
50
|
+
// CRITICAL: Must point publicPath to CDN so it finds WASM/Models
|
|
51
|
+
// otherwise it tries to load from blob: URL
|
|
51
52
|
const blob = await removeBackground(uri, {
|
|
53
|
+
publicPath: 'https://cdn.jsdelivr.net/npm/@imgly/background-removal@1.7.0/dist/',
|
|
52
54
|
progress: (key, current, total) => {
|
|
53
|
-
// Map progress roughly
|
|
54
55
|
const p = current / total;
|
|
55
56
|
self.postMessage({ id, type: 'progress', payload: { progress: p } });
|
|
56
57
|
},
|