-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (49 loc) · 1.17 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
var gm = require("gm");
var through = require("through2");
var _ = require("lodash");
var PluginError = require('plugin-error');
var PLUGIN_NAME = "gulp-unretina";
module.exports = function imageResizer(options) {
options = _.defaults(options || {}, {
quality : 1,
suffix: "@2x"
});
var _gm = gm;
return through.obj(function (file, enc, done) {
if (file.isNull())
{
return done(null, file);
}
if (file.isStream())
{
return done(new PluginError(PLUGIN_NAME, "Streaming not supported"));
}
var gmFile = _gm(file.contents, file.path);
gmFile.size(function (err, size) {
if (err)
{
return done(new PluginError(PLUGIN_NAME, err));
}
else
{
gmFile.resize(Math.ceil(size.width / 2), Math.ceil(size.height / 2));
gmFile.quality(Math.floor(options.quality * 100));
gmFile.toBuffer(function (err, buffer) {
if (err)
{
return done(new PluginError(PLUGIN_NAME, err));
}
else
{
if (options.suffix !== false || options.suffix !== "")
{
file.path = file.path.replace(options.suffix + ".", ".");
}
file.contents = buffer;
done(null, file);
}
});
}
});
});
};