rn-remove-image-bg 0.0.23 → 0.0.25

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,38 @@
1
+ import type { CompressImageOptions } from '../core/types';
2
+ import { loadImage } from './uriHelper';
3
+
4
+ export async function compressImage(uri: string, options: CompressImageOptions): Promise<string> {
5
+ const img = await loadImage(uri);
6
+
7
+ let { width, height, quality = 0.8, format = 'jpeg' } = options;
8
+
9
+ // Default dimensions to original if not specified
10
+ if (!width) width = img.naturalWidth;
11
+ if (!height) height = img.naturalHeight;
12
+
13
+ // Calculate aspect ratio if one dimension is missing (though simpler to just use natural if both default)
14
+ const ratio = img.naturalWidth / img.naturalHeight;
15
+ if (options.width && !options.height) height = Math.round(width / ratio);
16
+ if (options.height && !options.width) width = Math.round(height * ratio);
17
+
18
+ // Normalize format
19
+ const mimeType = format === 'png' ? 'image/png' : format === 'webp' ? 'image/webp' : 'image/jpeg';
20
+
21
+ // Create canvas
22
+ const canvas = document.createElement('canvas');
23
+ canvas.width = width;
24
+ canvas.height = height;
25
+ const ctx = canvas.getContext('2d');
26
+
27
+ if (!ctx) {
28
+ throw new Error('Canvas 2D context not available');
29
+ }
30
+
31
+ // Draw image
32
+ ctx.drawImage(img, 0, 0, width, height);
33
+
34
+ // Export
35
+ // Note: quality (0-1) is ignored for PNG
36
+ const dataUrl = canvas.toDataURL(mimeType, quality);
37
+ return dataUrl;
38
+ }
@@ -0,0 +1,40 @@
1
+ import * as ThumbHash from 'thumbhash';
2
+ import { loadImage } from './uriHelper';
3
+
4
+ export async function generateThumbhash(uri: string): Promise<string> {
5
+ const img = await loadImage(uri);
6
+
7
+ // Thumbhash works best with images < 100x100
8
+ const maxSize = 100;
9
+ let width = img.naturalWidth;
10
+ let height = img.naturalHeight;
11
+
12
+ const scale = Math.min(maxSize / width, maxSize / height);
13
+ if (scale < 1) {
14
+ width = Math.round(width * scale);
15
+ height = Math.round(height * scale);
16
+ }
17
+
18
+ const canvas = document.createElement('canvas');
19
+ canvas.width = width;
20
+ canvas.height = height;
21
+ const ctx = canvas.getContext('2d');
22
+
23
+ if (!ctx) {
24
+ throw new Error('Canvas 2D context not available');
25
+ }
26
+
27
+ ctx.drawImage(img, 0, 0, width, height);
28
+
29
+ // Get RGBA data
30
+ const imageData = ctx.getImageData(0, 0, width, height);
31
+ const rgba = imageData.data;
32
+
33
+ // Generate binary hash
34
+ const hash = ThumbHash.rgbaToThumbHash(width, height, rgba);
35
+
36
+ // Convert to base64 using browser API
37
+ // hash is Uint8Array, spread into String.fromCharCode is safe for small thumbhashes (~30 bytes)
38
+ const binary = String.fromCharCode(...hash);
39
+ return window.btoa(binary);
40
+ }
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Converts a Blob to a Data URL string (base64)
3
+ */
4
+ export function blobToDataUrl(blob: Blob): Promise<string> {
5
+ return new Promise((resolve, reject) => {
6
+ const reader = new FileReader();
7
+ reader.onload = () => {
8
+ if (typeof reader.result === 'string') {
9
+ resolve(reader.result);
10
+ } else {
11
+ reject(new Error('Failed to convert blob to data URL'));
12
+ }
13
+ };
14
+ reader.onerror = () => reject(reader.error);
15
+ reader.readAsDataURL(blob);
16
+ });
17
+ }
@@ -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
+ }