Last active
January 25, 2024 23:12
-
-
Save jkeefe/cbf62994ac6cae92e166445689717f1f to your computer and use it in GitHub Desktop.
Adding text to images in Node
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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