0

I want to match two images with each other and if they matches then the result will be true. If it isn't then it will return false. But I want it in JavaScript.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Robi
  • 158
  • 1
  • 9

1 Answers1

1

You can check by converting image into base64 string

function getBase64Image(img) {
// Create an empty canvas element
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");

return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
}

Then

var a = new Image(),
    b = new Image();
a.src = url_a;
b.src = url_b;

var a_base64 = getBase64Image(a),
    b_base64 = getBase64Image(b);

if (a_base64 === b_base64)
{
    // they are identical
}
else
{
    // you can probably guess what this means
}

You can see this link to know more.

Community
  • 1
  • 1
AtanuCSE
  • 8,832
  • 14
  • 74
  • 112