Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 65 additions & 2 deletions bin/psd.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ program
.version(require('../package.json').version)
.arguments('<file...>')
.option('-c, --convert', 'Convert to PNG file named <FILENAME>.png')
.option('-l, --layer', 'Convert layer to PNG file named <FILENAME>/<LAYERNAME>.png')
.option('-t, --text', 'Extract text content to <FILENAME>.txt')
.option('-o, --open', 'Preview file after conversion (triggers -c option)')
.action(processFiles)
Expand All @@ -39,11 +40,37 @@ function convertFile(filepath, psdPromise, cb) {
});
}

function extractLayerFromFile(filepath, psdPromise, cb) {
var filedir = filepath.replace(/\.psd$/, '');
var start = new Date();
if (!fs.existsSync(filedir)){
fs.mkdirSync(filedir);
}

psdPromise.then(function(psd) {
psd.tree().descendants().forEach(function (node) {
if ( node.isGroup() ) return true;
if (typeof node.text !== 'undefined' ) return true;
if ( node.visible() ) {
node.saveAsPng(filedir+"/" + node.name.replace(/[^a-z0-9]/gi, '_').replace(/(_{2,})/gi, '_')+ ".png").catch(function (err) {
console.log(err.stack);
});
}
});
}).then(function () {
console.log("Finished in " + ((new Date()) - start) + "ms");
}).catch(function (err) {
console.log(err.stack);
});
}

// extract text from PSD file
function extractTextFromFile(filepath, psdPromise, cb) {
var fileText = filepath.replace(/\.psd$/, '.txt');
var fileString = '';

var summarizeFonts = {};
var summarizeFontsColor = {};
var summarizeFontsSizes = {};
psdPromise.then(function(psd) {

psd.tree().export().children.forEach(function(child) {
Expand All @@ -55,8 +82,16 @@ function extractTextFromFile(filepath, psdPromise, cb) {
fileString += '\n' + t.path.join(' > ');
fileString += '\n' + '---';
fileString += '\n\n' + t.text.replace(/\r/g, '\n');
fileString += '\n\n' + t.fontinfo.replace(/\r/g, '\n');
summarizeFonts[t.font.name] = t.font.name;
summarizeFontsColor[t.font.colors] = t.font.colors;
summarizeFontsSizes[t.font.sizes] = t.font.sizes;
});
});
fileString += '\n\n' + '===== SUMMARY =====';
fileString += '\n' + 'FONTS: '+Object.keys(summarizeFonts).join(", ");
fileString += '\n' + 'FONTS COLORS: '+Object.keys(summarizeFontsColor).join(" ");
fileString += '\n' + 'FONTS SIZES: '+Object.keys(summarizeFontsSizes).join(" ");

fs.writeFile(fileText, fileString, function(err) {
if (err) {
Expand Down Expand Up @@ -98,6 +133,14 @@ function processFiles(files, env) {
convertFile(filepath, psdPromise, cb);
});
}

// convert layer to PNG
if (program.layer) {
asyncTasks.push(function(cb) {
extractLayerFromFile(filepath, psdPromise, cb);
});
}

// extract text data
if (program.text) {
asyncTasks.push(function(cb) {
Expand Down Expand Up @@ -146,23 +189,43 @@ function getCommandLine() {
}



function PSDLayer(path, element) {
this.path = path.slice();
this.path.push(element.name);

var self = this;
var convertPtToPx= function (pt) {
var px = pt * 96 / 72;
return Math.round((px*100))*0.01;
};

return {
extractText: function() {
var text = [];

if (typeof element.text !== 'undefined' && element.text !== undefined) {

/*
samples:
{"name":"Novecentosanswide-Bold","sizes":[12.5,12.5,12.5,12.5],"colors":[[19,62,89,255],[19,62,89,255],[19,62,89,255],[19,62,89,255]],"alignment":["center","center","center","center"]}
*/
var f = element.text.font;
font = {};
font.name = f.name ;
font.sizes = convertPtToPx(f.sizes[0]);
font.colors = 'rgba('+f.colors[0].join(', ')+')';
font.alignment = f.alignment[0];

text.push({
path: self.path,
text: element.text.value || null,
font: font,
fontinfo: JSON.stringify(font) || null
});

}

if (typeof(element.children) !== 'undefined') {
element.children.forEach(function(child) {
var layer = new PSDLayer(self.path, child);
Expand Down