Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add centered text with gm for node (graphicsmagick/imagemagick)?

Tags:

node.js

gm

This relates to the "gm" extension for node, http://aheckmann.github.io/gm/docs.html

I need to add some text centered around a bounding box (horizontally is enough). The function drawText() requires x,y coordinates, but there is no way to draw centered text.

I would otherwise need a function which can return the width of a text string in the font/size given, so I can calculate my starting x position in javascript, before calling drawText().

like image 435
Rildo Pragana Avatar asked Apr 19 '16 15:04

Rildo Pragana


1 Answers

You can use the region and gravity functions this way:

gm(filePath)
    .region(WIDTH, HEIGHT, X, Y)
    .gravity('Center')
    .fill(color)
    .fontSize(textFontSize)
    .font(font)
    .drawText(0, 0, 'This text will be centered inside the region')
like image 63
tomericco Avatar answered Sep 27 '22 23:09

tomericco