Skip to content

Node.js based forum software built for the modern web

add threshold for image resizing

Barış Soner Uşaklı9c03881d5ae7
diff --git a/install/data/defaults.json b/install/data/defaults.json
index e41f0dc966..beacb2f00d 100644
--- a/install/data/defaults.json
+++ b/install/data/defaults.json
@@ -33,10 +33,11 @@
"allowUserHomePage": 1,
"allowMultipleBadges": 0,
"maximumFileSize": 2048,
- "maximumImageWidth": 760,
+ "resizeImageWidthThreshold": 2000,
+ "resizeImageWidth": 760,
"rejectImageWidth": 5000,
"rejectImageHeight": 5000,
- "resizeImageQuality": 60,
+ "resizeImageQuality": 80,
"topicThumbSize": 120,
"minimumTitleLength": 3,
"maximumTitleLength": 255,
diff --git a/public/language/en-GB/admin/settings/uploads.json b/public/language/en-GB/admin/settings/uploads.json
index 83a0c73428..10e53d7e32 100644
--- a/public/language/en-GB/admin/settings/uploads.json
+++ b/public/language/en-GB/admin/settings/uploads.json
@@ -4,8 +4,10 @@
"private": "Make uploaded files private",
"private-extensions": "File extensions to make private",
"private-uploads-extensions-help": "Enter comma-separated list of file extensions to make private here (e.g. <code>pdf,xls,doc</code>). An empty list means all files are private.",
- "max-image-width": "Resize images down to specified width (in pixels)",
- "max-image-width-help": "(in pixels, default: 760 pixels, set to 0 to disable)",
+ "resize-image-width-threshold": "Resize images if they are wider than specified width",
+ "resize-image-width-threshold-help": "(in pixels, default: 1520 pixels, set to 0 to disable)",
+ "resize-image-width": "Resize images down to specified width",
+ "resize-image-width-help": "(in pixels, default: 760 pixels, set to 0 to disable)",
"resize-image-quality": "Quality to use when resizing images",
"resize-image-quality-help": "Use a lower quality setting to reduce the file size of resized images.",
"max-file-size": "Maximum File Size (in KiB)",
diff --git a/src/controllers/uploads.js b/src/controllers/uploads.js
index f4313fed0a..5ea85d7b6b 100644
--- a/src/controllers/uploads.js
+++ b/src/controllers/uploads.js
@@ -71,7 +71,7 @@ function uploadAsImage(req, uploadedFile, callback) {
uploadsController.uploadFile(req.uid, uploadedFile, next);
},
function (fileObj, next) {
- if (meta.config.maximumImageWidth === 0) {
+ if (meta.config.resizeImageWidth === 0 || meta.config.resizeImageWidthThreshold === 0) {
return next(null, fileObj);
}
@@ -112,14 +112,14 @@ function resizeImage(fileObj, callback) {
image.size(fileObj.path, next);
},
function (imageData, next) {
- if (imageData.width < meta.config.maximumImageWidth) {
+ if (imageData.width < meta.config.resizeImageWidthThreshold || meta.config.resizeImageWidth > meta.config.resizeImageWidthThreshold) {
return callback(null, fileObj);
}
image.resizeImage({
path: fileObj.path,
target: file.appendToFileName(fileObj.path, '-resized'),
- width: meta.config.maximumImageWidth,
+ width: meta.config.resizeImageWidth,
quality: meta.config.resizeImageQuality,
}, next);
},
diff --git a/src/upgrades/1.11.0/resize_image_width.js b/src/upgrades/1.11.0/resize_image_width.js
new file mode 100644
index 0000000000..60d98f9991
--- /dev/null
+++ b/src/upgrades/1.11.0/resize_image_width.js
@@ -0,0 +1,24 @@
+'use strict';
+
+var db = require('../../database');
+
+var async = require('async');
+
+module.exports = {
+ name: 'Rename maximumImageWidth to resizeImageWidth',
+ timestamp: Date.UTC(2018, 9, 24),
+ method: function (callback) {
+ const meta = require('../../meta');
+ async.waterfall([
+ function (next) {
+ meta.configs.get('maximumImageWidth', next);
+ },
+ function (value, next) {
+ meta.configs.set('resizeImageWidth', value, next);
+ },
+ function (next) {
+ db.deleteObjectField('config', 'maximumImageWidth', next);
+ },
+ ], callback);
+ },
+};
diff --git a/src/views/admin/settings/post.tpl b/src/views/admin/settings/post.tpl
index c1bf5a780d..b95e13fc7d 100644
--- a/src/views/admin/settings/post.tpl
+++ b/src/views/admin/settings/post.tpl
@@ -28,29 +28,29 @@
<div class="col-sm-2 col-xs-12 settings-header">[[admin/settings/post:length]]</div>
<div class="col-sm-10 col-xs-12">
<form>
- <div class="row">
- <div class="col-xs-6">
- <div class="form-group">
- <label for="minimumTitleLength">[[admin/settings/post:restrictions.min-title-length]]</label>
- <input id="minimumTitleLength" type="text" class="form-control" value="3" data-field="minimumTitleLength">
- </div>
- <div class="form-group">
- <label for="maximumTitleLength">[[admin/settings/post:restrictions.max-title-length]]</label>
- <input id="maximumTitleLength" type="text" class="form-control" value="255" data-field="maximumTitleLength">
- </div>
- </div>
- <div class="col-xs-6">
- <div class="form-group">
- <label for="minimumPostLength">[[admin/settings/post:restrictions.min-post-length]]</label>
- <input id="minimumPostLength" type="text" class="form-control" value="8" data-field="minimumPostLength">
- </div>
- <div class="form-group">
- <label for="maximumPostLength">[[admin/settings/post:restrictions.max-post-length]]</label>
- <input id="maximumPostLength" type="text" class="form-control" value="32767" data-field="maximumPostLength">
+ <div class="row">
+ <div class="col-xs-6">
+ <div class="form-group">
+ <label for="minimumTitleLength">[[admin/settings/post:restrictions.min-title-length]]</label>
+ <input id="minimumTitleLength" type="text" class="form-control" value="3" data-field="minimumTitleLength">
+ </div>
+ <div class="form-group">
+ <label for="maximumTitleLength">[[admin/settings/post:restrictions.max-title-length]]</label>
+ <input id="maximumTitleLength" type="text" class="form-control" value="255" data-field="maximumTitleLength">
+ </div>
+ </div>
+ <div class="col-xs-6">
+ <div class="form-group">
+ <label for="minimumPostLength">[[admin/settings/post:restrictions.min-post-length]]</label>
+ <input id="minimumPostLength" type="text" class="form-control" value="8" data-field="minimumPostLength">
+ </div>
+ <div class="form-group">
+ <label for="maximumPostLength">[[admin/settings/post:restrictions.max-post-length]]</label>
+ <input id="maximumPostLength" type="text" class="form-control" value="32767" data-field="maximumPostLength">
+ </div>
+ </div>
</div>
</form>
- </div>
- </div>
</div>
</div>
@@ -71,7 +71,7 @@
<label for="preventTopicDeleteAfterReplies">[[admin/settings/post:restrictions.replies-no-delete]]</label>
<input id="preventTopicDeleteAfterReplies" type="text" class="form-control" value="0" data-field="preventTopicDeleteAfterReplies">
</div>
-
+
<div class="form-group">
<label for="topicStaleDays">[[admin/settings/post:restrictions.days-until-stale]]</label>
<input id="topicStaleDays" type="text" class="form-control" value="60" data-field="topicStaleDays">
@@ -98,7 +98,7 @@
<div class="form-group">
<label for="initialPostDelay">[[admin/settings/post:restrictions.seconds-defore-new]]</label>
<input id="initialPostDelay" type="text" class="form-control" value="10" data-field="initialPostDelay">
-
+
</div>
</div>
</div>
@@ -113,7 +113,7 @@
[[admin/settings/post:restrictions.post-queue-help]]
</p>
</div>
-
+
<div class="form-group">
<label for="newbiePostDelayThreshold">[[admin/settings/post:restrictions.rep-threshold]]</label>
<input id="newbiePostDelayThreshold" type="text" class="form-control" value="3" data-field="newbiePostDelayThreshold">
diff --git a/src/views/admin/settings/uploads.tpl b/src/views/admin/settings/uploads.tpl
index d008b0d9d3..175aef06fc 100644
--- a/src/views/admin/settings/uploads.tpl
+++ b/src/views/admin/settings/uploads.tpl
@@ -28,12 +28,26 @@
</p>
</div>
- <div class="form-group">
- <label for="maximumImageWidth">[[admin/settings/uploads:max-image-width]]</label>
- <input type="text" class="form-control" value="760" data-field="maximumImageWidth" placeholder="760">
- <p class="help-block">
- [[admin/settings/uploads:max-image-width-help]]
- </p>
+ <div class="row">
+ <div class="col-xs-6">
+ <div class="form-group">
+ <label for="resizeImageWidthThreshold">[[admin/settings/uploads:resize-image-width-threshold]]</label>
+ <input type="text" class="form-control" value="2000" data-field="resizeImageWidthThreshold" placeholder="2000">
+ <p class="help-block">
+ [[admin/settings/uploads:resize-image-width-threshold-help]]
+ </p>
+ </div>
+ </div>
+
+ <div class="col-xs-6">
+ <div class="form-group">
+ <label for="resizeImageWidth">[[admin/settings/uploads:resize-image-width]]</label>
+ <input type="text" class="form-control" value="760" data-field="resizeImageWidth" placeholder="760">
+ <p class="help-block">
+ [[admin/settings/uploads:resize-image-width-help]]
+ </p>
+ </div>
+ </div>
</div>
<div class="form-group">
@@ -89,6 +103,7 @@
</div>
</form>
</div>
+
</div>
<div class="row">
diff --git a/test/uploads.js b/test/uploads.js
index 9da7056941..092813108a 100644
--- a/test/uploads.js
+++ b/test/uploads.js
@@ -108,15 +108,17 @@ describe('Upload Controllers', function () {
});
it('should resize and upload an image to a post', function (done) {
- var oldValue = meta.config.maximumImageWidth;
- meta.config.maximumImageWidth = 10;
+ var oldValue = meta.config.resizeImageWidth;
+ meta.config.resizeImageWidth = 10;
+ meta.config.resizeImageWidthThreshold = 10;
helpers.uploadFile(nconf.get('url') + '/api/post/upload', path.join(__dirname, '../test/files/test.png'), {}, jar, csrf_token, function (err, res, body) {
assert.ifError(err);
assert.equal(res.statusCode, 200);
assert(Array.isArray(body));
assert(body[0].url);
assert(body[0].url.match(/\/assets\/uploads\/files\/\d+-test-resized\.png/));
- meta.config.maximumImageWidth = oldValue;
+ meta.config.resizeImageWidth = oldValue;
+ meta.config.resizeImageWidthThreshold = 1520;
done();
});
});