mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-10 16:05:49 +01:00
closes #1130
This commit is contained in:
@@ -23,7 +23,7 @@
|
|||||||
"less-middleware": "0.1.12",
|
"less-middleware": "0.1.12",
|
||||||
"marked": "0.2.8",
|
"marked": "0.2.8",
|
||||||
"async": "~0.2.8",
|
"async": "~0.2.8",
|
||||||
"node-imagemagick": "0.1.8",
|
"gm": "1.14.2",
|
||||||
"gravatar": "1.0.6",
|
"gravatar": "1.0.6",
|
||||||
"nconf": "~0.6.7",
|
"nconf": "~0.6.7",
|
||||||
"sitemap": "~0.7.1",
|
"sitemap": "~0.7.1",
|
||||||
|
|||||||
36
src/image.js
36
src/image.js
@@ -1,6 +1,7 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
var fs = require('fs'),
|
var fs = require('fs'),
|
||||||
imagemagick = require('node-imagemagick'),
|
gm = require('gm').subClass({imageMagick: true}),
|
||||||
meta = require('./meta');
|
meta = require('./meta');
|
||||||
|
|
||||||
var image = {};
|
var image = {};
|
||||||
@@ -11,35 +12,26 @@ image.resizeImage = function(path, extension, width, height, callback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(extension === '.gif') {
|
if(extension === '.gif') {
|
||||||
imagemagick.convert([
|
gm().in(path)
|
||||||
path,
|
.in('-coalesce')
|
||||||
'-coalesce',
|
.in('-resize')
|
||||||
'-repage',
|
.in(width+'x'+height)
|
||||||
'0x0',
|
.write(path, done);
|
||||||
'-crop',
|
|
||||||
width+'x'+height+'+0+0',
|
|
||||||
'+repage',
|
|
||||||
path
|
|
||||||
], done);
|
|
||||||
} else {
|
} else {
|
||||||
imagemagick.crop({
|
gm(path)
|
||||||
srcPath: path,
|
.crop(width, height, 0, 0)
|
||||||
dstPath: path,
|
.write(path, done);
|
||||||
width: width,
|
|
||||||
height: height
|
|
||||||
}, done);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
image.convertImageToPng = function(path, extension, callback) {
|
image.convertImageToPng = function(path, extension, callback) {
|
||||||
var convertToPNG = parseInt(meta.config['profile:convertProfileImageToPNG'], 10);
|
var convertToPNG = parseInt(meta.config['profile:convertProfileImageToPNG'], 10);
|
||||||
if(convertToPNG && extension !== '.png') {
|
if(convertToPNG && extension !== '.png') {
|
||||||
imagemagick.convert([path, 'png:-'], function(err, stdout) {
|
gm(path).toBuffer('png', function(err, buffer) {
|
||||||
if(err) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
fs.writeFile(path, buffer, 'binary', callback);
|
||||||
fs.writeFile(path, stdout, 'binary', callback);
|
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
callback();
|
callback();
|
||||||
@@ -50,6 +42,6 @@ image.convertImageToBase64 = function(path, callback) {
|
|||||||
fs.readFile(path, function(err, data) {
|
fs.readFile(path, function(err, data) {
|
||||||
callback(err, data ? data.toString('base64') : null);
|
callback(err, data ? data.toString('base64') : null);
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
module.exports = image;
|
module.exports = image;
|
||||||
@@ -116,6 +116,7 @@ var fs = require('fs'),
|
|||||||
|
|
||||||
var uploadSize = parseInt(meta.config.maximumProfileImageSize, 10) || 256;
|
var uploadSize = parseInt(meta.config.maximumProfileImageSize, 10) || 256;
|
||||||
if (req.files.userPhoto.size > uploadSize * 1024) {
|
if (req.files.userPhoto.size > uploadSize * 1024) {
|
||||||
|
fs.unlink(req.files.userPhoto.path);
|
||||||
return res.send({
|
return res.send({
|
||||||
error: 'Images must be smaller than ' + uploadSize + ' kb!'
|
error: 'Images must be smaller than ' + uploadSize + ' kb!'
|
||||||
});
|
});
|
||||||
@@ -123,6 +124,7 @@ var fs = require('fs'),
|
|||||||
|
|
||||||
var allowedTypes = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'];
|
var allowedTypes = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'];
|
||||||
if (allowedTypes.indexOf(req.files.userPhoto.type) === -1) {
|
if (allowedTypes.indexOf(req.files.userPhoto.type) === -1) {
|
||||||
|
fs.unlink(req.files.userPhoto.path);
|
||||||
return res.send({
|
return res.send({
|
||||||
error: 'Allowed image types are png, jpg and gif!'
|
error: 'Allowed image types are png, jpg and gif!'
|
||||||
});
|
});
|
||||||
@@ -130,6 +132,7 @@ var fs = require('fs'),
|
|||||||
|
|
||||||
var extension = path.extname(req.files.userPhoto.name);
|
var extension = path.extname(req.files.userPhoto.name);
|
||||||
if (!extension) {
|
if (!extension) {
|
||||||
|
fs.unlink(req.files.userPhoto.path);
|
||||||
return res.send({
|
return res.send({
|
||||||
error: 'Error uploading file! Error : Invalid extension!'
|
error: 'Error uploading file! Error : Invalid extension!'
|
||||||
});
|
});
|
||||||
@@ -184,6 +187,7 @@ var fs = require('fs'),
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(err) {
|
if(err) {
|
||||||
|
fs.unlink(req.files.userPhoto.path);
|
||||||
return res.send({error:err.message});
|
return res.send({error:err.message});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user