You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

155 lines
4.5 KiB

var gm = require('gm').subClass({ imageMagick: true })
var async = require('async')
var path = require('path')
var fs = require('fs')
var mkdirp = require('mkdirp')
var CFG = require('../config')
var tools = {}
tools.getBoothList = function (pFrom, pCallback) {
fs.readdir(CFG.paths.final, function (pErr, pBoothList) {
var boothList = pBoothList ? pBoothList.sort() : []
boothList = boothList.splice(boothList.indexOf(pFrom) + 1)
pCallback(pErr, boothList)
})
}
tools.buildBooth = function (pBoothId, pCallback) {
// Create output prebuild directory
mkdirp.sync(path.join(CFG.paths.prebuilt, pBoothId))
// Resize pictures
async.mapSeries(CFG.pictNames,
function (pPictName, pCb) {
var srcPict = path.join(CFG.paths.original, pBoothId, pPictName)
var dstPict = path.join(CFG.paths.prebuilt, pBoothId, pPictName)
console.log('Resizing ' + dstPict)
gm(srcPict)
.autoOrient()
.gravity('Center')
.resize(CFG.cropSize.width, CFG.cropSize.height, '^')
.crop(CFG.cropSize.width, CFG.cropSize.height)
.write(dstPict, function (pErr) {
if (pErr) console.log('Failed to resize picture', pErr)
pCb()
})
},
function (pErr) {
console.log('Building booth')
switch (CFG.style) {
default :
BOB_generateBooth(pBoothId, 'default', pCallback)
}
})
}
function BOB_generateBooth (pBoothId, pType, pCallback) {
var prebuiltPath = path.join(CFG.paths.prebuilt, pBoothId)
var finalPict = path.join(CFG.paths.final, pBoothId + '.jpg')
var finalLdPict = path.join(CFG.paths.final_ld, pBoothId + '.jpg')
var outPict = gm(CFG.booths[pType].template)
var printedDate = new Date(parseInt(pBoothId))
outPict.fill(CFG.background)
outPict.drawRectangle(0, 0, CFG.booths[pType].resolution.width, CFG.booths[pType].resolution.height)
// Draw footer
outPict.draw('image Over 0,2100 1600,250 \'' + CFG.footer + '\'')
CFG.pictNames.forEach(function (pPictName, pIndex) {
var cmdDraw = 'image Over'
cmdDraw += ' ' + CFG.booths[pType].layout[pIndex].x
cmdDraw += ',' + CFG.booths[pType].layout[pIndex].y
cmdDraw += ' ' + CFG.booths[pType].layout[pIndex].width
cmdDraw += ',' + CFG.booths[pType].layout[pIndex].height
cmdDraw += ' \'' + path.join(prebuiltPath, pPictName) + '\''
// console.log(cmdDraw);
outPict.draw(cmdDraw)
})
outPict.fill('#666')
outPict.pointSize(20)
outPict.draw('text 1250,2350 "' + printedDate.toUTCString() + '"')
outPict.write(finalPict, function (pErr) {
if (pErr) {
console.log('Write outfile')
console.log(pErr)
pCallback(pErr)
} else {
gm(finalPict)
.resize(400, null)
.write(finalLdPict, function (pErr) {
pCallback(pErr)
})
}
})
}
module.exports = tools
tools.init = function () {
BOB_updateConfig()
}
// function (pConfig) {
// CFG = pConfig
// return tools
// }
function BOB_updateConfig () {
fs.readdir(CFG.paths.template, function (pErr, pFiles) {
if (pErr) {
console.log('BOB_updateConfig', 'Path:', CFG.paths.template, pErr)
} else {
async.map(pFiles,
function (pFile, pCb) {
// Get name of template
var boothName = pFile.split('.')[0]
// Init layouts object for the template
CFG.booths[boothName] = {
name: boothName,
template: path.join(CFG.paths.template, pFile),
resolution: { width: 0, height: 0 },
layout: []
}
// Get reslution of current template
gm(CFG.booths[boothName].template).size(function (pErr, pValue) {
if (pErr) {
console.log('[ERROR] Get template size', pErr)
} else {
CFG.booths[boothName].resolution = pValue
BOB_generatePictLayout(boothName)
}
pCb()
})
},
function (pErr) {
})
}
})
}
function BOB_generatePictLayout (pBoothName) {
var index = 0
var marginX = 20
var marginY = 20
var pictWidth = (CFG.booths[pBoothName].resolution.width - 3 * marginX) / 2
var pictHeight = parseInt(pictWidth * CFG.cropSize.height / CFG.cropSize.width)
CFG.pictNames.forEach(function (pPictName, pIndex) {
CFG.booths[pBoothName].layout.push({
x: marginX + (marginX + pictWidth) * (pIndex % 2),
y: marginY + (marginY + pictHeight) * parseInt(pIndex / 2),
width: pictWidth,
height: pictHeight
})
})
console.log('INIT DONE')
}