This commit is contained in:
Baris Soner Usakli
2014-03-05 23:00:27 -05:00
parent 38145abae1
commit 096f352c82
3 changed files with 19 additions and 23 deletions

View File

@@ -1,6 +1,7 @@
'use strict';
var fs = require('fs'),
imagemagick = require('node-imagemagick'),
gm = require('gm').subClass({imageMagick: true}),
meta = require('./meta');
var image = {};
@@ -11,35 +12,26 @@ image.resizeImage = function(path, extension, width, height, callback) {
}
if(extension === '.gif') {
imagemagick.convert([
path,
'-coalesce',
'-repage',
'0x0',
'-crop',
width+'x'+height+'+0+0',
'+repage',
path
], done);
gm().in(path)
.in('-coalesce')
.in('-resize')
.in(width+'x'+height)
.write(path, done);
} else {
imagemagick.crop({
srcPath: path,
dstPath: path,
width: width,
height: height
}, done);
gm(path)
.crop(width, height, 0, 0)
.write(path, done);
}
};
image.convertImageToPng = function(path, extension, callback) {
var convertToPNG = parseInt(meta.config['profile:convertProfileImageToPNG'], 10);
if(convertToPNG && extension !== '.png') {
imagemagick.convert([path, 'png:-'], function(err, stdout) {
if(err) {
gm(path).toBuffer('png', function(err, buffer) {
if (err) {
return callback(err);
}
fs.writeFile(path, stdout, 'binary', callback);
fs.writeFile(path, buffer, 'binary', callback);
});
} else {
callback();
@@ -50,6 +42,6 @@ image.convertImageToBase64 = function(path, callback) {
fs.readFile(path, function(err, data) {
callback(err, data ? data.toString('base64') : null);
});
}
};
module.exports = image;