Skip to content

Commit ccb467f

Browse files
committed
Fixed scaling algorithm for cases with both max and min settings.
1 parent 9a62f4c commit ccb467f

File tree

4 files changed

+44
-12
lines changed

4 files changed

+44
-12
lines changed

load-image.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* JavaScript Load Image 1.1
2+
* JavaScript Load Image 1.1.1
33
* https://github.com/blueimp/JavaScript-Load-Image
44
*
55
* Copyright 2011, Sebastian Tschan
@@ -53,16 +53,16 @@
5353
loadImage.scale = function (img, options) {
5454
options = options || {};
5555
var canvas = document.createElement('canvas'),
56-
scale = Math.min(
57-
(options.maxWidth || img.width) / img.width,
58-
(options.maxHeight || img.height) / img.height
59-
);
60-
if (scale >= 1) {
6156
scale = Math.max(
6257
(options.minWidth || img.width) / img.width,
6358
(options.minHeight || img.height) / img.height
6459
);
65-
}
60+
img.width = parseInt(img.width * scale, 10);
61+
img.height = parseInt(img.height * scale, 10);
62+
scale = Math.min(
63+
(options.maxWidth || img.width) / img.width,
64+
(options.maxHeight || img.height) / img.height
65+
);
6666
img.width = parseInt(img.width * scale, 10);
6767
img.height = parseInt(img.height * scale, 10);
6868
if (!options.canvas || !canvas.getContext) {

load-image.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
},
1313
"bugs": "https://github.com/blueimp/JavaScript-Load-Image/issues",
1414
"main": "load-image",
15-
"version": "1.1.0"
15+
"version": "1.1.1"
1616
}

test/test.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* JavaScript Load Image Test 1.1
2+
* JavaScript Load Image Test 1.1.1
33
* https://github.com/blueimp/JavaScript-Load-Image
44
*
55
* Copyright 2011, Sebastian Tschan
@@ -88,6 +88,38 @@
8888
}, {minHeight: 240}));
8989
});
9090

91+
$.asyncTest('Scale to options.minWidth but respect options.maxWidth', function () {
92+
$.ok($.loadImage(blob, function (img) {
93+
$.start();
94+
$.strictEqual(img.width, 160);
95+
$.strictEqual(img.height, 120);
96+
}, {minWidth: 240, maxWidth: 160}));
97+
});
98+
99+
$.asyncTest('Scale to options.minHeight but respect options.maxHeight', function () {
100+
$.ok($.loadImage(blob, function (img) {
101+
$.start();
102+
$.strictEqual(img.width, 160);
103+
$.strictEqual(img.height, 120);
104+
}, {minHeight: 180, maxHeight: 120}));
105+
});
106+
107+
$.asyncTest('Scale to options.minWidth but respect options.maxHeight', function () {
108+
$.ok($.loadImage(blob, function (img) {
109+
$.start();
110+
$.strictEqual(img.width, 160);
111+
$.strictEqual(img.height, 120);
112+
}, {minWidth: 240, maxHeight: 120}));
113+
});
114+
115+
$.asyncTest('Scale to options.minHeight but respect options.maxWidth', function () {
116+
$.ok($.loadImage(blob, function (img) {
117+
$.start();
118+
$.strictEqual(img.width, 160);
119+
$.strictEqual(img.height, 120);
120+
}, {minHeight: 180, maxWidth: 160}));
121+
});
122+
91123
$.module('Canvas');
92124

93125
$.asyncTest('Return img element to callback if options.canvas is not true', function () {

0 commit comments

Comments
 (0)