forked from ratwix/PhotoTwixNodeJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_photoPrint.js
115 lines (95 loc) · 2.98 KB
/
server_photoPrint.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
var fs = require('fs');
var fs = require('fs-extra');
var Canvas = require('canvas');
var Winreg = require('winreg');
var log = function () {};
var iv_path = '"' + __dirname + '/bin/IrfanView/i_view32.exe' + '"';
var iv_path_short = 'bin/IrfanView/';
var imt_path = '';
function printPhoto(printObject) {
var photo_big = '"' + __dirname + '/public/' + printObject.big + '"';
log("Print photo : " + photo_big);
//Determine if file has to be duplicate and cut
var data = fs.readFileSync('./public/' + printObject.big);
//create new image result
var img = new Canvas.Image; // Create a new Image
img.src = data;
if (img.height < img.width) {
select_orientation(true);
} else {
select_orientation(false);
}
if (img.height / img.width > 2) {
//Image has to be duplicate, and cutter has to be twice
var canvas = new Canvas(img.width * 2, img.height);
var ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, img.width, img.height);
ctx.drawImage(img, img.width, 0, img.width, img.height);
require('./server_buildPhoto').saveImage(canvas.toDataURL(), './public/photos/tmp/tmp.png');
img_path = '"' + __dirname + '/public/photos/tmp/tmp.png';
load_printer_json('./conf/printer_cut.json', print);
} else {
img_path = photo_big;
load_printer_json('./conf/printer_nocut.json', print);
//direct print photo, no problem
}
}
function select_orientation(landscape, callback) {
if (landscape) {
fs.copySync(iv_path_short + 'i_view32_landscape.ini', iv_path_short + 'i_view32.ini');
} else {
fs.copySync(iv_path_short + 'i_view32_portrait.ini', iv_path_short + 'i_view32.ini');
}
}
//Run print
function print() {
var exec = require('child_process').exec,
child;
var cmd = iv_path + ' ' + img_path;
//var cmd = iv_path + ' ' + img_path; //uncomment if just preview photo
if (require('os').platform() == 'win32') {
cmd = cmd.replace(/\//g, "\\"); //comment if linux system
}
cmd = cmd + " /print";
log("CMD : " + cmd);
child = exec(cmd,
function (error, stdout, stderr) {
log('stdout: ' + stdout);
log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
}
//Modify registry to set cutter preference for printer
function load_printer_json(filename, callback) {
fs.readFile(filename, function (err, data) {
if (err) {
console.log('Error: ' + err);
return;
}
data = JSON.parse(data);
var count = 0;
for (var i = 0; i < data.registry.length; i++) {
var reg = data.registry[i];
var Winreg = require('winreg')
, registry = new Winreg({
hive: reg.hive,
key: reg.key
})
log("HIVE : " + reg.hive + " KEY : " + reg.key + " NAME : " + reg.name + " TYPE " + reg.type);
registry.set(reg.name, reg.type, reg.value, function (err) {
if (err) {
console.log('Error: ' + err);
}
//if all change has return success, call callback
++count;
if (count == data.registry.length) {
log("I print");
callback();
}
});
}
});
}
exports.printPhoto = printPhoto;