Skip to content

Instantly share code, notes, and snippets.

@koozdra
Created June 7, 2017 16:40
Show Gist options
  • Save koozdra/659b66c7ff61453977182d599a9346f4 to your computer and use it in GitHub Desktop.
Save koozdra/659b66c7ff61453977182d599a9346f4 to your computer and use it in GitHub Desktop.
gm image manipulation
const fs = require('fs');
const gm = require('gm');
function getSize(image) {
return new Promise((resolve, reject) => {
image.size((err, size) => {
if (err) {
reject(err);
return;
}
resolve(size);
})
});
}
function write(name, image) {
return new Promise((resolve, reject) => {
image.write(name, (err) => {
if (err) {
reject(err);
return;
}
resolve();
})
})
}
const padding = 20;
const fontSize = 24;
const rectHeight = padding * 2 + fontSize - 10;
const image = gm('image.jpg');
getSize(image)
.then(({width, height}) => {
image
.stroke('#ec2c22')
.fill('#ec2c2233')
.drawRectangle(0, height - rectHeight, width, height);
image
.stroke('#ffffff')
.fill('#ffffff')
.font('Change-Calibre-Regular.ttf', fontSize)
.drawText(padding, height - padding, 'Sign this petition');
console.log(width, height);
return write('image_small.jpg', image)
})
.then(() => console.log('done'))
.catch(console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment