Skip to content

Commit 862b4da

Browse files
committed
Pad mismatched screenshots
1 parent 40d3a0f commit 862b4da

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

scripts/sitemap-visual-diff.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,18 +118,32 @@ async function screenshotFullPage(page: any, url: string, outputPath: string) {
118118
}
119119
}
120120

121+
function padImage(img: PNG, width: number, height: number): PNG {
122+
if (img.width === width && img.height === height) {
123+
return img;
124+
}
125+
const out = new PNG({ width, height });
126+
PNG.bitblt(img, out, 0, 0, img.width, img.height, 0, 0);
127+
return out;
128+
}
129+
121130
function compareImages(
122131
prodPath: string,
123132
prevPath: string,
124133
diffPath: string,
125134
tolerance: number,
126135
diffAlpha: number
127136
): boolean {
128-
const prod = PNG.sync.read(fs.readFileSync(prodPath));
129-
const prev = PNG.sync.read(fs.readFileSync(prevPath));
137+
let prod = PNG.sync.read(fs.readFileSync(prodPath));
138+
let prev = PNG.sync.read(fs.readFileSync(prevPath));
130139
if (prod.width !== prev.width || prod.height !== prev.height) {
131-
console.warn(`Size mismatch for ${prevPath}`);
132-
return false;
140+
const width = Math.max(prod.width, prev.width);
141+
const height = Math.max(prod.height, prev.height);
142+
console.warn(
143+
`Size mismatch for ${prevPath}, padding images to ${width}x${height}`
144+
);
145+
prod = padImage(prod, width, height);
146+
prev = padImage(prev, width, height);
133147
}
134148
const diff = new PNG({ width: prod.width, height: prod.height });
135149
const numDiff = pixelmatch(

0 commit comments

Comments
 (0)