friendly-img-hash-js 1.0.3 → 1.1.0
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 +14 -9
- package/browser.js +26 -0
- package/index.js +47 -16
- package/package.json +12 -4
- package/patches/react-native-libwebpjs+1.0.0.patch +13 -0
- package/src/byte-resolver.js +18 -0
- package/src/url-resolver.js +27 -0
package/README.md
CHANGED
|
@@ -17,8 +17,11 @@ Browser example:
|
|
|
17
17
|
```
|
|
18
18
|
import { hash } from 'friendly-img-hash-js';
|
|
19
19
|
|
|
20
|
+
// Image formats support depends on your browser
|
|
21
|
+
// https://en.wikipedia.org/wiki/Comparison_of_web_browsers#Image_format_support
|
|
22
|
+
|
|
20
23
|
// Construct a hash!
|
|
21
|
-
const result = hash('https://my-sweet-image-url.png');
|
|
24
|
+
const result = await hash('https://my-sweet-image-url.png');
|
|
22
25
|
```
|
|
23
26
|
|
|
24
27
|
Nodejs Example:
|
|
@@ -26,8 +29,10 @@ Nodejs Example:
|
|
|
26
29
|
```
|
|
27
30
|
import { hash } from 'friendly-img-hash-js';
|
|
28
31
|
|
|
32
|
+
// Image formats supported are jpeg, png, webp, bitmap, tiff
|
|
33
|
+
|
|
29
34
|
// Construct a hash!
|
|
30
|
-
const result = hash('./some/file/on/disk.png');
|
|
35
|
+
const result = await hash('./some/file/on/disk.png');
|
|
31
36
|
|
|
32
37
|
```
|
|
33
38
|
|
|
@@ -48,7 +53,7 @@ import { hash, Convert } from 'friendly-img-hash-js';
|
|
|
48
53
|
|
|
49
54
|
// Example of how to specify that option
|
|
50
55
|
console.log(
|
|
51
|
-
hash(image {
|
|
56
|
+
await hash(image {
|
|
52
57
|
output: Convert.options.BigInt
|
|
53
58
|
})
|
|
54
59
|
);
|
|
@@ -71,7 +76,7 @@ import { hash, Hash } from 'friendly-img-hash-js';
|
|
|
71
76
|
|
|
72
77
|
// Example of how to specify that option
|
|
73
78
|
console.log(
|
|
74
|
-
hash(image, {
|
|
79
|
+
await hash(image, {
|
|
75
80
|
algorithm: Hash.options.Gradient
|
|
76
81
|
})
|
|
77
82
|
);
|
|
@@ -81,7 +86,7 @@ console.log(
|
|
|
81
86
|
Enable Discrete Cosine Transform (DCT)
|
|
82
87
|
|
|
83
88
|
```
|
|
84
|
-
import { hash,
|
|
89
|
+
import { hash, Dct } from 'friendly-img-hash-js';
|
|
85
90
|
|
|
86
91
|
const imageBytes = // get your image bytes following the examples above.
|
|
87
92
|
|
|
@@ -91,7 +96,7 @@ const imageBytes = // get your image bytes following the examples above.
|
|
|
91
96
|
|
|
92
97
|
// Example of how to specify that option
|
|
93
98
|
console.log(
|
|
94
|
-
hash(image, {
|
|
99
|
+
await hash(image, {
|
|
95
100
|
dct: {
|
|
96
101
|
algorithm: Dct.options.Type2
|
|
97
102
|
}
|
|
@@ -106,7 +111,7 @@ If you want to write your own algorithm, you can for any option. Here is an exam
|
|
|
106
111
|
import { hash } from 'friendly-img-hash-js';
|
|
107
112
|
|
|
108
113
|
console.log(
|
|
109
|
-
hash(image, {
|
|
114
|
+
await hash(image, {
|
|
110
115
|
// Dimension is required because of custom algorithm
|
|
111
116
|
dimension: (width, height) => ({ width, height }),
|
|
112
117
|
algorithm: (bytes) => {
|
|
@@ -129,8 +134,8 @@ import leven from 'leven';
|
|
|
129
134
|
|
|
130
135
|
const config = { output: Convert.options.BigInt };
|
|
131
136
|
|
|
132
|
-
const result1 = hash(image1, config);
|
|
133
|
-
const result2 = hash(image2, config);
|
|
137
|
+
const result1 = await hash(image1, config);
|
|
138
|
+
const result2 = await hash(image2, config);
|
|
134
139
|
|
|
135
140
|
if (leven(result1.toString(), result2.toString()) <= 12) {
|
|
136
141
|
console.log ('Images are very similar!');
|
package/browser.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { hash as coreHash } from 'img-hash-js-core';
|
|
2
|
+
|
|
3
|
+
import { reduceToRgb } from './src/byte-resolver';
|
|
4
|
+
|
|
5
|
+
export {
|
|
6
|
+
Grayscale, Resize, Dct, Hash, Convert,
|
|
7
|
+
} from 'img-hash-js-core';
|
|
8
|
+
|
|
9
|
+
// Hash an image!
|
|
10
|
+
// Input is a string to a url, local file, etc of an image
|
|
11
|
+
// See defaultConfig for options.
|
|
12
|
+
export const hash = async (img, userConfig) => {
|
|
13
|
+
const image = await loadBytesFromBrowser(img);
|
|
14
|
+
const bytes = reduceToRgb(image.data, image.width, image.height);
|
|
15
|
+
return coreHash(bytes, image.width, image.height, userConfig);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const loadBytesFromBrowser = async (img) => {
|
|
19
|
+
const canvas = document.createElement('canvas');
|
|
20
|
+
const context = canvas.getContext('2d');
|
|
21
|
+
const image = await new Promise((r) => { const i = new Image(); i.onload = (() => r(i)); i.src = img; });
|
|
22
|
+
canvas.height = image.naturalHeight;
|
|
23
|
+
canvas.width = image.naturalWidth;
|
|
24
|
+
context.drawImage(image, 0, 0, image.naturalWidth, image.naturalHeight);
|
|
25
|
+
return { data: context.getImageData(0, 0, canvas.width, canvas.height), width: canvas.width, height: canvas.height };
|
|
26
|
+
};
|
package/index.js
CHANGED
|
@@ -1,34 +1,65 @@
|
|
|
1
1
|
import { hash as coreHash } from 'img-hash-js-core';
|
|
2
|
-
import
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import imageType from 'image-type';
|
|
4
|
+
import { PNG } from 'pngjs';
|
|
5
|
+
import jpeg from 'jpeg-js';
|
|
6
|
+
import bmp from 'bmp-js';
|
|
7
|
+
import UTIF from 'utif2';
|
|
8
|
+
import { WebPDecoder, WebPRiffParser } from 'react-native-libwebpjs';
|
|
9
|
+
|
|
10
|
+
import { getUrlData, isUrl } from './src/url-resolver.js';
|
|
11
|
+
import { reduceToRgb } from './src/byte-resolver.js';
|
|
3
12
|
|
|
4
13
|
export {
|
|
5
14
|
Grayscale, Resize, Dct, Hash, Convert,
|
|
6
15
|
} from 'img-hash-js-core';
|
|
7
16
|
|
|
17
|
+
const extToResolver = {
|
|
18
|
+
png: PNG.sync.read,
|
|
19
|
+
jpg: jpeg.decode,
|
|
20
|
+
bmp: bmp.decode,
|
|
21
|
+
tif: (raw) => {
|
|
22
|
+
const ifds = UTIF.decode(raw);
|
|
23
|
+
UTIF.decodeImage(raw, ifds[0]);
|
|
24
|
+
const rgba = UTIF.toRGBA8(ifds[0]); // Uint8Array with RGBA pixels
|
|
25
|
+
return { data: rgba, width: ifds[0].width, height: ifds[0].height };
|
|
26
|
+
},
|
|
27
|
+
webp: (raw) => {
|
|
28
|
+
const { frames } = WebPRiffParser(raw, 0);
|
|
29
|
+
|
|
30
|
+
const decoder = new WebPDecoder();
|
|
31
|
+
const tmp = { w: [], h: [] };
|
|
32
|
+
const data = decoder.WebPDecodeRGBA(raw, frames[0].src_off, frames[0].src_size, tmp.w, tmp.h);
|
|
33
|
+
|
|
34
|
+
return { data, width: tmp.w, height: tmp.h };
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
|
|
8
38
|
// Hash an image!
|
|
9
39
|
// Input is a string to a url, local file, etc of an image
|
|
10
40
|
// See defaultConfig for options.
|
|
11
41
|
export const hash = async (img, userConfig) => {
|
|
12
|
-
const image = await
|
|
13
|
-
const bytes = reduceToRgb(image.
|
|
14
|
-
return coreHash(bytes, image.
|
|
42
|
+
const image = await loadBytesFromNode(img);
|
|
43
|
+
const bytes = reduceToRgb(image.data, image.width, image.height);
|
|
44
|
+
return coreHash(bytes, image.width, image.height, userConfig);
|
|
15
45
|
};
|
|
16
46
|
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
return data;
|
|
21
|
-
}
|
|
47
|
+
const loadBytesFromNode = async (img) => {
|
|
48
|
+
const rawFileData = await readRawFileData(img);
|
|
49
|
+
const format = await imageType(rawFileData);
|
|
22
50
|
|
|
23
|
-
|
|
24
|
-
if (
|
|
25
|
-
|
|
51
|
+
const resolver = extToResolver[format.ext];
|
|
52
|
+
if (!resolver) {
|
|
53
|
+
throw new Error(`Unknown image format ${format.ext}, try a png instead.`);
|
|
26
54
|
}
|
|
27
55
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
56
|
+
return resolver.call({}, rawFileData);
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const readRawFileData = async (s) => {
|
|
60
|
+
if (isUrl(s)) {
|
|
61
|
+
return getUrlData(s);
|
|
31
62
|
}
|
|
32
63
|
|
|
33
|
-
|
|
64
|
+
return fs.readFileSync(s);
|
|
34
65
|
};
|
package/package.json
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "friendly-img-hash-js",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "User friendly algorithms for generating image hashes.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"test": "mocha index.test.js",
|
|
9
9
|
"lint": "eslint src/** index.js index.test.js --no-error-on-unmatched-pattern",
|
|
10
|
-
"lint:fix": "eslint src/** index.js index.test.js --fix"
|
|
10
|
+
"lint:fix": "eslint src/** index.js index.test.js --fix",
|
|
11
|
+
"postinstall": "patch-package"
|
|
11
12
|
},
|
|
12
13
|
"repository": {
|
|
13
14
|
"type": "git",
|
|
@@ -37,7 +38,14 @@
|
|
|
37
38
|
"mocha": "^10.2.0"
|
|
38
39
|
},
|
|
39
40
|
"dependencies": {
|
|
41
|
+
"bmp-js": "^0.1.0",
|
|
42
|
+
"image-type": "^5.2.0",
|
|
40
43
|
"img-hash-js-core": "^1.0.7",
|
|
41
|
-
"
|
|
42
|
-
|
|
44
|
+
"jpeg-js": "^0.4.4",
|
|
45
|
+
"patch-package": "^8.0.0",
|
|
46
|
+
"pngjs": "^7.0.0",
|
|
47
|
+
"react-native-libwebpjs": "^1.0.0",
|
|
48
|
+
"utif2": "^4.1.0"
|
|
49
|
+
},
|
|
50
|
+
"browser": "browser.js"
|
|
43
51
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
diff --git a/node_modules/react-native-libwebpjs/package.json b/node_modules/react-native-libwebpjs/package.json
|
|
2
|
+
old mode 100644
|
|
3
|
+
new mode 100755
|
|
4
|
+
index 2bb6d59..f7cf399
|
|
5
|
+
--- a/node_modules/react-native-libwebpjs/package.json
|
|
6
|
+
+++ b/node_modules/react-native-libwebpjs/package.json
|
|
7
|
+
@@ -1,5 +1,6 @@
|
|
8
|
+
{
|
|
9
|
+
"name": "react-native-libwebpjs",
|
|
10
|
+
+ "type": "module",
|
|
11
|
+
"version": "1.0.0",
|
|
12
|
+
"main": "libwebp.js",
|
|
13
|
+
"description": "A forked libwebpjs-v0.6.0 for React Native"
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export const reduceToRgb = (data, width, height) => {
|
|
2
|
+
// already rgb
|
|
3
|
+
if (data.length === (width * height * 3)) {
|
|
4
|
+
return data;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
// rgba
|
|
8
|
+
if (data.length === (width * height * 4)) {
|
|
9
|
+
return data.filter((_, i) => (i + 1) % 4);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// l8
|
|
13
|
+
if (data.length === (width * height)) {
|
|
14
|
+
return new Array(width * height * 3).fill(0).map((_, i) => data[Math.floor(i / 3)]);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
throw new Error(`Unknown data length, expected ${data.length} to be a factor of width * height (${width} * ${height}).`);
|
|
18
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { URL } from 'url';
|
|
2
|
+
import https from 'https';
|
|
3
|
+
|
|
4
|
+
export const isUrl = (s) => {
|
|
5
|
+
try {
|
|
6
|
+
// eslint-disable-next-line no-new
|
|
7
|
+
new URL(s);
|
|
8
|
+
return true;
|
|
9
|
+
} catch (err) {
|
|
10
|
+
// Nope.
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return false;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export const getUrlData = async (s) => {
|
|
17
|
+
const options = new URL(s);
|
|
18
|
+
const chunks = [];
|
|
19
|
+
return new Promise((r) => {
|
|
20
|
+
https.request(options, (res) => {
|
|
21
|
+
res.on('data', (chunk) => {
|
|
22
|
+
chunks.push(chunk);
|
|
23
|
+
});
|
|
24
|
+
res.on('end', () => r(Buffer.concat(chunks)));
|
|
25
|
+
}).end();
|
|
26
|
+
});
|
|
27
|
+
};
|