【问题标题】:Extract description from EXIF data and exclude duplicate text从 EXIF 数据中提取描述并排除重复文本
【发布时间】:2017-05-23 21:31:18
【问题描述】:

我正在开发一个带有肖像的图片库,我从 Exif 数据中提取文本。当我编辑图片时,我会写下人物的名字,这些名字稍后会在图片库中显示为标题。

我想创建一个列表,但不知道如何将其制成一个数组,我可以使用array_unique() 排除重复项。

我的代码是:

$imgdir = 'images/'; //pick your folder ie.: 'images/'
$current_folder = basename(dirname(__FILE__));
$allowed_types = array('png','jpg','jpeg','gif');

$dimg = opendir($imgdir);
while($imgfile = readdir($dimg)){
    if(in_array(strtolower(substr($imgfile,-3)),$allowed_types)){
        $a_img[] = $imgfile;
        sort($a_img);
        reset ($a_img);
    }
}
$totimg = count($a_img);

?><!DOCTYPE html>
<html>
<head>
    <title>Names listing</title>
</head>
<body>
<?php
for ($x=0; $x < $totimg; $x++){
    if ($x == ($totimg-1))
        $_content = (exif_read_data('images/'.$a_img[$x])[ImageDescription]).'';
    else
        $_content = (exif_read_data('images/'.$a_img[$x])[ImageDescription]).', ';
    $_unique = $_content;
    echo $_unique;
}    
?>
</body>
</html>

如果我有同一个人的多张图片,我会得到如下列表:

John, John, Jane, Jane, Jane, ... etc.

我想要的是:

John, Jane, ... etc.

提前致谢。

【问题讨论】:

    标签: php arrays exif array-unique


    【解决方案1】:

    您应该将所有ImageDescription 值存储在一个临时数组中(例如$descriptions),然后在循环完成后在临时数组上调用array_unique() 以删除重复项,然后您可以选择按字母顺序排列这些值,并且最后使用implode() 将剩余的值转换为字符串,并用[逗号和空格] 分隔值。

    for($x=0; $x<$totimg; ++$x){
        $descriptions[]=exif_read_data('images/'.$a_img[$x])[ImageDescription];
    }
    $descriptions=array_unique($descriptions);       // remove duplicates before sorting
    sort($descriptions);                             // sort alphabetically
    echo implode(', ',$descriptions);                // display csv
    

    【讨论】:

    • 我不得不添加一些括号,但非常感谢!
    • @kdt2017 是的,抱歉我太草率了。我现在已经修复了我的代码。你不应该在你的 exif 电话周围需要额外的括号。如果我错了,请告诉我。
    • 它工作正常,谢谢。我也尝试对列表进行排序,但没有运气。有没有简单的方法可以做到这一点?
    猜你喜欢
    • 2012-12-25
    • 1970-01-01
    • 2021-05-29
    • 2018-01-18
    • 2016-09-15
    • 1970-01-01
    • 2014-07-11
    • 2014-09-03
    • 1970-01-01
    相关资源
    最近更新 更多