mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-10 16:05:49 +01:00
Replace lwip library with jimp native lib for image manipulation
This commit is contained in:
@@ -30,10 +30,10 @@
|
|||||||
"express-session": "^1.8.2",
|
"express-session": "^1.8.2",
|
||||||
"gravatar": "^1.1.0",
|
"gravatar": "^1.1.0",
|
||||||
"heapdump": "^0.3.0",
|
"heapdump": "^0.3.0",
|
||||||
|
"jimp": "^0.2.5",
|
||||||
"less": "^2.0.0",
|
"less": "^2.0.0",
|
||||||
"logrotate-stream": "^0.2.3",
|
"logrotate-stream": "^0.2.3",
|
||||||
"lru-cache": "^2.6.1",
|
"lru-cache": "^2.6.1",
|
||||||
"lwip": "0.0.7",
|
|
||||||
"mime": "^1.3.4",
|
"mime": "^1.3.4",
|
||||||
"minimist": "^1.1.1",
|
"minimist": "^1.1.1",
|
||||||
"mkdirp": "~0.5.0",
|
"mkdirp": "~0.5.0",
|
||||||
|
|||||||
51
src/image.js
51
src/image.js
@@ -1,7 +1,8 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var fs = require('fs'),
|
var fs = require('fs'),
|
||||||
lwip = require('lwip'),
|
Jimp = require('jimp'),
|
||||||
|
async = require('async'),
|
||||||
plugins = require('./plugins');
|
plugins = require('./plugins');
|
||||||
|
|
||||||
var image = {};
|
var image = {};
|
||||||
@@ -17,15 +18,41 @@ image.resizeImage = function(path, extension, width, height, callback) {
|
|||||||
callback(err);
|
callback(err);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
tryOpen(path, function(err, image) {
|
new Jimp(path, function(err, image) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
image.batch()
|
var w = image.bitmap.width,
|
||||||
.cover(width, height)
|
h = image.bitmap.height,
|
||||||
.crop(width, height)
|
origRatio = w/h,
|
||||||
.writeFile(path, function(err) {
|
desiredRatio = width/height,
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
crop;
|
||||||
|
|
||||||
|
if (desiredRatio > origRatio) {
|
||||||
|
desiredRatio = 1/desiredRatio;
|
||||||
|
}
|
||||||
|
if (origRatio >= 1) {
|
||||||
|
y = 0; // height is the smaller dimension here
|
||||||
|
x = Math.floor((w/2) - (h * desiredRatio / 2));
|
||||||
|
crop = async.apply(image.crop.bind(image), x, y, h * desiredRatio, h);
|
||||||
|
} else {
|
||||||
|
x = 0; // width is the smaller dimension here
|
||||||
|
y = Math.floor(h/2 - (w * desiredRatio / 2));
|
||||||
|
crop = async.apply(image.crop.bind(image), x, y, w, w * desiredRatio);
|
||||||
|
}
|
||||||
|
|
||||||
|
async.waterfall([
|
||||||
|
crop,
|
||||||
|
function(image, next) {
|
||||||
|
image.resize(width, height, next);
|
||||||
|
},
|
||||||
|
function(image, next) {
|
||||||
|
image.write(path, next);
|
||||||
|
}
|
||||||
|
], function(err) {
|
||||||
callback(err);
|
callback(err);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -41,23 +68,15 @@ image.normalise = function(path, extension, callback) {
|
|||||||
callback(err);
|
callback(err);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
tryOpen(path, function(err, image) {
|
new Jimp(path, function(err, image) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
image.writeFile(path, 'png', callback);
|
image.write(path + '.png', callback);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function tryOpen(path, callback) {
|
|
||||||
try {
|
|
||||||
lwip.open(path, callback);
|
|
||||||
} catch (err) {
|
|
||||||
callback(err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
image.convertImageToBase64 = function(path, callback) {
|
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);
|
||||||
|
|||||||
Reference in New Issue
Block a user