Skip to content

Instantly share code, notes, and snippets.

@RaoHai
Created December 9, 2016 07:46
Show Gist options
  • Save RaoHai/8821a9222b8d343902e0e7c021d070f0 to your computer and use it in GitHub Desktop.
Save RaoHai/8821a9222b8d343902e0e7c021d070f0 to your computer and use it in GitHub Desktop.
Simpe Image Resampling
// var fileInput = document.querySelector('file');
var targetWidth = 400;
var targetHeight = 400;
function doUpload () {
// upload picture
}
fileInput.onChange = function () {
var file = this.files[0];
var reader = new FileReader();
reader.onload = function () {
var image = new Image();
image.onload = function () {
var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
context.drawImage(image, 0, 0, targetWidth, targetHeight);
var imageData = context.toDataURL('image/png');
doUpload(imageData);
}
}
reader.readAsDataURL(file);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment