I'm trying to create a simple dynamic-badge (png) to embed in static pages to let know the status of my application.
I'd like so to use an existing PNG image and write on it some text with Node.js.
I've found lot of libraries but all of them use Imagemagick or Cairo as native dependencies, I'd like to avoid to install anything else on the server.
I've then found lwip, but I can't really understand how to write text on an image with it. How can I do?
The easiest way to write to files in Node.js is to use the fs.writeFile () API. const content = 'Some content!'; Alternatively, you can use the synchronous version fs.writeFileSync (): const content = 'Some content!'; You can also use the promise-based fsPromises.writeFile () method offered by the fs/promises module:
In this article, you learned how to use sharp methods to process images in Node.js. First, you created an instance to read an image and used the metadata () method to extract the image metadata. You then used the resize () method to resize an image. Afterwards, you used the format () method to change the image type, and compress the image.
Node.js has an ecosystem of libraries you can use to process images, such as sharp, jimp, and gm module. This article will focus on the sharp module. sharp is a popular Node.js image processing library that supports various image file formats, such as JPEG, PNG, GIF, WebP, AVIF, SVG and TIFF.
Within the function body, you read the image by calling sharp () which takes the image path as an argument, here with sammy.png. Apart from taking an image path, sharp () can also read image data stored in a Buffer, Uint8Array, or Uint8ClampedArray provided the image is JPEG, PNG, GIF, WebP, AVIF, SVG or TIFF.
You could use Jimp. It has a print method:
var Jimp = require("jimp");
var fileName = 'test.png';
var imageCaption = 'Image caption';
var loadedImage;
Jimp.read(fileName)
.then(function (image) {
loadedImage = image;
return Jimp.loadFont(Jimp.FONT_SANS_16_BLACK);
})
.then(function (font) {
loadedImage.print(font, 10, 10, imageCaption)
.write(fileName);
})
.catch(function (err) {
console.error(err);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With