-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.js
67 lines (56 loc) · 1.64 KB
/
utils.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
var url = require("url"),
fs = require("fs"),
http = require("http"),
Canvas = require("canvas");
exports.dataToCanvas = function(imagedata) {
img = new Canvas.Image();
img.src = new Buffer(imagedata, 'binary');
var canvas = new Canvas(img.width, img.height);
var ctx = canvas.getContext('2d');
ctx.patternQuality = "best";
ctx.drawImage(img, 0, 0, img.width, img.height,
0, 0, img.width, img.height);
return canvas;
}
exports.drawImgToCanvas = function(file, callback) {
fs.readFile(file, function(err, data) {
if (err) {
return callback(err);
}
try {
var canvas = exports.dataToCanvas(data);
} catch(err) {
return callback(err);
}
callback(null, canvas);
});
}
exports.drawImgToCanvasSync = function(file) {
var data = fs.readFileSync(file);
if (!data.length) {
throw "empty file";
}
var canvas = exports.dataToCanvas(data);
return canvas;
}
exports.writeCanvasToFile = function(canvas, file, callback) {
var buffer = canvas.toBuffer(); // png data
fs.writeFile(file, buffer, callback);
}
exports.writeCanvasToFileSync = function(canvas, file) {
var buffer = canvas.toBuffer(); // png data
fs.writeFileSync(file, buffer);
}
exports.saveImgToFile = function(file, imagedata) {
fs.writeFile(file, imagedata, 'binary', function(err) {
if (err) throw err;
})
}
exports.resizeCanvas = function(canvas, width, height) {
var resizeCanvas = new Canvas(width, height);
var ctx = resizeCanvas.getContext('2d');
ctx.patternQuality = "best";
ctx.drawImage(canvas, 0, 0, canvas.width, canvas.height,
0, 0, width, height);
return resizeCanvas;
}