-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
40 lines (32 loc) · 1.02 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
var Jimp = require("jimp");
var loaderUtils = require("loader-utils");
var Datauri = require('datauri');
var path = require('path');
module.exports = function (source, map) {
this.cacheable();
var callback = this.async();
var originalImage = path.resolve(this.resource)
this.addDependency(originalImage);
var config = {
publicPath: false,
name: "[name]_original.[ext]"
};
Jimp.read(originalImage).then(function (image) {
var extension = loaderUtils.interpolateName(this, '.[ext]', {});
var size = {
width: image.bitmap.width,
height: image.bitmap.height
};
image.scaleToFit(42, 42) // resize
.quality(60) // set JPEG quality
image.getBuffer(Jimp.MIME_JPEG, function (err, result) {
var dataUri = new Datauri().format(extension, result).content;
callback(null, "module.exports = " + JSON.stringify({
dataUri: dataUri,
size: size
}) + ";", map);
}.bind(this));
}.bind(this)).catch(function (err) {
console.log(err);
});
}