var fileinput = document.getElementById('imageUpload'); var max_width = fileinput.getAttribute('data-maxwidth'); var max_height = fileinput.getAttribute('data-maxheight'); var canvas = document.getElementById('canvas'); var form = document.getElementById('formUpload'); function processfile(file, o) { if( !( /image/i ).test( file.type ) ) { // alert( "File "+ file.name +" is not an image." ); return false; } // read the files if (window.FileReader && window.FileReader.prototype.readAsArrayBuffer) { var reader = new FileReader(); reader.readAsArrayBuffer(file); reader.onload = function(event) { // blob stuff var blob = new Blob([event.target.result]); // create blob... window.URL = window.URL || window.webkitURL; var blobURL = window.URL.createObjectURL(blob); // and get it's URL // helper Image object var image = new Image(); image.src = blobURL; image.onload = function() { // have to wait till it's loaded resized = resizeMe(image); // send it to canvas var newinput = document.createElement("input"); newinput.type = 'hidden' newinput.name = 'images[]' newinput.value = resized; // put result from canvas into new hidden input form.appendChild(newinput); o.submit(); //submit external object }; }; } } function readfiles(files, o) { // remove the existing canvases and hidden inputs if user re-selects new pics var existinginputs = document.getElementsByName('images[]'); var existingcanvases = document.getElementsByTagName('canvas'); while (existinginputs.length > 0) { // it's a live list so removing the first element each time // DOMNode.prototype.remove = function() {this.parentNode.removeChild(this);} form.removeChild(existinginputs[0]); } for (var i = 0; i < files.length; i++) { processfile(files[i], o); // process each file at once } fileinput.value = ""; //remove the original files from fileinput // TODO remove the previous hidden inputs if user selects other files } function resizeMe(img) { var canvas = document.createElement('canvas'); var width = img.width; var height = img.height; // calculate the width and height, constraining the proportions if (width > height) { if (width > max_width) { //height *= max_width / width; height = Math.round(height *= max_width / width); width = max_width; } } else { if (height > max_height) { //width *= max_height / height; width = Math.round(width *= max_height / height); height = max_height; } } // resize the canvas and draw the image data into it canvas.width = width; canvas.height = height; var ctx = canvas.getContext("2d"); ctx.drawImage(img, 0, 0, width, height); return canvas.toDataURL("image/jpeg",0.7); //set the data from canvas as 70% JPG (can be also PNG, etc.) }