Skip to content

Instantly share code, notes, and snippets.

@noudadrichem
Created June 22, 2021 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noudadrichem/703025bacdc2e90d8e4695d794578878 to your computer and use it in GitHub Desktop.
Save noudadrichem/703025bacdc2e90d8e4695d794578878 to your computer and use it in GitHub Desktop.
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