rn-remove-image-bg 0.0.22 → 0.0.23
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 +7 -3
- package/lib/ImageProcessing.web.js +18 -8
- package/package.json +1 -1
- package/src/ImageProcessing.web.ts +20 -9
package/README.md
CHANGED
|
@@ -326,12 +326,16 @@ try {
|
|
|
326
326
|
|
|
327
327
|
### Web
|
|
328
328
|
|
|
329
|
-
1. **Add the Script**: You must add the `@imgly/background-removal`
|
|
329
|
+
1. **Add the Script**: You must add the `@imgly/background-removal` setup to your web `index.html`.
|
|
330
|
+
Since the library is distributed as an ES Module, you need to import it and assign it to the window object:
|
|
330
331
|
```html
|
|
331
|
-
<script
|
|
332
|
+
<script type="module">
|
|
333
|
+
import { removeBackground } from "https://cdn.jsdelivr.net/npm/@imgly/background-removal@1.7.0/+esm";
|
|
334
|
+
window.imglyRemoveBackground = removeBackground;
|
|
335
|
+
</script>
|
|
332
336
|
```
|
|
333
337
|
|
|
334
|
-
2. **Usage**: The library will automatically detect the global `
|
|
338
|
+
2. **Usage**: The library will automatically detect the global `imglyRemoveBackground` function.
|
|
335
339
|
|
|
336
340
|
- **Technology**: [@imgly/background-removal](https://github.com/imgly/background-removal-js) running in a Web Worker (managed by the library).
|
|
337
341
|
- **Performance**: Zero UI blocking.
|
|
@@ -5,9 +5,22 @@
|
|
|
5
5
|
export async function removeBgImage(uri, options = {}) {
|
|
6
6
|
const { onProgress, debug = false } = options;
|
|
7
7
|
// Safety check
|
|
8
|
-
|
|
8
|
+
// @ts-ignore
|
|
9
|
+
if (typeof window !== 'undefined' && !window.imglyRemoveBackground) {
|
|
10
|
+
// Also check if user defined it as imglyRemoveBackground globally
|
|
11
|
+
// @ts-ignore
|
|
12
|
+
if (typeof imglyRemoveBackground !== 'undefined') {
|
|
13
|
+
// @ts-ignore
|
|
14
|
+
window.imglyRemoveBackground = imglyRemoveBackground;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
// @ts-ignore
|
|
18
|
+
if (typeof window.imglyRemoveBackground === 'undefined') {
|
|
9
19
|
throw new Error('[rn-remove-image-bg] Library not found. Please add the following script to your web index.html:\n' +
|
|
10
|
-
'<script
|
|
20
|
+
'<script type="module">\n' +
|
|
21
|
+
' import { removeBackground } from "https://cdn.jsdelivr.net/npm/@imgly/background-removal@1.7.0/+esm";\n' +
|
|
22
|
+
' window.imglyRemoveBackground = removeBackground;\n' +
|
|
23
|
+
'</script>');
|
|
11
24
|
}
|
|
12
25
|
if (debug)
|
|
13
26
|
console.log('[rmbg] Starting...');
|
|
@@ -16,7 +29,6 @@ export async function removeBgImage(uri, options = {}) {
|
|
|
16
29
|
const config = {
|
|
17
30
|
debug: debug,
|
|
18
31
|
// Point publicPath to CDN for assets (wasm/models)
|
|
19
|
-
// This is crucial for UMD build to find its dependencies on the CDN
|
|
20
32
|
publicPath: 'https://cdn.jsdelivr.net/npm/@imgly/background-removal@1.7.0/dist/',
|
|
21
33
|
progress: (_key, current, total) => {
|
|
22
34
|
if (onProgress && total > 0) {
|
|
@@ -24,7 +36,8 @@ export async function removeBgImage(uri, options = {}) {
|
|
|
24
36
|
}
|
|
25
37
|
}
|
|
26
38
|
};
|
|
27
|
-
|
|
39
|
+
// @ts-ignore
|
|
40
|
+
const blob = await window.imglyRemoveBackground(uri, config);
|
|
28
41
|
onProgress?.(100);
|
|
29
42
|
// Convert blob to DataURL
|
|
30
43
|
return new Promise((resolve, reject) => {
|
|
@@ -102,10 +115,7 @@ export async function generateThumbhash(imageUri, options = {}) {
|
|
|
102
115
|
}
|
|
103
116
|
}
|
|
104
117
|
export async function clearCache() {
|
|
105
|
-
//
|
|
106
|
-
if (typeof imglyBackgroundRemoval !== 'undefined' && imglyBackgroundRemoval.preload) {
|
|
107
|
-
// no explicit clear cache in uMD version usually
|
|
108
|
-
}
|
|
118
|
+
// Caching is handled internally by the library
|
|
109
119
|
}
|
|
110
120
|
export function getCacheSize() { return 0; }
|
|
111
121
|
export async function onLowMemory() {
|
package/package.json
CHANGED
|
@@ -14,7 +14,7 @@ export interface RemoveBgImageOptions {
|
|
|
14
14
|
|
|
15
15
|
// Check for global variable
|
|
16
16
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
17
|
-
declare const
|
|
17
|
+
declare const imglyRemoveBackground: any;
|
|
18
18
|
|
|
19
19
|
export async function removeBgImage(
|
|
20
20
|
uri: string,
|
|
@@ -23,10 +23,24 @@ export async function removeBgImage(
|
|
|
23
23
|
const { onProgress, debug = false } = options;
|
|
24
24
|
|
|
25
25
|
// Safety check
|
|
26
|
-
|
|
26
|
+
// @ts-ignore
|
|
27
|
+
if (typeof window !== 'undefined' && !window.imglyRemoveBackground) {
|
|
28
|
+
// Also check if user defined it as imglyRemoveBackground globally
|
|
29
|
+
// @ts-ignore
|
|
30
|
+
if (typeof imglyRemoveBackground !== 'undefined') {
|
|
31
|
+
// @ts-ignore
|
|
32
|
+
window.imglyRemoveBackground = imglyRemoveBackground;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// @ts-ignore
|
|
37
|
+
if (typeof window.imglyRemoveBackground === 'undefined') {
|
|
27
38
|
throw new Error(
|
|
28
39
|
'[rn-remove-image-bg] Library not found. Please add the following script to your web index.html:\n' +
|
|
29
|
-
'<script
|
|
40
|
+
'<script type="module">\n' +
|
|
41
|
+
' import { removeBackground } from "https://cdn.jsdelivr.net/npm/@imgly/background-removal@1.7.0/+esm";\n' +
|
|
42
|
+
' window.imglyRemoveBackground = removeBackground;\n' +
|
|
43
|
+
'</script>'
|
|
30
44
|
);
|
|
31
45
|
}
|
|
32
46
|
|
|
@@ -37,7 +51,6 @@ export async function removeBgImage(
|
|
|
37
51
|
const config = {
|
|
38
52
|
debug: debug,
|
|
39
53
|
// Point publicPath to CDN for assets (wasm/models)
|
|
40
|
-
// This is crucial for UMD build to find its dependencies on the CDN
|
|
41
54
|
publicPath: 'https://cdn.jsdelivr.net/npm/@imgly/background-removal@1.7.0/dist/',
|
|
42
55
|
progress: (_key: string, current: number, total: number) => {
|
|
43
56
|
if (onProgress && total > 0) {
|
|
@@ -46,7 +59,8 @@ export async function removeBgImage(
|
|
|
46
59
|
}
|
|
47
60
|
};
|
|
48
61
|
|
|
49
|
-
|
|
62
|
+
// @ts-ignore
|
|
63
|
+
const blob = await window.imglyRemoveBackground(uri, config);
|
|
50
64
|
onProgress?.(100);
|
|
51
65
|
|
|
52
66
|
// Convert blob to DataURL
|
|
@@ -160,10 +174,7 @@ export async function generateThumbhash(
|
|
|
160
174
|
}
|
|
161
175
|
|
|
162
176
|
export async function clearCache(): Promise<void> {
|
|
163
|
-
//
|
|
164
|
-
if (typeof imglyBackgroundRemoval !== 'undefined' && imglyBackgroundRemoval.preload) {
|
|
165
|
-
// no explicit clear cache in uMD version usually
|
|
166
|
-
}
|
|
177
|
+
// Caching is handled internally by the library
|
|
167
178
|
}
|
|
168
179
|
|
|
169
180
|
export function getCacheSize(): number { return 0; }
|