Skip to content

Instantly share code, notes, and snippets.

@jkeefe
Last active January 25, 2024 23:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkeefe/cbf62994ac6cae92e166445689717f1f to your computer and use it in GitHub Desktop.
Save jkeefe/cbf62994ac6cae92e166445689717f1f to your computer and use it in GitHub Desktop.
Adding text to images in Node
const fs = require('fs')
const gm = require('gm').subClass({ imageMagick: '7+' });
WIDTH = 1138
HEIGHT = 50
X = 0
Y = 0
gm('./images/us_at_bottom.png')
.region(WIDTH, HEIGHT, X, Y)
.gravity('North')
.fill('#000000')
.fontSize(30)
.font('./fonts/FontName.otf')
.drawText(0, 0, 'This should be text')
.write('./images/with_text.png', () => {
console.log('done')
})
// note you can change alignment (flush left, right, etc) based on gravity - Center, South, NorthEast, etc.
// from https://stackoverflow.com/questions/36723279/how-to-add-centered-text-with-gm-for-node-graphicsmagick-imagemagick
// see also: https://www.npmjs.com/package/gm
const fs = require('fs')
const gm = require('gm').subClass({ imageMagick: '7+' });
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')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment