-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessImage.js
More file actions
87 lines (80 loc) · 3.19 KB
/
Copy pathprocessImage.js
File metadata and controls
87 lines (80 loc) · 3.19 KB
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
var sharp = require('sharp');
sharp.cache(false);
// sharp.cache({ files : 0 });
var lib = {
processImage: function (imgfile, oplist, filename) {
const sharpStream = sharp({
failOnError: true
});
console.log("Transformer Oplist ", oplist);
// return new Promise((resolve, reject) => {});
const transformer = sharp(imgfile)
.pipe(sharpStream);
oplist.reduce((_, op) => {
console.log("Op ", op.opname, op.options);
switch (op.opname) {
case 'fliphorizontal':
// Flop the image about the horizontal X axis.
transformer.pipe(sharpStream.flop());
break;
case 'flipvertical':
// Flip the image about the vertical Y axis.
transformer.pipe(sharpStream.flip());
break;
case 'greyscale':
// Convert to 8-bit greyscale; 256 shades of grey.
transformer.pipe(sharpStream.greyscale());
break;
case 'thumbnail':
// console.log("thumbnail");
transformer.pipe(sharpStream.resize(200, 200, {fit: sharp.fit.cover}));
break;
case 'rotate':
// Rotate
transformer.pipe(sharpStream.rotate(parseInt(op.options || 0)));
break;
case 'rotateright':
// Rotate right ie by 90
transformer.pipe(sharpStream.rotate(90));
break;
case 'rotateleft':
// Rotate right ie by 270
transformer.pipe(sharpStream.rotate(270));
break;
case 'resize':
// Resize
let opt = op.options;
if (opt && opt.includes(",")) {
let size = opt.split(",");
transformer.pipe(sharpStream.resize({
height: parseInt(size[0]),
width: parseInt(size[1])
}))
.on('error', (err) => {
return err
});
} else {
transformer.pipe(sharpStream.resize(parseInt(opt)));
}
break;
case 'end':
transformer.pipe(
sharp().toFile(filename, (err, info) => {
if (err)
console.error("Transformer Tofile Error ", err);
else
console.log("Transformer Tofile", info);
})
);
break;
default:
console.log("Unmatched / Default case ")
break;
}
}, {});
// return transformer;
return new Promise((resolve, reject) =>
transformer.on('end', resolve).on('error', reject));
}
};
module.exports = lib;