Created
June 22, 2021 13:00
-
-
Save noudadrichem/703025bacdc2e90d8e4695d794578878 to your computer and use it in GitHub Desktop.
Compare images from URL or Buffer based on looks-same module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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