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.
            Asked
            
        
        
            Active
            
        
            Viewed 3,454 times
        
    0
            
            
         
    
    
        Jonathan Leffler
        
- 730,956
- 141
- 904
- 1,278
 
    
    
        Robi
        
- 158
- 1
- 9
- 
                    what do you want to match? the name? the content? – jcesarmobile Mar 11 '15 at 07:09
1 Answers
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.
- 
                    
- 
                    there is nothing wrong in combining two answers, but you should add some more stuff from yourself – Naeem Shaikh Mar 11 '15 at 05:45
- 
                    Also you code doesnt work.. http://jsfiddle.net/naeemshaikh27/hgs41ctd/ – Naeem Shaikh Mar 11 '15 at 05:49
 
    