Browse files

Only apply the new width/height after all calculations have been done.

  • Loading branch information...
1 parent fb21dd5 commit 46baf0c85f242d262bc10d87283622188833a509 @blueimp committed Apr 20, 2012
Showing with 17 additions and 13 deletions.
  1. +16 −12 load-image.js
  2. +1 −1 load-image.min.js
View
28 load-image.js
@@ -1,5 +1,5 @@
/*
- * JavaScript Load Image 1.1.4
+ * JavaScript Load Image 1.1.5
* https://github.com/blueimp/JavaScript-Load-Image
*
* Copyright 2011, Sebastian Tschan
@@ -56,29 +56,33 @@
loadImage.scale = function (img, options) {
options = options || {};
var canvas = document.createElement('canvas'),
+ width = img.width,
+ height = img.height,
scale = Math.max(
- (options.minWidth || img.width) / img.width,
- (options.minHeight || img.height) / img.height
+ (options.minWidth || width) / width,
+ (options.minHeight || height) / height
);
if (scale > 1) {
- img.width = parseInt(img.width * scale, 10);
- img.height = parseInt(img.height * scale, 10);
+ width = parseInt(width * scale, 10);
+ height = parseInt(height * scale, 10);
}
scale = Math.min(
- (options.maxWidth || img.width) / img.width,
- (options.maxHeight || img.height) / img.height
+ (options.maxWidth || width) / width,
+ (options.maxHeight || height) / height
);
if (scale < 1) {
- img.width = parseInt(img.width * scale, 10);
- img.height = parseInt(img.height * scale, 10);
+ width = parseInt(width * scale, 10);
+ height = parseInt(height * scale, 10);
}
if (!options.canvas || !canvas.getContext) {
+ img.width = width;
+ img.height = height;
return img;
}
- canvas.width = img.width;
- canvas.height = img.height;
+ canvas.width = width;
+ canvas.height = height;
canvas.getContext('2d')
- .drawImage(img, 0, 0, img.width, img.height);
+ .drawImage(img, 0, 0, width, height);
return canvas;
};
View
2 load-image.min.js
@@ -1 +1 @@
-(function(a){"use strict";var b=function(a,c,d){var e=document.createElement("img"),f,g;return e.onerror=c,e.onload=function(){g&&b.revokeObjectURL(g),c(b.scale(e,d))},window.Blob&&a instanceof Blob||window.File&&a instanceof File?f=g=b.createObjectURL(a):f=a,f?(e.src=f,e):b.readFile(a,function(a){e.src=a})},c=window.createObjectURL&&window||window.URL&&URL||window.webkitURL&&webkitURL;b.scale=function(a,b){b=b||{};var c=document.createElement("canvas"),d=Math.max((b.minWidth||a.width)/a.width,(b.minHeight||a.height)/a.height);return d>1&&(a.width=parseInt(a.width*d,10),a.height=parseInt(a.height*d,10)),d=Math.min((b.maxWidth||a.width)/a.width,(b.maxHeight||a.height)/a.height),d<1&&(a.width=parseInt(a.width*d,10),a.height=parseInt(a.height*d,10)),!b.canvas||!c.getContext?a:(c.width=a.width,c.height=a.height,c.getContext("2d").drawImage(a,0,0,a.width,a.height),c)},b.createObjectURL=function(a){return c?c.createObjectURL(a):!1},b.revokeObjectURL=function(a){return c?c.revokeObjectURL(a):!1},b.readFile=function(a,b){if(window.FileReader&&FileReader.prototype.readAsDataURL){var c=new FileReader;return c.onload=function(a){b(a.target.result)},c.readAsDataURL(a),c}return!1},typeof define!="undefined"&&define.amd?define(function(){return b}):a.loadImage=b})(this);
+(function(a){"use strict";var b=function(a,c,d){var e=document.createElement("img"),f,g;return e.onerror=c,e.onload=function(){g&&b.revokeObjectURL(g),c(b.scale(e,d))},window.Blob&&a instanceof Blob||window.File&&a instanceof File?f=g=b.createObjectURL(a):f=a,f?(e.src=f,e):b.readFile(a,function(a){e.src=a})},c=window.createObjectURL&&window||window.URL&&URL||window.webkitURL&&webkitURL;b.scale=function(a,b){b=b||{};var c=document.createElement("canvas"),d=a.width,e=a.height,f=Math.max((b.minWidth||d)/d,(b.minHeight||e)/e);return f>1&&(d=parseInt(d*f,10),e=parseInt(e*f,10)),f=Math.min((b.maxWidth||d)/d,(b.maxHeight||e)/e),f<1&&(d=parseInt(d*f,10),e=parseInt(e*f,10)),!b.canvas||!c.getContext?(a.width=d,a.height=e,a):(c.width=d,c.height=e,c.getContext("2d").drawImage(a,0,0,d,e),c)},b.createObjectURL=function(a){return c?c.createObjectURL(a):!1},b.revokeObjectURL=function(a){return c?c.revokeObjectURL(a):!1},b.readFile=function(a,b){if(window.FileReader&&FileReader.prototype.readAsDataURL){var c=new FileReader;return c.onload=function(a){b(a.target.result)},c.readAsDataURL(a),c}return!1},typeof define!="undefined"&&define.amd?define(function(){return b}):a.loadImage=b})(this);

0 comments on commit 46baf0c

Please sign in to comment.