Refactor image rendering to use a browser pool and update dependencies

This commit is contained in:
ImBenji
2025-12-31 18:23:15 +00:00
parent 410a78ab2b
commit f876b01529
5 changed files with 130 additions and 36 deletions

View File

@@ -1,6 +1,6 @@
const nodeHtmlToImage = require('node-html-to-image');
const fs = require('fs');
const path = require('path');
const { renderHtml, shutdownPool } = require('./browserPool');
async function generateQuote(config, outputPath = 'quote.png') {
const htmlTemplate = fs.readFileSync(path.join(__dirname, 'quote.html'), 'utf8');
@@ -13,20 +13,8 @@ async function generateQuote(config, outputPath = 'quote.png') {
const modifiedHtml = htmlTemplate.replace('</head>', `${configScript}</head>`);
await nodeHtmlToImage({
output: outputPath,
html: modifiedHtml,
puppeteerArgs: {
args: ['--no-sandbox'],
defaultViewport: {
width: 3240,
height: 3240
}
},
beforeScreenshot: async (page) => {
await new Promise(resolve => setTimeout(resolve, 500));
}
});
const imageBuffer = await renderHtml(modifiedHtml, 500);
fs.writeFileSync(outputPath, imageBuffer);
console.log(`Quote generated: ${outputPath}`);
}
@@ -57,6 +45,7 @@ const exampleTweetWithImage = {
(async () => {
await generateQuote(exampleTweet, 'quote-no-image.png');
await generateQuote(exampleTweetWithImage, 'quote-with-image.png');
await shutdownPool();
})();
module.exports = { generateQuote };