From f6f7959d28f2663877962e31c6e5b2d2735b3e2c Mon Sep 17 00:00:00 2001 From: psychobunny Date: Sun, 8 Dec 2013 11:25:15 -0500 Subject: [PATCH] convert profile images to static png if setting is checked in control panel, closes #562 --- src/routes/user.js | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/routes/user.js b/src/routes/user.js index 3eb0acd6b8..a978e50eee 100644 --- a/src/routes/user.js +++ b/src/routes/user.js @@ -102,7 +102,7 @@ var fs = require('fs'), if (!req.user) return res.redirect('/403'); - var uploadSize = meta.config.maximumProfileImageSize || 256; + var uploadSize = parseInt(meta.config.maximumProfileImageSize, 10) || 256; if (req.files.userPhoto.size > uploadSize * 1024) { res.send({ @@ -147,18 +147,21 @@ var fs = require('fs'), return; } - var filename = uid + '-profileimg' + extension; + var convertToPNG = parseInt(meta.config['profile:convertProfileImageToPNG'], 10); + + var filename = uid + '-profileimg' + (convertToPNG ? '.png' : extension); var uploadPath = path.join(nconf.get('base_dir'), nconf.get('upload_path'), filename); winston.info('Attempting upload to: ' + uploadPath); var is = fs.createReadStream(tempPath); var os = fs.createWriteStream(uploadPath); + var im = require('node-imagemagick'); is.on('end', function () { fs.unlinkSync(tempPath); - require('node-imagemagick').crop({ + im.crop({ srcPath: uploadPath, dstPath: uploadPath, width: 128, @@ -177,6 +180,22 @@ var fs = require('fs'), user.setUserField(uid, 'uploadedpicture', imageUrl); user.setUserField(uid, 'picture', imageUrl); + if (convertToPNG) { + im.convert([uploadPath, 'png:-'], + function(err, stdout){ + if (err) { + winston.err(err); + res.send({ + error: 'Unable to convert image to PNG.' + }); + return; + } + + fs.writeFileSync(uploadPath, stdout, 'binary'); + }); + } + + res.json({ path: imageUrl });