与PHP中的输入图像进行比较后,在目录中输出相似的图像

发布于 2025-02-09 13:29:13 字数 1055 浏览 2 评论 0原文

在我的网页中,用户将上传图像,然后提交后,应将其与目录中的所有图像进行比较,并输出相似的图像。我使用MD5进行了此操作,但它只会输出精确的图像,我知道原因,但我不知道如何使用RGB比较使用输入的图像循环所有图像...有人可以帮助我。这是我当前的代码:

    <?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."'/>";
            }

        }


    
}
}
?>

和我的HTML代码

    <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>

In my web page a user will upload an image then after submit it should be compared with all images in a directory and output similar images. I did this with md5 but it will output only exact images and I know the reason but I don't know how to loop all images in my directory with the inputted image using RGB comparisons... Can somebody please help me. Here 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扎心 2025-02-16 13:29:13

我使用了此GIT存储库中提到的类,该类别计算了图像哈希及其差异。

https://github.com/nvthaovn/compareimage

并更改了我的代码:

<?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  "<img height='70px' width='70px' src='".$image."'/>";
            }
        }
        
        $flag = 1;
    }
}

I used the class mentioned in this git repo that calculates the image hashes and their differences.

https://github.com/nvthaovn/CompareImage

and changed my code to this:

<?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  "<img height='70px' width='70px' src='".$image."'/>";
            }
        }
        
        $flag = 1;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文