Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save blopa/7c4b896b80b504086efb7f43857afa3f to your computer and use it in GitHub Desktop.
Save blopa/7c4b896b80b504086efb7f43857afa3f to your computer and use it in GitHub Desktop.
Code for post "How to automatically create thumbnails for your videos with Node.js"
const generateImageForPost = async (
backgroundImagePath,
foregroundImagePath,
title,
outputPath
) => {
const backgroundImage = await loadImage(backgroundImagePath);
const foregroundImage = await loadImage(foregroundImagePath);
const canvas = createCanvas(backgroundImage.width, backgroundImage.height);
const ctx = canvas.getContext('2d');
ctx.drawImage(foregroundImage, 0, 0);
// TODO add text
// addCenteredTextWithBackground(ctx, title, canvas.width, canvas.height);
await new Promise((resolve, reject) => {
const out = createWriteStream(outputPath);
const stream = canvas.createPNGStream();
stream.pipe(out);
out.on('finish', () => {
console.log('The PNG file was created.');
resolve();
});
out.on('error', (err) => {
reject(err);
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment