2014-03-05 23:00:27 -05:00
|
|
|
'use strict';
|
2014-02-09 00:34:05 -05:00
|
|
|
|
|
|
|
|
var fs = require('fs'),
|
2015-09-21 15:36:08 -04:00
|
|
|
Jimp = require('jimp'),
|
|
|
|
|
async = require('async'),
|
2015-06-04 12:32:39 -04:00
|
|
|
plugins = require('./plugins');
|
2014-02-09 00:34:05 -05:00
|
|
|
|
|
|
|
|
var image = {};
|
|
|
|
|
|
|
|
|
|
image.resizeImage = function(path, extension, width, height, callback) {
|
2015-06-04 12:32:39 -04:00
|
|
|
if (plugins.hasListeners('filter:image.resize')) {
|
|
|
|
|
plugins.fireHook('filter:image.resize', {
|
|
|
|
|
path: path,
|
|
|
|
|
extension: extension,
|
|
|
|
|
width: width,
|
|
|
|
|
height: height
|
|
|
|
|
}, function(err, data) {
|
|
|
|
|
callback(err);
|
2015-05-30 20:04:42 +02:00
|
|
|
});
|
2015-06-04 12:32:39 -04:00
|
|
|
} else {
|
2015-09-21 15:36:08 -04:00
|
|
|
new Jimp(path, function(err, image) {
|
2015-08-18 15:17:07 -04:00
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-21 15:36:08 -04:00
|
|
|
var w = image.bitmap.width,
|
|
|
|
|
h = image.bitmap.height,
|
|
|
|
|
origRatio = w/h,
|
|
|
|
|
desiredRatio = width/height,
|
|
|
|
|
x = 0,
|
|
|
|
|
y = 0,
|
|
|
|
|
crop;
|
|
|
|
|
|
|
|
|
|
if (desiredRatio > origRatio) {
|
|
|
|
|
desiredRatio = 1/desiredRatio;
|
|
|
|
|
}
|
|
|
|
|
if (origRatio >= 1) {
|
|
|
|
|
y = 0; // height is the smaller dimension here
|
|
|
|
|
x = Math.floor((w/2) - (h * desiredRatio / 2));
|
|
|
|
|
crop = async.apply(image.crop.bind(image), x, y, h * desiredRatio, h);
|
|
|
|
|
} else {
|
|
|
|
|
x = 0; // width is the smaller dimension here
|
|
|
|
|
y = Math.floor(h/2 - (w * desiredRatio / 2));
|
|
|
|
|
crop = async.apply(image.crop.bind(image), x, y, w, w * desiredRatio);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
|
crop,
|
|
|
|
|
function(image, next) {
|
|
|
|
|
image.resize(width, height, next);
|
|
|
|
|
},
|
|
|
|
|
function(image, next) {
|
|
|
|
|
image.write(path, next);
|
|
|
|
|
}
|
|
|
|
|
], function(err) {
|
|
|
|
|
callback(err);
|
2015-06-04 12:32:39 -04:00
|
|
|
});
|
2015-09-21 15:36:08 -04:00
|
|
|
});
|
2015-06-04 12:32:39 -04:00
|
|
|
}
|
2014-02-09 00:34:05 -05:00
|
|
|
};
|
|
|
|
|
|
2015-06-04 12:32:39 -04:00
|
|
|
image.normalise = function(path, extension, callback) {
|
|
|
|
|
if (plugins.hasListeners('filter:image.normalise')) {
|
|
|
|
|
plugins.fireHook('filter:image.normalise', {
|
|
|
|
|
path: path,
|
|
|
|
|
extension: extension
|
|
|
|
|
}, function(err, data) {
|
|
|
|
|
callback(err);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2015-09-21 15:36:08 -04:00
|
|
|
new Jimp(path, function(err, image) {
|
2014-03-05 23:00:27 -05:00
|
|
|
if (err) {
|
2014-02-09 00:34:05 -05:00
|
|
|
return callback(err);
|
|
|
|
|
}
|
2015-09-21 15:36:08 -04:00
|
|
|
image.write(path + '.png', callback);
|
2014-02-09 00:34:05 -05:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
image.convertImageToBase64 = function(path, callback) {
|
|
|
|
|
fs.readFile(path, function(err, data) {
|
|
|
|
|
callback(err, data ? data.toString('base64') : null);
|
|
|
|
|
});
|
2014-03-05 23:00:27 -05:00
|
|
|
};
|
2014-02-09 00:34:05 -05:00
|
|
|
|
2014-04-10 20:31:57 +01:00
|
|
|
module.exports = image;
|