Skip to content

Commit

Permalink
cssrender #2
Browse files Browse the repository at this point in the history
  • Loading branch information
darsan-in committed Jul 11, 2024
1 parent f924c23 commit 9dab718
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions lib/imageset/cssrender.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import puppeteer from "puppeteer";

export async function getImageSize(
selectors: { id: string; classes: string[] },
htmlPath: string,
screenSizes: Record<string, number>,
): Promise<Record<string, number>> {
// Browser instance
const browser = await puppeteer.launch({
headless: "new",
args: ["--start-maximized"],
});

// Page to load
const page = await browser.newPage();

const nativeScreenSize = await page.evaluate(() => {
return {
height: window.screen.availHeight,
};
});

const imageSizes: Record<string, number> = {};

for (const screenKey of Object.keys(screenSizes)) {
// Setting specific width and height for viewport
await page.setViewport({
width: screenSizes[screenKey],
height: nativeScreenSize.height,
});

// Navigate to the htmlPath
await page.goto(htmlPath);

const selector: string = selectors.id
? selectors.id
: selectors.classes
? selectors.classes[0]
: "img";

// Extract the size of the image
const imageSize = await page.evaluate((selector) => {
const img: any = document.querySelector(selector);

return {
width: img?.width,
height: img?.height,
};
}, selector);

imageSizes[screenKey] = imageSize as any;
}

// Close the browser
await browser.close();

return imageSizes;
}

0 comments on commit 9dab718

Please sign in to comment.