add threshold for image resizing

v1.18.x
Barış Soner Uşaklı 7 years ago
parent 369e4854d2
commit 9c03881d5a

@ -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,

@ -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)",

@ -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);
},

@ -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);
},
};

@ -48,9 +48,9 @@
<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>
</form>
</div>
</div>
</form>
</div>
</div>

@ -28,13 +28,27 @@
</p>
</div>
<div class="row">
<div class="col-xs-6">
<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">
<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:max-image-width-help]]
[[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">
<label for="resizeImageQuality">[[admin/settings/uploads:resize-image-quality]]</label>
@ -89,6 +103,7 @@
</div>
</form>
</div>
</div>
<div class="row">

@ -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();
});
});

Loading…
Cancel
Save