Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Compare images from URL or Buffer based on looks-same module
import axios from 'axios';
import LooksSame from 'looks-same'
import path from 'path'
type Image = string | Buffer
class ImageCompareService {
public rootPath: string;
public staticPath: string;
constructor() {
this.rootPath = path.resolve(`${__dirname}/../../`)
this.staticPath = this.rootPath + '/static'
}
async imgUrlToBuffer(url: string) {
return axios
.get(url, { responseType: 'arraybuffer' })
.then(response => Buffer.from(response.data, 'binary'))
}
compare(x: Image, y: Image): Promise<any> {
const config = {
strict: true
}
return new Promise((resolve, reject) => {
LooksSame(x, y, config, (error, response) => {
if (error) reject(error)
resolve(response)
})
})
}
}
export default new ImageCompareService();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment