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",
|
||||
"marked": "0.2.8",
|
||||
"async": "~0.2.8",
|
||||
"node-imagemagick": "0.1.8",
|
||||
"gm": "1.14.2",
|
||||
"gravatar": "1.0.6",
|
||||
"nconf": "~0.6.7",
|
||||
"sitemap": "~0.7.1",
|
||||
|
||||
34
src/image.js
34
src/image.js
@@ -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) {
|
||||
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;
|
||||
@@ -116,6 +116,7 @@ var fs = require('fs'),
|
||||
|
||||
var uploadSize = parseInt(meta.config.maximumProfileImageSize, 10) || 256;
|
||||
if (req.files.userPhoto.size > uploadSize * 1024) {
|
||||
fs.unlink(req.files.userPhoto.path);
|
||||
return res.send({
|
||||
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'];
|
||||
if (allowedTypes.indexOf(req.files.userPhoto.type) === -1) {
|
||||
fs.unlink(req.files.userPhoto.path);
|
||||
return res.send({
|
||||
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);
|
||||
if (!extension) {
|
||||
fs.unlink(req.files.userPhoto.path);
|
||||
return res.send({
|
||||
error: 'Error uploading file! Error : Invalid extension!'
|
||||
});
|
||||
@@ -184,6 +187,7 @@ var fs = require('fs'),
|
||||
}
|
||||
|
||||
if(err) {
|
||||
fs.unlink(req.files.userPhoto.path);
|
||||
return res.send({error:err.message});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user