rossturner/HTML5-ImageUploader

A few potential fixes

ryancramerdesign opened this issue · 3 comments

Thanks for this Image uploader/resizer. I've been adapting it for another purpose (just the resizing part), and noticed a few things weren't working in my own testing. It looks like all are simple fixes, and just wanted to pass them along:

  • As far as I can tell, the client-side resizing does not come into play at all unless the config.autoRotate option is true. See this conditional. I basically just removed the autoRotate conditional and left everything within it, and it seems to work fine.

  • The config.maxSize option doesn't appear to work as described in the README. It suggests that it is expecting a float like 1.7, but the actual code seems to be expecting something different. I was able to correct it by adjusting the maxSize part of the scaleImage function to the following (though I'm guessing you know something better):

if(this.config.maxSize > 0 && (this.config.maxSize < (canvas.width * canvas.height) / 1000000)) { 
  var mSize = Math.floor(Math.sqrt(this.config.maxSize * ratio) * 1000);
  mWidth = mWidth > 0 ? Math.min(mWidth, mSize) : mSize;
}
  • As far as I can tell, your resizing code works equally well with png and gif files (?), once I replaced the static image/jpeg content-type from the canvas.toDataURL() call with this.currentFile.type at the bottom of the scaleImage function. I changed it to this below, and it appears to be working, though I may be missing something.
var quality = this.currentFile.type == 'image/jpeg' ? this.config.quality : 1.0;
var imageData = canvas.toDataURL(this.currentFile.type, quality);

Thanks a lot, good spot on the resizing not working - poor ownership on my part as I've not updated this repo in years myself and that will have come in from another PR I accepted. Should have reviewed it a bit more rigorously! Same for the maxSize, introduced my someone else. A lesson to be learned in accepting requests without enough review!

Very interesting bonus it works with other image types too :) Suppose it makes sense when the browser can convert the source file into a canvas.

Was just coming to point out the maxSize oopsie, but it's already here. Cheers