【问题标题】:Compare two images with ImageMagick使用 ImageMagick 比较两个图像
【发布时间】:2013-12-30 06:19:15
【问题描述】:

我正在尝试拍摄两张图像并使用 ImageMagick 进行比较。

我想知道图像是否接近相同并返回一个数字以存储在 php 的变量中并显示在我的浏览器屏幕中。

我的主机帐户是 bluehost,我通过调整图像大小进行了测试,ImageMagick 已安装并运行。以下代码在这里工作:http://zing-cards.com/imagetest2.php

<?php
$photo="/home/tcsdesig/public_html/zing/flower.jpg";
$cmd = "/usr/bin/convert $photo -thumbnail 100x100 JPG:-";

header("Content-type: image/jpeg");
passthru($cmd, $retval);

?>

我了解以下命令行代码(根据此post)应该以数字格式获取结果:

compare -metric RMSE first.png second.png NULL:

到目前为止,这是我的代码:

<?php
$photoPath1="/home/*username*/public_html/flower.jpg";
$photoPath2="/home/*username*/public_html/flower1.jpg";

$cmd = "/usr/bin/compare -metric RMSE $photoPath1 $photoPath2 NULL:";
exec($cmd);
?>

如何让这个值显示在我的浏览器中?

除了上面的代码我试过了:

$result = exec($cmd);
echo $result;

据我了解,这应该显示一个较小的数字(因为图像是相等的),如果我使用完全不同的图像,它们应该显示一个很大的数字,但我没有得到任何结果。

【问题讨论】:

    标签: php imagemagick compare


    【解决方案1】:

    如果我这样做了

    <?php
        $photoPath1="/home/*username*/public_html/flower.jpg";
        $photoPath2="/home/*username*/public_html/flower1.jpg";
    
        $result = exec("/usr/bin/compare -metric RMSE /usr/share/pixmaps/faces/dice.jpg /usr/share/pixmaps/faces/fish.jpg NULL:");
        echo $result;
    ?>
    

    我明白了

    20455.4 (0.312129)
    

    这是我所期望的,因为图像完全不同......

    我想你应该事先在命令行上尝试compare...

    例如,如果您的系统上没有安装compare,则不会得到任何结果...

    更新:要进一步调查持续缺乏输出的情况,请执行以下操作:

    <?php
        $photoPath1 = "/home/*username*/public_html/flower.jpg";
        $photoPath2 = "/home/*username*/public_html/flower1.jpg";
    
        $compareCommand = "/usr/bin/compare";
        if (!is_executable($compareCommand)) die("Sorry, can't execute $compareCommand!");
        $output = shell_exec("$compareCommand -metric RMSE $photoPath1 $photoPath2 NULL: 2>&1");
        print $output;
    ?>
    

    更新 2

    在评论中回答 OP 问题:

    在转换手册页中没有指定是否以及如何仅打印 RMSE 错误百分比...

    您可以在命令中添加(例如)| sed -e 's/.*(\(.*\))/\1/g',以保持 RMSE 错误百分比...

    【讨论】:

    • 我简化了我的代码:它在这里:zing-cards.com/imagecompare.php Compare Images"; $result = exec("/usr/bin/compare -metric RMSE /home/*username*/public_html/zing/flower.jpg /home/*username*/public_html/zing/woman1.png NULL:");回声“结果:”。 $结果; ?>
    • 您在使用 Bluehost 吗?我需要做些什么来安装或激活比较吗?正如我所说,转换命令有效。我只是得到一个空白,没有结果。有没有办法让imagemagick输出错误码?
    • 我没有安装 imagick:zing-cards.com/imagick.php 这可能是个问题吗?这个页面是这样编码的:
    • Bluehost 说我在加载我的内容类型之前正在获取我的数据。我该如何解决这个问题?
    • header("Content-type: text/html"); $result = exec("/usr/bin/compare -metric RMSE /home/tcsdesig/public_html/zing/flower.jpg /home/tcsdesig/public_html/zing/woman1.png NULL:");回声 $result;
    猜你喜欢
    • 2017-03-14
    • 2016-08-06
    • 1970-01-01
    • 2019-07-07
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    • 2017-12-10
    • 1970-01-01
    相关资源
    最近更新 更多