/
resize.js
85 lines (68 loc) · 1.81 KB
/
resize.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
var Jimp = require("jimp");
var gutil = require('gulp-util');
var File = require('vinyl');
const PLUGIN_NAME = "gulp-jimp-resize";
function goGoGadgetImageResize (file, opt) {
var path = file.path;
var suffix = "";
if(opt.suffix) { var suffix = "-" + opt.suffix; }
var extension = path.substring(path.lastIndexOf('.'));
var name = path.substring(path.lastIndexOf('\\')+1, path.lastIndexOf('.')) + suffix + extension;
if(!opt.width && !opt.height) { //no resizing to be done
return new gutil.File({
path: name,
contents: file.contents
})
}
var result = new Promise(function(resolve, reject) {
Jimp.read(file.contents, function (err, image) {
if (err) {
reject(err);
return;
}
var width = Jimp.AUTO; var height = Jimp.AUTO;
if((opt.width && opt.width > image.bitmap.width) ||
(opt.height && opt.height > image.bitmap.height)) {
if(opt.upscale === false){
resolve(new gutil.File({
path: name,
contents: file.contents
}));
return;
}else{
gutil.log(PLUGIN_NAME, 'You are resizing an image to a larger size than the original');
}
}
if(opt.width) { width = opt.width; }
if(opt.height) { height = opt.height; }
image.resize(width, height).quality(100);
var mime;
switch(extension.toLowerCase()){
case ".jpg":
case ".jpeg":
case ".jpe":
mime = Jimp.MIME_JPEG;
break;
case ".png":
mime = Jimp.MIME_PNG;
break;
case ".bmp":
case ".dib":
mime = Jimp.MIME_BMP;
break;
}
var newImg = image.getBuffer(mime, function(err, buffer){
if (err) {
reject(err);
return;
}
resolve(new gutil.File({
path: name,
contents:buffer
}));
});
});
})
return result;
};
module.exports = goGoGadgetImageResize