Jimp: HORIZONTAL_ALIGN_CENTER and VERTICAL_ALIGN_MIDDLE not working?

Created on 15 Sep 2018  路  1Comment  路  Source: oliver-moran/jimp

Expected Behavior

Hello there! I'm trying to center printed text! Am I doing something wrong?

Current Behavior

Text isn't centered!

Steps to Reproduce

  1. test.png is a small 60by60 png file
  2. run code
  3. text will not be centered
var Jimp = require('jimp');

var fileName = 'test.png';
var imageCaption = '12';
var loadedImage;

Jimp.read(fileName, (err, image) => {
  if (err) throw err;

  Jimp.loadFont(Jimp.FONT_SANS_16_BLACK).then(font => {
    // load font from .fnt file
    image
      .print(font, 0, 0, {
        text: imageCaption,
        alignmentX: Jimp.HORIZONTAL_ALIGN_CENTER,
        alignmentY: Jimp.VERTICAL_ALIGN_MIDDLE
      })
      .write(fileName); // save
  });
});

Screenshots

Context

  • Jimp Version: 0.4.0
  • Operating System: Windows 7
  • Node version: v8.9.3

Failure Logs

Most helpful comment

When you supply HORIZONTAL_ALIGN_CENTER and VERTICAL_ALIGN_MIDDLE you have to also supply maxHeight and maxWidth to tell the print function where to center in.

const Jimp = require('jimp');

const fileName = 'test.png';
const imageCaption = '12';
let loadedImage;

Jimp.read(fileName, (err, image) => {
  if (err) {
    throw err;
  }

  Jimp.loadFont(Jimp.FONT_SANS_16_BLACK).then(font => {
    // load font from .fnt file
    image
      .print(
        font,
        0,
        0,
        {
          text: imageCaption,
          alignmentX: Jimp.HORIZONTAL_ALIGN_CENTER,
          alignmentY: Jimp.VERTICAL_ALIGN_MIDDLE
        },
        image.bitmap.width,
        image.bitmap.height
      )
      .write(fileName); // save
  });
});

>All comments

When you supply HORIZONTAL_ALIGN_CENTER and VERTICAL_ALIGN_MIDDLE you have to also supply maxHeight and maxWidth to tell the print function where to center in.

const Jimp = require('jimp');

const fileName = 'test.png';
const imageCaption = '12';
let loadedImage;

Jimp.read(fileName, (err, image) => {
  if (err) {
    throw err;
  }

  Jimp.loadFont(Jimp.FONT_SANS_16_BLACK).then(font => {
    // load font from .fnt file
    image
      .print(
        font,
        0,
        0,
        {
          text: imageCaption,
          alignmentX: Jimp.HORIZONTAL_ALIGN_CENTER,
          alignmentY: Jimp.VERTICAL_ALIGN_MIDDLE
        },
        image.bitmap.width,
        image.bitmap.height
      )
      .write(fileName); // save
  });
});
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Vanuan picture Vanuan  路  14Comments

codan84 picture codan84  路  25Comments

cancerberoSgx picture cancerberoSgx  路  64Comments

marcolino picture marcolino  路  21Comments

mkondel picture mkondel  路  14Comments