Skip to content

Commit 89d1d13

Browse files
committed
Add option to scale image by a ratio decimal
1 parent 4ad86fc commit 89d1d13

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ These properties are used as conditions when the image should be scaled down pro
3131
- **maxWidth** - An integer in pixels for the maximum width allowed for uploaded images, selected images with a greater width than this value will be scaled down before upload. Default value: 1024.
3232
- **maxHeight** - An integer in pixels for the maximum height allowed for uploaded images, selected images with a greater height than this value will be scaled down before upload. Default value: 1024.
3333
- **maxSize** - A float value in megapixels (MP) for the maximum overall size of the image allowed for uploaded images, selected images with a greater size than this value will be scaled down before upload. The size of the image is calculated by the formula `size = width * height / 1000`, where `width` and `height` are the dimensions of the image in pixels. If the value is null or is not specified, then maximum size restriction is not applied. Default value: null. For websites it's good to set this value around 1.7: for landscape images taken by standard photo cameras (Canon, Nikon, etc.), this value will lead to scaling down the original photo to size about 1600 x 1000 px, which is sufficient for displaying the scaled image on large screen monitors.
34+
- **scaleRatio** - Allows scaling down to a specified fraction of the original size. (Example: a value of 0.5 will reduce the size by half.) Accepts a decimal value between 0 and 1.
3435

3536
#### Other properties ####
3637
- **quality** - A float between 0 and 1.00 for the image quality to use in the resulting image data, around 0.9 is recommended. Default value: 1.0.

src/main/webapp/js/ImageUploader.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ ImageUploader.prototype.scaleImage = function(img, completionCallback, orientati
172172
var mWidth = Math.min(this.config.maxWidth, ratio*this.config.maxHeight);
173173
if ( (this.config.maxSize>0) && (this.config.maxSize<canvas.width*canvas.height/1000) )
174174
mWidth = Math.min(mWidth, Math.floor(Math.sqrt(this.config.maxSize*ratio)));
175+
if ( !!this.config.scaleRatio )
176+
mWidth = Math.min(mWidth, Math.floor(this.config.scaleRatio*canvas.width));
175177

176178
if (this.config.debug){
177179
console.log('ImageUploader: original image size = ' + canvas.width + ' px (width) X ' + canvas.height + ' px (height)');
@@ -355,6 +357,9 @@ ImageUploader.prototype.setConfig = function(customConfig) {
355357
if ( (!this.config.maxSize) || (this.config.maxSize<0) ) {
356358
this.config.maxSize = null;
357359
}
360+
if ( (!this.config.scaleRatio) || (this.config.scaleRatio <= 0) || (this.config.scaleRatio >= 1) ) {
361+
this.config.scaleRatio = null;
362+
}
358363
this.config.autoRotate = true;
359364
if (typeof customConfig.autoRotate === 'boolean')
360365
this.config.autoRotate = customConfig.autoRotate;

0 commit comments

Comments
 (0)