Output similar images in directory after comparing with image input in php
P粉245003607
P粉245003607 2024-01-29 14:38:39
0
1
527

In my web page, the user will upload an image and then upon submission it should be compared with all the images in the directory and similar images should be output. I did this with md5 but it only outputs the exact image, I know the reason but I don't know how to loop all the images in my directory with the input image using RGB comparison... Can anyone help me ? This is my current code:

<?php 
    if(isset($_POST['submit'])){

    $filepath=pathinfo($_FILES['file']['name']) ;
    $extension=$filepath['extension'];
    
    $iname= date('H-i-s').'.'.$extension;
    $path='upload/'.$iname;
    if(move_uploaded_file($_FILES['file']['tmp_name'],$path)){
        $img=$path;
        echo $img;
        $f=md5(file_get_contents($img));
        $images=glob("img/*");
        foreach($images as $image){
            if($f==md5(file_get_contents($image))){
                echo  "<img height='70px' width='70px' src='".$image."'/>";
            }

        }


    
}
}
?>

And my html code

<html>
     <body> 
       <form method=post enctype="multipart/form-data">
          <input type=file name=file><br><input type=submit name=submit value=submit>
       </form>
    </body>
   </html>

P粉245003607
P粉245003607

reply all(1)
P粉860897943

I used the class mentioned in this git repository to calculate image hashes and their differences.

https://github.com/nvthaovn/CompareImage

and change my code to:

<?php 
include('compareImages.php');
$flag = 0;

if(isset($_POST['submit'])) {

    $filepath = pathinfo($_FILES['file']['name']);
    $extension = $filepath['extension'];
    
    $iname = date('H-i-s').'.'.$extension;
    $path = 'upload/'.$iname;

    if(move_uploaded_file($_FILES['file']['tmp_name'], $path)) {
        
        $compareMachine = new compareImages($path);
        $images = glob("img/*");

        foreach($images as $image)
        {
            $diff = $compareMachine->compareWith($image);
            
            if($diff ";
            }
        }
        
        $flag = 1;
    }
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template