🧹 Diff files cleanup

This commit is contained in:
ful1e5 2020-08-29 17:32:25 +05:30
parent b4a678497c
commit f0ca4c5689

View file

@ -1,27 +1,19 @@
import fs from "fs";
import path from "path";
import { PNG } from "pngjs";
import pixelmatch from "pixelmatch";
export const matchImages = (img1Buff: Buffer, img2Buff: Buffer) => {
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 });
const out = pixelmatch(img1.data, img2.data, diff.data, width, height, {
threshold: 0.3,
return pixelmatch(img1.data, img2.data, diff.data, width, height, {
threshold: 0.25,
});
if (process.env.NODE_ENV === "development") {
const diffFilesPath = path.resolve(process.cwd(), "diff");
if (!fs.existsSync(diffFilesPath)) fs.mkdirSync(diffFilesPath);
fs.writeFileSync(
path.resolve(diffFilesPath, `${out}.png`),
PNG.sync.write(diff)
);
}
return out;
};