diff --git a/packages/common/src/utils/matchImages.ts b/packages/common/src/utils/matchImages.ts new file mode 100644 index 00000000..f4127d6a --- /dev/null +++ b/packages/common/src/utils/matchImages.ts @@ -0,0 +1,19 @@ +import { PNG } from "pngjs"; +import pixelmatch from "pixelmatch"; + +interface MatchImagesArgs { + img1Buff: Buffer; + img2Buff: Buffer; +} + +export const matchImages = ({ img1Buff, img2Buff }: MatchImagesArgs) => { + const img1 = PNG.sync.read(img1Buff); + const img2 = PNG.sync.read(img2Buff); + const { width, height } = img1; + + const diff = new PNG({ width, height }); + + return pixelmatch(img1.data, img2.data, diff.data, width, height, { + threshold: 0.25 + }); +};