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.
 
 
 

153 lines
4.8 KiB

var BOB_mod_gm = require('gm').subClass({ imageMagick: true });
var BOB_mod_async = require('async');
var BOB_mod_path = require('path');
var BOB_mod_fs = require('fs');
var BOB_mod_mkdirp = require('mkdirp');
var BOB_cfg_config = null;
var BOB_module = {};
BOB_module.getBoothList = function(pFrom, pCallback) {
BOB_mod_fs.readdir(BOB_cfg_config.paths.final, function(pErr, pBoothList) {
var boothList = pBoothList.sort();
boothList = boothList.splice(boothList.indexOf(pFrom) + 1);
pCallback(pErr, boothList);
})
}
BOB_module.buildBooth = function(pBoothId, pCallback) {
var index = 0;
var funcResizeArray = [];
BOB_mod_mkdirp.sync(BOB_mod_path.join(BOB_cfg_config.paths.prebuilt, pBoothId));
BOB_cfg_config.pictNames.forEach(function(pPictName) {
funcResizeArray.push(function(pCb) {
var srcPict = BOB_mod_path.join(BOB_cfg_config.paths.original, pBoothId, pPictName);
var dstPict = BOB_mod_path.join(BOB_cfg_config.paths.prebuilt, pBoothId, pPictName);
console.log("Resizing "+dstPict);
BOB_mod_gm(srcPict)
.autoOrient()
.gravity('Center')
.resize(BOB_cfg_config.cropSize.width, BOB_cfg_config.cropSize.height, "^")
.crop(BOB_cfg_config.cropSize.width, BOB_cfg_config.cropSize.height)
// .modulate(100, 0)
// .contrast(+2)
.write(dstPict, function(pErr) {
pCb()
})
})
})
BOB_mod_async.parallel(
funcResizeArray,
function(pErr, pData) {
console.log("Building booth")
switch(BOB_cfg_config.style) {
default :
BOB_generateBooth(pBoothId, 'default', pCallback);
}
}
)
}
function BOB_generateBooth(pBoothId, pType, pCallback) {
var prebuiltPath = BOB_mod_path.join(BOB_cfg_config.paths.prebuilt, pBoothId);
var finalPict = BOB_mod_path.join(BOB_cfg_config.paths.final, pBoothId+".jpg");
var finalLdPict = BOB_mod_path.join(BOB_cfg_config.paths.final_ld, pBoothId+".jpg");
var outPict = BOB_mod_gm(BOB_cfg_config.booths[pType].template);
var printedDate = new Date(parseInt(pBoothId));
outPict.fill(BOB_cfg_config.background);
outPict.drawRectangle(0, 0, BOB_cfg_config.booths[pType].resolution.width, BOB_cfg_config.booths[pType].resolution.height);
BOB_cfg_config.pictNames.forEach(function(pPictName, pIndex) {
var cmdDraw = 'image Over';
cmdDraw += ' '+BOB_cfg_config.booths[pType].layout[pIndex].x;
cmdDraw += ','+BOB_cfg_config.booths[pType].layout[pIndex].y;
cmdDraw += ' '+BOB_cfg_config.booths[pType].layout[pIndex].width;
cmdDraw += ','+BOB_cfg_config.booths[pType].layout[pIndex].height;
cmdDraw += ' '+BOB_mod_path.join(prebuiltPath, pPictName);
console.log(cmdDraw);
outPict.draw(cmdDraw);
})
outPict.fill("#000000");
outPict.pointSize(40);
outPict.draw('text 1900,3850 "'+printedDate.toUTCString()+'"');
outPict.write(finalPict, function (pErr) {
BOB_mod_gm(finalPict)
.resize(600, null)
.write(finalLdPict, function(pErr) {
pCallback(pErr);
})
});
}
module.exports = function(pConfig) {
BOB_cfg_config = pConfig;
BOB_updateConfig();
return BOB_module;
}
function BOB_updateConfig() {
BOB_mod_fs.readdir(BOB_cfg_config.paths.template, function(pErr, pFiles) {
if( pErr ) {
console.log(pErr);
} else {
BOB_mod_async.map(pFiles,
function(pFile, pCb) {
// Get name of template
var boothName = pFile.split('.')[0];
// Init layouts object for the template
BOB_cfg_config.booths[boothName] = {
name : boothName,
template : BOB_mod_path.join(BOB_cfg_config.paths.template, pFile),
resolution : { width : 0, height : 0 },
layout : []
};
// Get reslution of current template
BOB_mod_gm(BOB_cfg_config.booths[boothName].template).size(function(pErr, pValue){
if( pErr ) {
console.log(pErr);
} else {
BOB_cfg_config.booths[boothName].resolution = pValue;
BOB_generatePictLayout(boothName);
}
pCb();
})
},
function(pErr) {
})
}
})
}
function BOB_generatePictLayout(pBoothName) {
var index = 0;
var marginX = 50;
var marginY = 50;
var pictWidth = ( BOB_cfg_config.booths[pBoothName].resolution.width - 3 * marginX ) / 2;
var pictHeight = parseInt(pictWidth * BOB_cfg_config.cropSize.height / BOB_cfg_config.cropSize.width);
BOB_cfg_config.pictNames.forEach(function(pPictName, pIndex) {
BOB_cfg_config.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");
}