I have JavaScript function which check the image is null or not and call the function to convert the image to base64 string, but after converting image also I can't able to return that variable value always shows undefiend.updateUserDetails its an onclick function, help me to solve it.
var userImgData;
function updateUserDetails() {
if (userImgData === "undefined") {
ImgData = (document.getElementById("userProfileImage"));
getUserBase64Image(ImgData.src)
}
updateDetails(userImgData)
}
function updateDetails(userImgData )
{
//i am calling ajax to update
}
function getUserBase64Image(img) {
var i = img;
var canvas = document.createElement('canvas'),
ctx = canvas.getContext('2d'),
img = new Image();
img.onload = function () {
canvas.width = img.width;
canvas.height = img.height;
if (canvas.width > canvas.height) {
canvas.style.width = "320px";
} else {
canvas.style.height = "300px";
}
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
var dataURl = (canvas.toDataURL('image/jpeg'));
userImgData = dataURl.replace("data:image/jpeg;base64,", " ")
return userImgData ;
}
img.src = img;
}