');
}
});
};
diff --git a/src/controllers/uploads.js b/src/controllers/uploads.js
index 59253c5666..5a6d814658 100644
--- a/src/controllers/uploads.js
+++ b/src/controllers/uploads.js
@@ -14,7 +14,7 @@ var image = require('../image');
var uploadsController = {};
-uploadsController.upload = function(req, res, filesIterator, next) {
+uploadsController.upload = function(req, res, filesIterator) {
var files = req.files.files;
if (!req.user && meta.config.allowGuestUploads !== '1') {
@@ -63,6 +63,30 @@ uploadsController.uploadPost = function(req, res, next) {
return next(new Error('[[error:uploads-are-disabled]]'));
}
uploadFile(req.uid, uploadedFile, next);
+ },
+ function(fileObj, next) {
+ if (!isImage || parseInt(meta.config.maximumImageWidth, 10) === 0) {
+ // Not an image, or resizing disabled. No need to resize.
+ return next(null, fileObj);
+ }
+
+ var fullPath = path.join(nconf.get('base_dir'), nconf.get('upload_path'), '..', fileObj.url),
+ parsedPath = path.parse(fullPath);
+
+ image.resizeImage({
+ path: fullPath,
+ target: path.join(parsedPath.dir, parsedPath.name + '-resized' + parsedPath.ext),
+ extension: parsedPath.ext,
+ width: parseInt(meta.config.maximumImageWidth, 10) || 760
+ }, function(err) {
+ // Return the resized version to the composer/postData
+ var parsedUrl = path.parse(fileObj.url);
+ delete parsedUrl.base;
+ parsedUrl.name = parsedUrl.name + '-resized';
+ fileObj.url = path.format(parsedUrl);
+
+ next(err, fileObj);
+ });
}
], next);
}, next);
diff --git a/src/image.js b/src/image.js
index f4b85acd4d..fccdc0f5ad 100644
--- a/src/image.js
+++ b/src/image.js
@@ -11,6 +11,7 @@ image.resizeImage = function(data, callback) {
if (plugins.hasListeners('filter:image.resize')) {
plugins.fireHook('filter:image.resize', {
path: data.path,
+ target: data.target,
extension: data.extension,
width: data.width,
height: data.height
@@ -26,28 +27,42 @@ image.resizeImage = function(data, callback) {
var w = image.bitmap.width,
h = image.bitmap.height,
origRatio = w/h,
- desiredRatio = data.width/data.height,
+ desiredRatio = data.width && data.height ? data.width/data.height : origRatio,
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);
+ if (origRatio !== desiredRatio) {
+ 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);
+ }
} 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);
+ // Simple resize given either width, height, or both
+ crop = async.apply(setImmediate);
}
async.waterfall([
crop,
- function(image, next) {
- image.resize(data.width, data.height, next);
+ function(_image, next) {
+ if (typeof _image === 'function' && !next) {
+ next = _image;
+ _image = image;
+ }
+
+ if ((data.width && data.height) || (w > data.width) || (h > data.height)) {
+ _image.resize(data.width || Jimp.AUTO, data.height || Jimp.AUTO, next);
+ } else {
+ next(null, image);
+ }
},
function(image, next) {
image.write(data.target || data.path, next);
diff --git a/src/views/admin/settings/uploads.tpl b/src/views/admin/settings/uploads.tpl
index 4881f8dcd7..4358dee47e 100644
--- a/src/views/admin/settings/uploads.tpl
+++ b/src/views/admin/settings/uploads.tpl
@@ -28,8 +28,19 @@
+
+