Files
NodeBB/src/image.js

119 lines
2.8 KiB
JavaScript
Raw Normal View History

2014-03-05 23:00:27 -05:00
'use strict';
2014-02-09 00:34:05 -05:00
2016-08-30 13:10:48 +03:00
var fs = require('fs');
var Jimp = require('jimp');
var async = require('async');
var plugins = require('./plugins');
2014-02-09 00:34:05 -05:00
2016-08-30 13:10:48 +03:00
var image = module.exports;
2014-02-09 00:34:05 -05:00
image.resizeImage = function (data, callback) {
if (plugins.hasListeners('filter:image.resize')) {
plugins.fireHook('filter:image.resize', {
path: data.path,
2016-04-20 13:58:25 -04:00
target: data.target,
extension: data.extension,
width: data.width,
height: data.height
}, function (err) {
callback(err);
2015-05-30 20:04:42 +02:00
});
} else {
new Jimp(data.path, function (err, image) {
2015-08-18 15:17:07 -04:00
if (err) {
return callback(err);
}
var w = image.bitmap.width,
h = image.bitmap.height,
2016-10-13 11:42:29 +02:00
origRatio = w / h,
desiredRatio = data.width && data.height ? data.width / data.height : origRatio,
x = 0,
y = 0,
crop;
2016-04-20 13:58:25 -04:00
if (origRatio !== desiredRatio) {
if (desiredRatio > origRatio) {
2016-10-13 11:42:29 +02:00
desiredRatio = 1 / desiredRatio;
2016-04-20 13:58:25 -04:00
}
if (origRatio >= 1) {
y = 0; // height is the smaller dimension here
2016-10-13 11:42:29 +02:00
x = Math.floor((w / 2) - (h * desiredRatio / 2));
2016-04-20 13:58:25 -04:00
crop = async.apply(image.crop.bind(image), x, y, h * desiredRatio, h);
} else {
x = 0; // width is the smaller dimension here
2016-10-13 11:42:29 +02:00
y = Math.floor(h / 2 - (w * desiredRatio / 2));
2016-04-20 13:58:25 -04:00
crop = async.apply(image.crop.bind(image), x, y, w, w * desiredRatio);
}
} else {
2016-04-20 13:58:25 -04:00
// Simple resize given either width, height, or both
crop = async.apply(setImmediate);
}
async.waterfall([
crop,
function (_image, next) {
2016-04-20 13:58:25 -04:00
if (typeof _image === 'function' && !next) {
next = _image;
_image = image;
}
if ((data.width && data.height) || (w > data.width) || (h > data.height)) {
_image.resize(data.width || Jimp.AUTO, data.height || Jimp.AUTO, next);
} else {
next(null, image);
}
},
function (image, next) {
2017-02-03 18:29:09 -07:00
if (data.write === false) {
return next();
}
image.write(data.target || data.path, next);
}
], function (err) {
callback(err);
});
});
}
2014-02-09 00:34:05 -05:00
};
image.normalise = function (path, extension, callback) {
if (plugins.hasListeners('filter:image.normalise')) {
plugins.fireHook('filter:image.normalise', {
path: path,
extension: extension
}, function (err) {
callback(err);
});
} else {
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);
}
image.write(path + '.png', function (err) {
2016-03-09 14:19:49 +02:00
callback(err);
});
2014-02-09 00:34:05 -05:00
});
}
};
image.size = function (path, callback) {
if (plugins.hasListeners('filter:image.size')) {
plugins.fireHook('filter:image.size', {
path: path,
}, function (err, image) {
callback(err, image);
});
} else {
new Jimp(path, function (err, data) {
callback(err, data ? data.bitmap : null);
});
}
2016-08-30 13:10:48 +03:00
};
2016-05-01 12:44:43 +03:00
image.convertImageToBase64 = function (path, callback) {
fs.readFile(path, function (err, data) {
2014-02-09 00:34:05 -05:00
callback(err, data ? data.toString('base64') : null);
});
2014-03-05 23:00:27 -05:00
};