Search code examples
node.jsimagetext

Create an image and write in it a text with Jimp (Node js)


I'm starting with node and I've written this program without success. I intend to create an image with dimensions with a color, write text in the image and then save to a file. The program does everything except the text. What am I doing wrong?

let Jimp = require('jimp');

let image = new Jimp(300, 530, 'green', (err, image) => {
    if (err) throw err;
});

let message = 'Hello!';
let x = 10;
let y = 10;
let maxWidth = 300;

Jimp.loadFont(Jimp.FONT_SANS_8_BLACK)
    .then(font => {
        image.print(font, x, y, message);
    });

let file = 'new_name' + '.' + image.getExtension();

Solution

  • I think @barro32 may be correct. I added some code to write out the image and it seems to work:

    let Jimp = require('jimp')
    
    let image = new Jimp(300, 530, 'green', (err, image) => {
      if (err) throw err
    })
    
    let message = 'Hello!'
    let x = 10
    let y = 10
    
    Jimp.loadFont(Jimp.FONT_SANS_64_BLACK)
      .then(font => {
        image.print(font, x, y, message)
        return image
      }).then(image => {
        let file = `new_name.${image.getExtension()}`
        return image.write(file) // save
      })
    

    The result was:

    new_name.png