-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
42 lines (35 loc) · 1.17 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
const { parse } = require('url')
const rp = require('request-promise')
const pixelmatch = require('pixelmatch')
const sizeOf = require('image-size')
module.exports = async (req, res) => {
const { query } = parse(req.url, true)
const { image_url, other_image_url } = query
if (
typeof query == 'undefined' ||
typeof image_url == 'undefined' ||
typeof other_image_url == 'undefined'
) {
res.end('USAGE: set image_url and other_image_url as query params')
} else {
const image = await rp(image_url, { encoding: null })
const other_image = await rp(other_image_url, { encoding: null })
const image_size = sizeOf(image)
const other_image_size = sizeOf(other_image)
const pixel_difference = pixelmatch(
image,
other_image,
null,
image_size.width,
image_size.height
)
const percent_difference = (pixel_difference / (image_size["width"] * image_size["height"])) * 100.0
const results = {
image: image_size,
other_image: other_image_size,
pixel_difference: pixel_difference,
percent_difference: Math.round(percent_difference * 10) / 10
}
res.end(JSON.stringify(results))
}
}