/** * Image resizing library * To resize an image before uploading it */ window.ImageResize = (function () { 'use strict'; // Polyfill if (!HTMLCanvasElement.prototype.toBlob) { Object.defineProperty(HTMLCanvasElement.prototype, 'toBlob', { value: function (callback, type, quality) { var binStr = atob( this.toDataURL(type, quality).split(',')[1] ), len = binStr.length, arr = new Uint8Array(len); for (var i=0; i height) { if (width > maxSize) { height *= maxSize / width; width = maxSize; } } else { if (height > maxSize) { width *= maxSize / height; height = maxSize; } } canvas.width = width; canvas.height = height; canvas.getContext('2d').drawImage(image, 0, 0, width, height); _this.output(canvas, outputType, callback); } image.src = dataURL; }, output: function(canvas, outputType, callback) { switch (outputType) { case 'file': canvas.toBlob(function (blob) { callback(blob); }, 'image/jpeg', 0.8); break; case 'dataURL': callback(canvas.toDataURL('image/jpeg', 0.8)); break; } } }; return ImageResize; }());