-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
49 lines (39 loc) · 1.28 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
'use strict'
const extractFrames = require('gif-extract-frames')
const pHash = require('phash-im')
const path = require('path')
const rmfr = require('rmfr')
const tempy = require('tempy')
exports.compute = async (input) => {
const directory = tempy.directory()
const filename = '%d.png'
const output = path.join(directory, filename)
const results = await extractFrames({
input,
output
})
const isAnimated = (results.shape.length === 4)
let result
if (isAnimated) {
const frames = results.shape[0]
const firstFrame = 0
const middleFrame = (frames / 2) | 0
const lastFrame = Math.max(frames - 1, 0)
const hashes = await Promise.all([
pHash.compute(path.join(directory, filename.replace('%d', firstFrame))),
pHash.compute(path.join(directory, filename.replace('%d', middleFrame))),
pHash.compute(path.join(directory, filename.replace('%d', lastFrame)))
])
result = hashes[0].concat(hashes[1]).concat(hashes[2])
} else {
result = await pHash.compute(path.join(directory, filename.replace('%d', 0)))
}
await rmfr(directory)
return result
}
exports.compare = async (hash1, hash2) => {
if (hash1.length !== hash2.length) {
throw new Error('unable to compare incompatible hashes')
}
return pHash.compare(hash1, hash2)
}