mirror of
https://codeberg.org/JasterV/imgphash.git
synced 2026-04-26 18:10:01 +00:00
first commit
This commit is contained in:
parent
68b65bc53f
commit
cbe7fe1d05
6 changed files with 74 additions and 46 deletions
1
.npmrc
Normal file
1
.npmrc
Normal file
|
|
@ -0,0 +1 @@
|
|||
//default.npmjs.org/:_authToken=${NPM_TOKEN}
|
||||
19
index.js
19
index.js
|
|
@ -1,19 +0,0 @@
|
|||
const { download, hash, calculateSimilarity } = require("./lib");
|
||||
|
||||
const URL1 =
|
||||
"https://res.cloudinary.com/demo/image/upload/f_auto,q_auto/w_400/koala1.jpg";
|
||||
const URL2 = "https://res.cloudinary.com/demo/image/upload/h_180/koala2.jpg";
|
||||
const URL3 = "https://res.cloudinary.com/demo/image/upload/h_180/koala4.jpg";
|
||||
|
||||
async function main() {
|
||||
console.log("Urls", { URL1, URL3 });
|
||||
const image1 = await download(URL1);
|
||||
const image2 = await download(URL3);
|
||||
const hash1 = await hash(image1);
|
||||
const hash2 = await hash(image2);
|
||||
console.log("Hashes", { hash1, hash2 });
|
||||
const similarity = calculateSimilarity(hash1, hash2);
|
||||
console.log("similarity", { similarity });
|
||||
}
|
||||
|
||||
main().catch((err) => console.log(err));
|
||||
10
package-lock.json
generated
10
package-lock.json
generated
|
|
@ -1,13 +1,13 @@
|
|||
{
|
||||
"name": "phash-test",
|
||||
"version": "1.0.0",
|
||||
"name": "image-phash",
|
||||
"version": "0.1.0",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "phash-test",
|
||||
"version": "1.0.0",
|
||||
"license": "ISC",
|
||||
"name": "image-phash",
|
||||
"version": "0.1.0",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@canvas/image": "^1.0.1",
|
||||
"axios": "^0.26.1",
|
||||
|
|
|
|||
30
package.json
30
package.json
|
|
@ -1,14 +1,16 @@
|
|||
{
|
||||
"name": "phash-test",
|
||||
"version": "1.0.0",
|
||||
"name": "image-phash",
|
||||
"version": "0.1.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"start": "node index.js"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"main": "./src/lib.js",
|
||||
"keywords": [
|
||||
"javascript",
|
||||
"phash",
|
||||
"hashing",
|
||||
"images"
|
||||
],
|
||||
"author": "Victor Martinez <jaster.victor@gmail.com>",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@canvas/image": "^1.0.1",
|
||||
"axios": "^0.26.1",
|
||||
|
|
@ -16,5 +18,13 @@
|
|||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^17.0.21"
|
||||
}
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/JasterV/hash-image.git"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/JasterV/hash-image/issues"
|
||||
},
|
||||
"homepage": "https://github.com/JasterV/hash-image#readme"
|
||||
}
|
||||
43
src/lib.js
Normal file
43
src/lib.js
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
const blockhash = require("blockhash-core");
|
||||
const { getImageData, imageFromBuffer } = require("@canvas/image");
|
||||
const { hexToBin, download } = require("./utils");
|
||||
|
||||
class HashImage {
|
||||
constructor(buffer) {
|
||||
if (!(buffer instanceof Uint8Array)) {
|
||||
throw new Error(
|
||||
"Invalid parameter, please use a buffer or an instance of Uint8Array"
|
||||
);
|
||||
}
|
||||
this.buffer = buffer;
|
||||
}
|
||||
|
||||
static async fromUrl(url) {
|
||||
const buffer = await download(url);
|
||||
return new HashImage(buffer);
|
||||
}
|
||||
|
||||
static hashCompare(hash1, hash2) {
|
||||
let similarity = 0;
|
||||
const hash1Array = hash1.split("");
|
||||
hash1Array.forEach((bit, index) => {
|
||||
hash2[index] === bit ? similarity++ : null;
|
||||
});
|
||||
return similarity / hash1.length;
|
||||
}
|
||||
|
||||
async hash() {
|
||||
const data = await imageFromBuffer(this.buffer);
|
||||
const hexHash = await blockhash.bmvbhash(getImageData(data), 8);
|
||||
const hash = hexToBin(hexHash);
|
||||
return hash;
|
||||
}
|
||||
|
||||
async compare(other) {
|
||||
const hash1 = await this.hash();
|
||||
const hash2 = await other.hash();
|
||||
return HashImage.hashCompare(hash1, hash2);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { HashImage };
|
||||
|
|
@ -1,13 +1,6 @@
|
|||
const blockhash = require("blockhash-core");
|
||||
const { getImageData, imageFromBuffer } = require("@canvas/image");
|
||||
const axios = require("axios").default;
|
||||
|
||||
async function hash(buffer) {
|
||||
const image = await imageFromBuffer(buffer);
|
||||
const hash = await blockhash.bmvbhash(getImageData(image), 8);
|
||||
return hexToBin(hash);
|
||||
}
|
||||
const { default: axios } = require("axios");
|
||||
|
||||
// Utils
|
||||
async function download(url) {
|
||||
const response = await axios.get(url, { responseType: "stream" });
|
||||
const stream = response.data;
|
||||
|
|
@ -19,7 +12,7 @@ async function download(url) {
|
|||
return buffer;
|
||||
}
|
||||
|
||||
function calculateSimilarity(hash1, hash2) {
|
||||
function calculateHashSimilarity(hash1, hash2) {
|
||||
let similarity = 0;
|
||||
const hash1Array = hash1.split("");
|
||||
hash1Array.forEach((bit, index) => {
|
||||
|
|
@ -61,7 +54,7 @@ function hexToBin(hexString) {
|
|||
}
|
||||
|
||||
module.exports = {
|
||||
hash,
|
||||
download,
|
||||
calculateSimilarity,
|
||||
hexToBin,
|
||||
calculateHashSimilarity,
|
||||
};
|
||||
Loading…
Reference in a new issue