search

Home  >  Q&A  >  body text

Output similar images in directory after comparing with image input in php

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粉245003607P粉245003607305 days ago543

reply all(1)I'll reply

  • P粉860897943

    P粉8608979432024-01-30 00:12:15

    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 < 21) {
                    $results[] = $image;
                    echo  "";
                }
            }
            
            $flag = 1;
        }
    }

    reply
    0
  • Cancelreply