From 37ad81a2b08ef43af1b0069d430655787459a6e8 Mon Sep 17 00:00:00 2001 From: Alpacatty Date: Sat, 30 May 2015 20:04:42 +0200 Subject: [PATCH 1/4] Swap out ImageMagick for lwip --- package.json | 2 +- src/image.js | 30 ++++++++++-------------------- 2 files changed, 11 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index adc006a2f7..9ffe720c59 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "daemon": "~1.1.0", "express": "^4.9.5", "express-session": "^1.8.2", - "gm": "1.17.0", + "lwip": "0.0.6", "gravatar": "^1.1.0", "heapdump": "^0.3.0", "less": "^2.0.0", diff --git a/src/image.js b/src/image.js index 0b2cea24cb..2fc391ed20 100644 --- a/src/image.js +++ b/src/image.js @@ -1,38 +1,28 @@ 'use strict'; var fs = require('fs'), - gm = require('gm').subClass({imageMagick: true}); + lwip = require('lwip'); var image = {}; image.resizeImage = function(path, extension, width, height, callback) { - function done(err, stdout, stderr) { - callback(err); - } - - if(extension === '.gif') { - gm().in(path) - .in('-coalesce') - .in('-resize') - .in(width+'x'+height+'^') - .write(path, done); - } else { - gm(path) - .in('-resize') - .in(width+'x'+height+'^') - .gravity('Center') + lwip.open(path, function(err, image) { + image.batch() + .cover(width, height) .crop(width, height) - .write(path, done); - } + .writeFile(path, function(err) { + callback(err) + }) + }); }; image.convertImageToPng = function(path, extension, callback) { if(extension !== '.png') { - gm(path).toBuffer('png', function(err, buffer) { + lwip.open(path, function(err, image) { if (err) { return callback(err); } - fs.writeFile(path, buffer, 'binary', callback); + image.writeFile(path, 'png', callback) }); } else { callback(); From 704104c3bf00f9d43a6b0a85e50e383884993b06 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Thu, 4 Jun 2015 12:23:14 -0400 Subject: [PATCH 2/4] fixed #3213 --- src/middleware/middleware.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/middleware/middleware.js b/src/middleware/middleware.js index 956963f76d..b1dd141c3c 100644 --- a/src/middleware/middleware.js +++ b/src/middleware/middleware.js @@ -274,7 +274,7 @@ middleware.renderHeader = function(req, res, callback) { templateValues.linkTags = results.tags.link; templateValues.isAdmin = results.user.isAdmin; templateValues.user = results.user; - templateValues.userJSON = JSON.stringify(results.user); + templateValues.userJSON = JSON.stringify(results.user).replace(/'/g, "\\'"); templateValues.customCSS = results.customCSS; templateValues.customJS = results.customJS; templateValues.maintenanceHeader = parseInt(meta.config.maintenanceMode, 10) === 1 && !results.isAdmin; From 88dd8b1d4dd49f5d84ede38d8b64a3a34e4d609e Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Thu, 4 Jun 2015 12:28:54 -0400 Subject: [PATCH 3/4] using new reset command in plugin incompatibility warning --- src/plugins/load.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/load.js b/src/plugins/load.js index 57fb8be4ab..d0f94c019a 100644 --- a/src/plugins/load.js +++ b/src/plugins/load.js @@ -57,7 +57,7 @@ module.exports = function(Plugins) { function display() { process.stdout.write('\n'); winston.warn('[plugins/' + pluginData.id + '] This plugin may not be compatible with your version of NodeBB. This may cause unintended behaviour or crashing.'); - winston.warn('[plugins/' + pluginData.id + '] In the event of an unresponsive NodeBB caused by this plugin, run ./nodebb reset plugin="' + pluginData.id + '".'); + winston.warn('[plugins/' + pluginData.id + '] In the event of an unresponsive NodeBB caused by this plugin, run ./nodebb reset -p ' + pluginData.id + '.'); process.stdout.write('\n'); } From e523ef7c17d25d7aae582679091f3a3129e77adf Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Thu, 4 Jun 2015 12:32:39 -0400 Subject: [PATCH 4/4] added plugin hooks so that imagemagick can still be called, as a plugin, for image manipulation tasks, falling back to using lwip --- src/image.js | 41 +++++++++++++++++++++++++++++------------ src/user/picture.js | 2 +- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/image.js b/src/image.js index 2fc391ed20..078b2d134d 100644 --- a/src/image.js +++ b/src/image.js @@ -1,31 +1,48 @@ 'use strict'; var fs = require('fs'), - lwip = require('lwip'); + lwip = require('lwip'), + plugins = require('./plugins'); var image = {}; image.resizeImage = function(path, extension, width, height, callback) { - lwip.open(path, function(err, image) { - image.batch() - .cover(width, height) - .crop(width, height) - .writeFile(path, function(err) { - callback(err) - }) + if (plugins.hasListeners('filter:image.resize')) { + plugins.fireHook('filter:image.resize', { + path: path, + extension: extension, + width: width, + height: height + }, function(err, data) { + callback(err); }); + } else { + lwip.open(path, function(err, image) { + image.batch() + .cover(width, height) + .crop(width, height) + .writeFile(path, function(err) { + callback(err) + }) + }); + } }; -image.convertImageToPng = function(path, extension, callback) { - if(extension !== '.png') { +image.normalise = function(path, extension, callback) { + if (plugins.hasListeners('filter:image.normalise')) { + plugins.fireHook('filter:image.normalise', { + path: path, + extension: extension + }, function(err, data) { + callback(err); + }); + } else { lwip.open(path, function(err, image) { if (err) { return callback(err); } image.writeFile(path, 'png', callback) }); - } else { - callback(); } }; diff --git a/src/user/picture.js b/src/user/picture.js index 8f030091a9..ada87589d6 100644 --- a/src/user/picture.js +++ b/src/user/picture.js @@ -41,7 +41,7 @@ module.exports = function(User) { }, function(next) { if (convertToPNG) { - image.convertImageToPng(picture.path, extension, next); + image.normalise(picture.path, extension, next); } else { next(); }