Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ronaldlangeveld/63ba2641a1baaa724d70033fce60e641 to your computer and use it in GitHub Desktop.
Save ronaldlangeveld/63ba2641a1baaa724d70033fce60e641 to your computer and use it in GitHub Desktop.
function ResizeImage() {
var description = $('#imgdescription').val();
var project_name = $('#project_name').val();
if (window.File && window.FileReader && window.FileList && window.Blob) {
var filesToUploads = document.getElementById('imageFile').files;
var file = filesToUploads[0];
if (file) {
var reader = new FileReader();
// Set the image once loaded into file reader
reader.onload = function(e) {
var img = document.createElement("img");
img.src = e.target.result;
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
var MAX_WIDTH = 500;
var MAX_HEIGHT = 500;
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;
}
}
canvas.width = width;
canvas.height = height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, width, height);
dataurl = canvas.toDataURL(file.type);
document.getElementById('output').src = dataurl;
console.log(dataurl)
console.log(description)
console.log(file.name)
console.log(project_name)
var ImageData = {
'image_file': dataurl,
'image_desc': description,
'image_name': file.name,
'project_name': project_name
};
var ImageFaultData = JSON.stringify(ImageData);
console.log(ImageFaultData);
if (dataurl.length && description.length > 0) {
// alert("Please wait while we process your form");
// $("#submitproject").hide();
ajaxCall('/newimage', ImageFaultData,
function (data) {
var succ = data.ok;
if(succ == 'ok'){
console.log("OK")
$('#imageFile').val('');
$('#imgdescription').val('');
alert("Thank you");
}
if(succ == 'no') {
alert("server error");
}
}
)}
else {
$('#submitproject').prop('disabled', false);
alert("Fill in all fields");
}
}
reader.readAsDataURL(file);
}
} else {
alert('Your browser is not supported. Please upgrade');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment