Created
March 8, 2019 11:59
-
-
Save WietseWind/d9331b68ab169ef9d7dda2db18347f92 to your computer and use it in GitHub Desktop.
Generate Meetup labels
This file contains 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 username = process.argv[2] | |
const type = process.argv[3] || 1 | |
const sharp = require('sharp') | |
const QRCode = require('qrcode') | |
const fetch = require('node-fetch') | |
const printer = require("node-printer-lp-complete") | |
const printOptions = { | |
media: 'Custom.25x50mm', | |
destination: "ColorLabelPrinter", | |
portrait: '', | |
fitplot: true, | |
n: 1 | |
} | |
const main = async () => { | |
console.log('Get avatar') | |
const avatarimg = await fetch('http://twivatar.glitch.me/' + username).then(res => res.buffer()) | |
console.log('Resize avatar') | |
const avatar = await sharp(avatarimg) | |
.resize(269) | |
.toBuffer() | |
const canvas = await sharp({ | |
create: { | |
width: 1060, | |
height: 560, | |
channels: 4, | |
background: { r: 0, g: 0, b: 0, alpha: 0.5 } | |
} | |
}) | |
.ensureAlpha() | |
.overlayWith(avatar, { | |
top: 106, | |
left: 187 | |
}) | |
.png().toBuffer() | |
console.log('Merge overlay') | |
const images = await sharp(canvas) | |
.ensureAlpha() | |
.overlayWith('lbl/' + type + '.png', { top: 0, left: 0 }) | |
.png().toBuffer() | |
console.log('Merge text') | |
const withtext = await sharp(images) | |
.ensureAlpha() | |
.overlayWith(Buffer.from(` | |
<svg viewBox="0 0 780 300" xmlns="http://www.w3.org/2000/svg"> | |
<text x="300" y="200" style="text-anchor: middle; font-size: 55px; font-family: Brandon Text; font-weight: bold; fill: white;">@${username}</text> | |
</svg> | |
`.trim()), { top: 435, left: 20 }) | |
.png().toBuffer() | |
console.log('Generate QR') | |
const qrimage = await new Promise((resolve, reject) => { | |
QRCode.toDataURL('xrptipbot://twitter/' + username, { | |
errorCorrectionLevel: 'Q', | |
type: 'png', | |
margin: 2, | |
width: 245, | |
color: { | |
dark: '#ffffffff', | |
light: '#00000000' | |
} | |
}, (err, url) => { | |
if (err) reject(err) | |
resolve(Buffer.from(url.split(',')[1], 'base64')) | |
}) | |
}) | |
console.log('Write output') | |
const output = await sharp(withtext) | |
.overlayWith(qrimage, { top: 122, left: 684 }) | |
.png() | |
.toFile('lbl/output.png') | |
console.log('Send to printer') | |
printer.printFile('lbl/output.png', printOptions, "meetup"); | |
console.log('Done', output) | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment