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

This refers to the "gm" extension for node, http://aheckmann.imtqy.com/gm/docs.html

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

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

+5
source share
1 answer

You can use the region and gravity functions as follows:

 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') 
+4
source

Source: https://habr.com/ru/post/1247443/


All Articles