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
5.1 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 ? pBoothList.sort() : [];
boothList = boothList.splice(boothList.indexOf(pFrom) + 1);
pCallback(pErr, boothList);
})
}
BOB_module.buildBooth = function(pBoothId, pCallback) {
// Create output prebuild directory
BOB_mod_mkdirp.sync(BOB_mod_path.join(BOB_cfg_config.paths.prebuilt, pBoothId));
// Resize pictures
BOB_mod_async.mapSeries(BOB_cfg_config.pictNames,
function(pPictName, 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)
.write(dstPict, function(pErr) {
if(pErr) console.log("Failed to resize picture", pErr)
pCb()
})
},
function(pErr) {
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);
// Draw footer
outPict.draw('image Over 0,2100 1600,250 \''+BOB_cfg_config.footer+'\'');
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("#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 {
BOB_mod_gm(finalPict)
.resize(400, 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("BOB_updateConfig", "Path:", BOB_cfg_config.paths.template, 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("[ERROR] Get template size", pErr);
} else {
BOB_cfg_config.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 = ( 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");
}