rn-remove-image-bg 0.0.25 → 0.0.26
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
CHANGED
|
@@ -326,46 +326,51 @@ try {
|
|
|
326
326
|
|
|
327
327
|
### Web (React Native Web)
|
|
328
328
|
|
|
329
|
-
This package supports **React Native Web** via the `@imgly/background-removal` library.
|
|
329
|
+
This package supports **React Native Web** via the `@imgly/background-removal` library loaded from CDN.
|
|
330
330
|
|
|
331
|
-
#### Setup
|
|
331
|
+
#### Setup (Required)
|
|
332
332
|
|
|
333
|
-
|
|
333
|
+
Add this script tag to your `index.html` (before your app bundle):
|
|
334
|
+
|
|
335
|
+
```html
|
|
336
|
+
<script type="module">
|
|
337
|
+
import { removeBackground } from "https://cdn.jsdelivr.net/npm/@imgly/background-removal@1.7.0/+esm";
|
|
338
|
+
window.imglyRemoveBackground = removeBackground;
|
|
339
|
+
</script>
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
> **Why CDN?** The `@imgly/background-removal` library uses dynamic imports that Metro bundler doesn't support. Loading from CDN bypasses this limitation.
|
|
334
343
|
|
|
335
344
|
#### Usage
|
|
336
345
|
|
|
337
|
-
Use `removeBgImage` exactly like on native platforms
|
|
346
|
+
Use `removeBgImage` exactly like on native platforms:
|
|
338
347
|
|
|
339
348
|
```typescript
|
|
340
349
|
import { removeBgImage } from 'rn-remove-image-bg';
|
|
341
350
|
|
|
342
351
|
const result = await removeBgImage(uri, {
|
|
343
352
|
maxDimension: 1024,
|
|
344
|
-
onProgress: (p) => console.log(p),
|
|
345
|
-
// Web-specific: Path to custom hosted models (optional)
|
|
346
|
-
// If omitted, models are downloaded from @imgly CDN (~30MB)
|
|
347
|
-
publicPath: '/assets/models/'
|
|
353
|
+
onProgress: (p) => console.log(`Progress: ${p}%`),
|
|
348
354
|
});
|
|
349
355
|
```
|
|
350
356
|
|
|
351
|
-
####
|
|
352
|
-
|
|
353
|
-
By default, the library downloads AI models (~30MB) from the **Internet (CDN)** on the first run and caches them in the browser (IndexedDB). This keeps your app bundle small (~2MB).
|
|
357
|
+
#### First-Time Use
|
|
354
358
|
|
|
355
|
-
|
|
356
|
-
1. Copy the `.wasm` and `.onnx` files from `node_modules/@imgly/background-removal/dist/` to your web `public/models` folder.
|
|
357
|
-
2. Pass `publicPath: '/models/'` (or your chosen path) in the options.
|
|
359
|
+
On first use, the library downloads AI models (~30MB) from the CDN and caches them in IndexedDB. Subsequent uses are much faster.
|
|
358
360
|
|
|
359
361
|
#### Compatibility
|
|
360
362
|
|
|
361
|
-
- **Browsers**: Chrome, Edge, Firefox, Safari (
|
|
362
|
-
- **
|
|
363
|
-
- **CORS**:
|
|
363
|
+
- **Browsers**: Chrome, Edge, Firefox, Safari (with WebGPU or WebGL2)
|
|
364
|
+
- **Expo Web**: ✅ Works with Metro bundler
|
|
365
|
+
- **CORS**: Remote images must have `Access-Control-Allow-Origin: *` headers
|
|
364
366
|
|
|
365
367
|
#### Troubleshooting (Web)
|
|
366
368
|
|
|
367
|
-
|
|
368
|
-
|
|
369
|
+
| Error | Solution |
|
|
370
|
+
|-------|----------|
|
|
371
|
+
| `Background removal library not loaded` | Add the script tag to your HTML |
|
|
372
|
+
| `"Resource not found"` (404s) | Check internet connection (models download from CDN) |
|
|
373
|
+
| `"Tainted canvas"` / CORS errors | Ensure image server sends CORS headers |
|
|
369
374
|
|
|
370
375
|
---
|
|
371
376
|
|
|
@@ -1,8 +1,25 @@
|
|
|
1
1
|
import type { RemoveBgImageOptions } from './types';
|
|
2
|
+
type ImglyRemoveBackground = (image: string | Blob | ArrayBuffer, config?: {
|
|
3
|
+
publicPath?: string;
|
|
4
|
+
progress?: (key: string, current: number, total: number) => void;
|
|
5
|
+
debug?: boolean;
|
|
6
|
+
}) => Promise<Blob>;
|
|
7
|
+
declare global {
|
|
8
|
+
interface Window {
|
|
9
|
+
imglyRemoveBackground?: ImglyRemoveBackground;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
2
12
|
export declare const BackgroundRemover: {
|
|
13
|
+
/**
|
|
14
|
+
* Checks if the background removal library is loaded.
|
|
15
|
+
*/
|
|
16
|
+
isAvailable(): boolean;
|
|
3
17
|
/**
|
|
4
18
|
* Removes background from an image.
|
|
5
|
-
* Returns a
|
|
19
|
+
* Returns a Blob of the processed image (PNG).
|
|
20
|
+
*
|
|
21
|
+
* Requires @imgly/background-removal to be loaded via CDN script tag.
|
|
6
22
|
*/
|
|
7
23
|
remove(uri: string, options: RemoveBgImageOptions): Promise<Blob>;
|
|
8
24
|
};
|
|
25
|
+
export {};
|
|
@@ -1,13 +1,38 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { mapErrorToBackgroundRemovalError } from '../errors/WebErrorAdapter';
|
|
1
|
+
import { mapErrorToBackgroundRemovalError, BackgroundRemovalError } from '../errors/WebErrorAdapter';
|
|
3
2
|
import { normalizeUri } from '../utils/uriHelper';
|
|
3
|
+
/**
|
|
4
|
+
* Gets the @imgly removeBackground function from window.
|
|
5
|
+
* This must be loaded via CDN script tag before use.
|
|
6
|
+
*/
|
|
7
|
+
function getImglyRemoveBackground() {
|
|
8
|
+
if (typeof window === 'undefined') {
|
|
9
|
+
throw new BackgroundRemovalError('Background removal is only available in browser environment.', 'ENVIRONMENT_ERROR');
|
|
10
|
+
}
|
|
11
|
+
if (!window.imglyRemoveBackground) {
|
|
12
|
+
throw new BackgroundRemovalError('Background removal library not loaded. Please add the following script to your HTML:\n' +
|
|
13
|
+
'<script type="module">\n' +
|
|
14
|
+
' import { removeBackground } from "https://cdn.jsdelivr.net/npm/@imgly/background-removal@1.7.0/+esm";\n' +
|
|
15
|
+
' window.imglyRemoveBackground = removeBackground;\n' +
|
|
16
|
+
'</script>', 'LIBRARY_NOT_LOADED');
|
|
17
|
+
}
|
|
18
|
+
return window.imglyRemoveBackground;
|
|
19
|
+
}
|
|
4
20
|
export const BackgroundRemover = {
|
|
21
|
+
/**
|
|
22
|
+
* Checks if the background removal library is loaded.
|
|
23
|
+
*/
|
|
24
|
+
isAvailable() {
|
|
25
|
+
return typeof window !== 'undefined' && typeof window.imglyRemoveBackground === 'function';
|
|
26
|
+
},
|
|
5
27
|
/**
|
|
6
28
|
* Removes background from an image.
|
|
7
|
-
* Returns a
|
|
29
|
+
* Returns a Blob of the processed image (PNG).
|
|
30
|
+
*
|
|
31
|
+
* Requires @imgly/background-removal to be loaded via CDN script tag.
|
|
8
32
|
*/
|
|
9
33
|
async remove(uri, options) {
|
|
10
34
|
try {
|
|
35
|
+
const imglyRemove = getImglyRemoveBackground();
|
|
11
36
|
const normalizedUri = await normalizeUri(uri);
|
|
12
37
|
const config = {
|
|
13
38
|
// Pass publicPath if provided (for self-hosted assets)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rn-remove-image-bg",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.26",
|
|
4
4
|
"description": "rn-remove-image-bg",
|
|
5
5
|
"homepage": "https://github.com/a-eid/rn-remove-image-bg",
|
|
6
6
|
"main": "lib/index",
|
|
@@ -33,11 +33,10 @@
|
|
|
33
33
|
"app.plugin.js",
|
|
34
34
|
"nitro.json",
|
|
35
35
|
"*.podspec",
|
|
36
|
-
"README.md"
|
|
37
|
-
"scripts"
|
|
36
|
+
"README.md"
|
|
38
37
|
],
|
|
39
38
|
"scripts": {
|
|
40
|
-
"postinstall": "
|
|
39
|
+
"postinstall": "tsc || exit 0;",
|
|
41
40
|
"typecheck": "tsc --noEmit",
|
|
42
41
|
"clean": "rm -rf android/build node_modules/**/android/build lib",
|
|
43
42
|
"lint": "eslint \"**/*.{js,ts,tsx}\" --fix",
|
|
@@ -97,8 +96,6 @@
|
|
|
97
96
|
"lib/"
|
|
98
97
|
],
|
|
99
98
|
"dependencies": {
|
|
100
|
-
"@imgly/background-removal": "^1.7.0",
|
|
101
|
-
"buffer": "^6.0.3",
|
|
102
99
|
"thumbhash": "^0.1.1",
|
|
103
100
|
"upng-js": "^2.1.0"
|
|
104
101
|
}
|
|
@@ -1,18 +1,70 @@
|
|
|
1
|
-
import { removeBackground as imglyRemove, type Config } from '@imgly/background-removal';
|
|
2
1
|
import type { RemoveBgImageOptions } from './types';
|
|
3
|
-
import { mapErrorToBackgroundRemovalError } from '../errors/WebErrorAdapter';
|
|
2
|
+
import { mapErrorToBackgroundRemovalError, BackgroundRemovalError } from '../errors/WebErrorAdapter';
|
|
4
3
|
import { normalizeUri } from '../utils/uriHelper';
|
|
5
4
|
|
|
5
|
+
// Type for the CDN-loaded @imgly/background-removal function
|
|
6
|
+
type ImglyRemoveBackground = (
|
|
7
|
+
image: string | Blob | ArrayBuffer,
|
|
8
|
+
config?: {
|
|
9
|
+
publicPath?: string;
|
|
10
|
+
progress?: (key: string, current: number, total: number) => void;
|
|
11
|
+
debug?: boolean;
|
|
12
|
+
}
|
|
13
|
+
) => Promise<Blob>;
|
|
14
|
+
|
|
15
|
+
// Declare the global window property
|
|
16
|
+
declare global {
|
|
17
|
+
interface Window {
|
|
18
|
+
imglyRemoveBackground?: ImglyRemoveBackground;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Gets the @imgly removeBackground function from window.
|
|
24
|
+
* This must be loaded via CDN script tag before use.
|
|
25
|
+
*/
|
|
26
|
+
function getImglyRemoveBackground(): ImglyRemoveBackground {
|
|
27
|
+
if (typeof window === 'undefined') {
|
|
28
|
+
throw new BackgroundRemovalError(
|
|
29
|
+
'Background removal is only available in browser environment.',
|
|
30
|
+
'ENVIRONMENT_ERROR'
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (!window.imglyRemoveBackground) {
|
|
35
|
+
throw new BackgroundRemovalError(
|
|
36
|
+
'Background removal library not loaded. Please add the following script to your HTML:\n' +
|
|
37
|
+
'<script type="module">\n' +
|
|
38
|
+
' import { removeBackground } from "https://cdn.jsdelivr.net/npm/@imgly/background-removal@1.7.0/+esm";\n' +
|
|
39
|
+
' window.imglyRemoveBackground = removeBackground;\n' +
|
|
40
|
+
'</script>',
|
|
41
|
+
'LIBRARY_NOT_LOADED'
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return window.imglyRemoveBackground;
|
|
46
|
+
}
|
|
47
|
+
|
|
6
48
|
export const BackgroundRemover = {
|
|
49
|
+
/**
|
|
50
|
+
* Checks if the background removal library is loaded.
|
|
51
|
+
*/
|
|
52
|
+
isAvailable(): boolean {
|
|
53
|
+
return typeof window !== 'undefined' && typeof window.imglyRemoveBackground === 'function';
|
|
54
|
+
},
|
|
55
|
+
|
|
7
56
|
/**
|
|
8
57
|
* Removes background from an image.
|
|
9
|
-
* Returns a
|
|
58
|
+
* Returns a Blob of the processed image (PNG).
|
|
59
|
+
*
|
|
60
|
+
* Requires @imgly/background-removal to be loaded via CDN script tag.
|
|
10
61
|
*/
|
|
11
62
|
async remove(uri: string, options: RemoveBgImageOptions): Promise<Blob> {
|
|
12
63
|
try {
|
|
64
|
+
const imglyRemove = getImglyRemoveBackground();
|
|
13
65
|
const normalizedUri = await normalizeUri(uri);
|
|
14
66
|
|
|
15
|
-
const config
|
|
67
|
+
const config = {
|
|
16
68
|
// Pass publicPath if provided (for self-hosted assets)
|
|
17
69
|
publicPath: options.publicPath,
|
|
18
70
|
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
// Patch script for onnxruntime-web Metro bundler compatibility
|
|
3
|
-
// Removes webpack-specific comments that break Metro bundler
|
|
4
|
-
// Usage: node scripts/patch-onnxruntime-web.js
|
|
5
|
-
|
|
6
|
-
const fs = require('fs');
|
|
7
|
-
const path = require('path');
|
|
8
|
-
|
|
9
|
-
const filesToPatch = [
|
|
10
|
-
'node_modules/onnxruntime-web/dist/ort.bundle.min.mjs',
|
|
11
|
-
'node_modules/onnxruntime-web/dist/ort.webgpu.bundle.min.mjs',
|
|
12
|
-
'node_modules/onnxruntime-web/dist/ort.all.bundle.min.mjs',
|
|
13
|
-
];
|
|
14
|
-
|
|
15
|
-
let patchedCount = 0;
|
|
16
|
-
let skippedCount = 0;
|
|
17
|
-
|
|
18
|
-
for (const relativePath of filesToPatch) {
|
|
19
|
-
const absolutePath = path.join(__dirname, '..', relativePath);
|
|
20
|
-
|
|
21
|
-
if (!fs.existsSync(absolutePath)) {
|
|
22
|
-
// File doesn't exist, skip silently (not all bundles may be present)
|
|
23
|
-
continue;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
try {
|
|
27
|
-
const content = fs.readFileSync(absolutePath, 'utf-8');
|
|
28
|
-
|
|
29
|
-
// Remove webpack-specific comments that break Metro
|
|
30
|
-
// Pattern: /*webpackIgnore:true*/ or /*webpackChunkName:...*/
|
|
31
|
-
const patched = content
|
|
32
|
-
.replace(/\/\*webpackIgnore:true\*\//g, '')
|
|
33
|
-
.replace(/\/\*webpackChunkName:[^*]+\*\//g, '');
|
|
34
|
-
|
|
35
|
-
if (content === patched) {
|
|
36
|
-
console.log(`✅ ${relativePath} - already patched`);
|
|
37
|
-
skippedCount++;
|
|
38
|
-
} else {
|
|
39
|
-
fs.writeFileSync(absolutePath, patched);
|
|
40
|
-
console.log(`✅ ${relativePath} - patched successfully`);
|
|
41
|
-
patchedCount++;
|
|
42
|
-
}
|
|
43
|
-
} catch (error) {
|
|
44
|
-
console.error(`❌ ${relativePath} - failed to patch: ${error.message}`);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
if (patchedCount === 0 && skippedCount === 0) {
|
|
49
|
-
console.log('⚠️ No onnxruntime-web files found to patch (library may not be installed yet)');
|
|
50
|
-
} else {
|
|
51
|
-
console.log(`\n📦 Patch complete: ${patchedCount} patched, ${skippedCount} already patched`);
|
|
52
|
-
}
|