forked from scalableminds/gulp-image-resize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
163 lines (133 loc) · 4.4 KB
/
index.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* grunt-image-resize
* https://github.com/scalableminds/gulp-image-resize
*
* Copyright (c) 2014 Norman Rzepka
* Licensed under the MIT license.
*/
var gm = require("gulp-gm");
var async = require("async");
var _ = require("lodash");
module.exports = function imageResizer(_options) {
_options = _.defaults(_options, {
overwrite : true,
upscale : false,
crop : false,
gravity : "Center",
quality : 1,
compress : null,
noProfile : false,
sharpen : false,
imageMagick : false,
format : null,
flatten : false,
interlace : false,
percentage : null,
cover : false,
extent : false,
density : [72, 72],
resample : false,
colorspace : "rgb"
});
return gm(function(gmfile, done) {
async.waterfall([
function (callback) {
gmfile.size(callback);
},
function (size, callback) {
var options = JSON.parse(JSON.stringify(_options)); // fix: we must make a copy, because we will change it!
if (options.filter != null) {
gmfile = gmfile.filter(options.filter);
}
if (options.height != null || options.width != null) {
// if upscale is not requested, restrict size
if(!options.upscale){
if (!isNaN(options.width)) {
options.width = Math.min(options.width, size.width);
}
if (!isNaN(options.height)) {
options.height = Math.min(options.height, size.height);
}
}
// if one dimension is not set - we fill it proportionally
if (!options.height) {
if (options.crop) {
options.height = size.height;
} else {
options.height = Math.ceil((options.width / size.width) * size.height);
}
}
if (!options.width) {
if (options.crop) {
options.width = size.width;
} else {
options.width = Math.ceil((options.height / size.height) * size.width);
}
}
if (options.crop) {
gmfile = gmfile
.resize(options.width, options.height, "^")
.gravity(options.gravity)
.crop(options.width, options.height);
} else if (options.cover) {
gmfile = gmfile
.resize(options.width, options.height, "^");
} else if (options.extent) {
gmfile = gmfile
.resize(options.width, options.height, "")
//.gravity(options.gravity)
//.extent(options.width, ((options.height * 2) + 2), "");
} else {
gmfile = gmfile
.resize(options.width, options.height);
}
} else if (options.percentage) {
gmfile = gmfile
.resize(options.percentage, null, '%');
}
if (options.format) {
gmfile = gmfile
.setFormat(options.format);
}
if (options.compress != null) {
gmfile = gmfile.compress(options.compress);
} else if (options.quality !== 1) {
gmfile = gmfile.quality(Math.floor(options.quality * 100));
}
if (options.samplingFactor != null) {
gmfile = gmfile
.samplingFactor(options.samplingFactor[0], options.samplingFactor[1]);
}
if (options.sharpen) {
options.sharpen = (typeof options.sharpen === 'string') ? options.sharpen : '1.5x1+0.7+0.02';
gmfile = gmfile.unsharp(options.sharpen);
}
if (options.flatten) {
gmfile = gmfile.flatten();
}
if (options.interlace) {
gmfile = gmfile.interlace('Line');
}
if (options.background) {
gmfile = gmfile.background(options.background);
}
if (options.noProfile) {
gmfile = gmfile.noProfile();
}
if (options.density) {
gmfile = gmfile
.density(options.density[0], options.density[1]);
}
if (options.resample) {
gmfile = gmfile
.resample(options.resample[0], options.resample[1]);
}
if (options.colorspace) {
gmfile = gmfile
.colorspace(options.colorspace);
}
callback(null, gmfile);
}
], done);
}, { imageMagick : _options.imageMagick });
};