You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.5 KiB
JavaScript
54 lines
1.5 KiB
JavaScript
let PSD = require('psd');
|
|
|
|
|
|
|
|
let args = process.argv.splice(2);
|
|
|
|
let filename = args[0];
|
|
let imagePath = args[1];
|
|
|
|
let psd = PSD.fromFile(filename);
|
|
psd.parse();
|
|
|
|
//let tree = psd.tree().children()[5].saveAsPng('./output.png');
|
|
|
|
let document = psd.tree().export().document;
|
|
let psdInfo = {};
|
|
psdInfo['width'] = document.width;
|
|
psdInfo['height'] = document.height;
|
|
|
|
let layers = [];
|
|
|
|
function psdDump(tree) {
|
|
for (let idx in tree) {
|
|
let layer = tree[idx];
|
|
if (layer.type == 'group') {
|
|
psdDump(layer.children())
|
|
}
|
|
let info = layer.export();
|
|
let layerInfo = {};
|
|
layerInfo['name'] = info.name;
|
|
layerInfo['visible'] = info.visible;
|
|
layerInfo['opacity'] = info.opacity;
|
|
layerInfo['left'] = info.left;
|
|
layerInfo['top'] = info.top;
|
|
layerInfo['width'] = info.width;
|
|
layerInfo['height'] = info.height;
|
|
if (layer.export().text) {
|
|
layerInfo['text'] = info.text.value;
|
|
layerInfo['font'] = info.text.font;
|
|
layerInfo['type'] = 'text';
|
|
} else {
|
|
let imgName = layer.name.replace('\\', '_').replace('/', '_');
|
|
let src = imagePath + '/' + imgName + '_'+ idx + '.png';
|
|
layer.saveAsPng(src).catch(e => {});
|
|
layerInfo['image'] = src;
|
|
layerInfo['type'] = 'image';
|
|
}
|
|
layers.push(layerInfo);
|
|
}
|
|
}
|
|
|
|
psdDump(psd.tree().children());
|
|
psdInfo['layers'] = layers;
|
|
console.log(JSON.stringify(psdInfo)); |