Refactor image rendering to use a browser pool and update dependencies
This commit is contained in:
19
generate.js
19
generate.js
@@ -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 };
|
||||
|
||||
Reference in New Issue
Block a user