|
2 | 2 |
|
3 | 3 | Sharp based implementation of perceptual hash (phash) algorithm described [there](http://www.hackerfactor.com/blog/?/archives/432-Looks-Like-It.html).
|
4 | 4 |
|
| 5 | +## Installation |
| 6 | + |
| 7 | +```sh |
| 8 | +yarn add sharp sharp-phash |
| 9 | +# or |
| 10 | +npm i sharp sharp-phash |
| 11 | +``` |
| 12 | + |
| 13 | +You **must** install **sharp** yourself. |
| 14 | + |
5 | 15 | ## How to use
|
6 | 16 |
|
7 | 17 | ```js
|
8 |
| -'use strict'; |
| 18 | +"use strict"; |
9 | 19 |
|
10 |
| -const fs = require('fs'); |
11 |
| -const Promise = require('bluebird'); |
| 20 | +const fs = require("fs"); |
| 21 | +const Promise = require("bluebird"); |
12 | 22 |
|
13 |
| -const assert = require('assert'); |
| 23 | +const assert = require("assert"); |
14 | 24 |
|
15 |
| -const phash = require('sharp-phash'); |
16 |
| -const dist = require('sharp-phash/distance'); |
| 25 | +const phash = require("sharp-phash"); |
| 26 | +const dist = require("sharp-phash/distance"); |
17 | 27 |
|
18 |
| -const img1 = fs.readFileSync('./Lenna.png'); |
19 |
| -const img2 = fs.readFileSync('./Lenna.jpg'); |
20 |
| -const img3 = fs.readFileSync('./Lenna-sepia.jpg'); |
| 28 | +const img1 = fs.readFileSync("./Lenna.png"); |
| 29 | +const img2 = fs.readFileSync("./Lenna.jpg"); |
| 30 | +const img3 = fs.readFileSync("./Lenna-sepia.jpg"); |
21 | 31 |
|
22 |
| -Promise.all([ |
23 |
| - phash(img1), |
24 |
| - phash(img2), |
25 |
| - phash(img3) |
26 |
| -]) |
27 |
| - .then(([hash1, hash2, hash3]) => { |
| 32 | +Promise.all([phash(img1), phash(img2), phash(img3)]).then( |
| 33 | + ([hash1, hash2, hash3]) => { |
28 | 34 | // hash returned is 64 characters length string with 0 and 1 only
|
29 | 35 | assert(dist(hash1, hash2) < 5);
|
30 | 36 | assert(dist(hash2, hash3) < 5);
|
31 | 37 | assert(dist(hash3, hash1) < 5);
|
32 |
| - }); |
33 |
| -``` |
34 |
| - |
35 |
| -## Your own sharp version |
36 |
| - |
37 |
| -```js |
38 |
| -const sharp = require('sharp'); |
39 |
| -const phash = require('sharp-phash'); |
40 |
| - |
41 |
| -phash(..., { sharp }) |
42 |
| - |
| 38 | + } |
| 39 | +); |
43 | 40 | ```
|
0 commit comments