Skip to content

Instantly share code, notes, and snippets.

@JustinK
Created October 30, 2014 20:55
Show Gist options
  • Save JustinK/604febe58cf1c00abd74 to your computer and use it in GitHub Desktop.
Save JustinK/604febe58cf1c00abd74 to your computer and use it in GitHub Desktop.
function OnClientFileSelected(sender, args) {
var fileInput = sender;
//get the file from the input field
var file = args.get_fileInputField().files[0];
var imageType = /image.*/;
//stop the image from uploading to the temp storage on the server
if (file.type === 'image/jpeg') {
sender.pauseUpload();
$('.ruUploadProgress').addClass('ruUploadSuccess');
$('.ruFileProgressWrap').css('display', 'none');
}
if (file.type.match(imageType)) {
var reader = new FileReader();
reader.onload = function (e) {
var img = new Image();
img.src = reader.result;
img.onload = function () {
var MAX_HEIGHT = 800;
var MAX_WIDTH = 600;
var width = img.width;
var height = img.height;
if (width > height) {
if (width > MAX_WIDTH) {
height *= MAX_WIDTH / width;
width = MAX_WIDTH;
}
} else {
if (height > MAX_HEIGHT) {
width *= MAX_HEIGHT / height;
height = MAX_HEIGHT;
}
}
var canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, width, height);
var mpImg = new MegaPixImage(img);
getImageOrientation(file, function (imgOrientation) {
mpImg.render(canvas, { width: width, height: height, orientation: imgOrientation });
var dataurl = canvas.toDataURL("image/jpeg");
$('#MainContent_hiddenFile').val(dataurl);
});
}
}
reader.readAsDataURL(file);
}
}
function getImageOrientation(file, callBack) {
EXIF.getData(file, function () {
return callBack(EXIF.getAllTags(file).Orientation);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment