compare-one-site.js
1 #!/usr/bin/env node 2 3 import sharp from 'sharp'; 4 import { join, dirname } from 'path'; 5 import { fileURLToPath } from 'url'; 6 7 const __filename = fileURLToPath(import.meta.url); 8 const __dirname = dirname(__filename); 9 10 const siteId = process.argv[2] || '23435'; 11 const screenshotDir = join(__dirname, '..', 'screenshots', siteId); 12 13 const pairs = [ 14 { cropped: 'desktop_above.jpg', uncropped: 'desktop_above_uncropped.jpg' }, 15 { cropped: 'desktop_below.jpg', uncropped: 'desktop_below_uncropped.jpg' }, 16 { cropped: 'mobile_above.jpg', uncropped: 'mobile_above_uncropped.jpg' }, 17 ]; 18 19 async function compareScreenshot(cropped, uncropped) { 20 try { 21 // Load cropped image and get dimensions 22 const croppedBuffer = await sharp(join(screenshotDir, cropped)).toBuffer(); 23 const croppedMeta = await sharp(join(screenshotDir, cropped)).metadata(); 24 25 console.log(`\n${cropped} vs ${uncropped}:`); 26 console.log( 27 ` Cropped: ${croppedMeta.width}x${croppedMeta.height} (${Math.round(croppedBuffer.length / 1024)}KB)` 28 ); 29 30 // Load uncropped original 31 const uncroppedOriginal = await sharp(join(screenshotDir, uncropped)).toBuffer(); 32 const uncroppedMeta = await sharp(join(screenshotDir, uncropped)).metadata(); 33 console.log( 34 ` Uncropped original: ${uncroppedMeta.width}x${uncroppedMeta.height} (${Math.round(uncroppedOriginal.length / 1024)}KB)` 35 ); 36 37 // Resize uncropped to match cropped dimensions 38 const resizedUncropped = await sharp(join(screenshotDir, uncropped)) 39 .resize(croppedMeta.width, croppedMeta.height, { 40 fit: 'cover', 41 position: 'entropy', 42 }) 43 .jpeg({ quality: 85, mozjpeg: true }) 44 .toBuffer(); 45 46 console.log( 47 ` Uncropped resized: ${croppedMeta.width}x${croppedMeta.height} (${Math.round(resizedUncropped.length / 1024)}KB)` 48 ); 49 50 const sizeDiff = Math.abs(croppedBuffer.length - resizedUncropped.length); 51 const sizeDiffPercent = (sizeDiff / resizedUncropped.length) * 100; 52 53 console.log( 54 `\n Size difference: ${sizeDiff} bytes (${Math.round(sizeDiff / 1024)}KB, ${sizeDiffPercent.toFixed(2)}%)` 55 ); 56 console.log( 57 ` Would be deleted with 5KB & 1% threshold? ${sizeDiff < 5120 && sizeDiffPercent < 1 ? 'YES' : 'NO'}` 58 ); 59 } catch (error) { 60 console.log(`\n${cropped} vs ${uncropped}: ${error.message}`); 61 } 62 } 63 64 console.log(`Analyzing screenshots for site ${siteId}...`); 65 66 for (const pair of pairs) { 67 await compareScreenshot(pair.cropped, pair.uncropped); 68 }