Files
NodeBB/src/image.js

49 lines
980 B
JavaScript
Raw Normal View History

2014-03-05 23:00:27 -05:00
'use strict';
2014-02-09 00:34:05 -05:00
var fs = require('fs'),
gm = require('gm').subClass({imageMagick: true});
2014-02-09 00:34:05 -05:00
var image = {};
image.resizeImage = function(path, extension, width, height, callback) {
function done(err, stdout, stderr) {
callback(err);
}
if(extension === '.gif') {
2014-03-05 23:00:27 -05:00
gm().in(path)
.in('-coalesce')
.in('-resize')
2014-05-10 23:56:45 -04:00
.in(width+'x'+height+'^')
2014-03-05 23:00:27 -05:00
.write(path, done);
2014-02-09 00:34:05 -05:00
} else {
2014-03-05 23:00:27 -05:00
gm(path)
2014-05-10 23:56:45 -04:00
.in('-resize')
.in(width+'x'+height+'^')
2014-04-27 17:40:39 -04:00
.gravity('Center')
.crop(width, height)
2014-03-05 23:00:27 -05:00
.write(path, done);
2014-02-09 00:34:05 -05:00
}
};
image.convertImageToPng = function(path, extension, callback) {
if(extension !== '.png') {
2014-03-05 23:00:27 -05:00
gm(path).toBuffer('png', function(err, buffer) {
if (err) {
2014-02-09 00:34:05 -05:00
return callback(err);
}
2014-03-05 23:00:27 -05:00
fs.writeFile(path, buffer, 'binary', callback);
2014-02-09 00:34:05 -05:00
});
} else {
callback();
}
};
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;