<img id="imgBeforeConvert" src="image/download.jpg" />
//'imgBeforeConvert' image tag contains image which we have to convert into binary.
<img id="imgAfterConvert"/>
//'imgAfterConvert' image tag is used to display the image after conversion to binary and again convert binary into image.
STEPS:
1) Find the 'imgBeforeConvert' image tag
var source = document.getElementById("imgBeforeConvert");
2) pass the source variable to getBase64Image() function:
function getBase64Image(source) {
var canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
// Copy the image contents to the canvas
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
// Get the data-URL formatted image
// Firefox supports PNG and JPEG. You could check img.src to guess the
// original format, but be aware the using "image/jpg" will re-encode the image.
var dataURL = canvas.toDataURL("image/png");
$('#imgAfterConvert').attr("src", dataURL);
return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
}
No comments:
Post a Comment