You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/image.js

49 lines
980 B
JavaScript

11 years ago
'use strict';
11 years ago
var fs = require('fs'),
gm = require('gm').subClass({imageMagick: true});
11 years ago
var image = {};
image.resizeImage = function(path, extension, width, height, callback) {
function done(err, stdout, stderr) {
callback(err);
}
if(extension === '.gif') {
11 years ago
gm().in(path)
.in('-coalesce')
.in('-resize')
11 years ago
.in(width+'x'+height+'^')
11 years ago
.write(path, done);
11 years ago
} else {
11 years ago
gm(path)
11 years ago
.in('-resize')
.in(width+'x'+height+'^')
11 years ago
.gravity('Center')
.crop(width, height)
11 years ago
.write(path, done);
11 years ago
}
};
image.convertImageToPng = function(path, extension, callback) {
if(extension !== '.png') {
11 years ago
gm(path).toBuffer('png', function(err, buffer) {
if (err) {
11 years ago
return callback(err);
}
11 years ago
fs.writeFile(path, buffer, 'binary', callback);
11 years ago
});
} else {
callback();
}
};
image.convertImageToBase64 = function(path, callback) {
fs.readFile(path, function(err, data) {
callback(err, data ? data.toString('base64') : null);
});
11 years ago
};
11 years ago
module.exports = image;