从 EXIF 数据中提取描述并排除重复文本

Extract description from EXIF data and exclude duplicate text

我正在处理一个包含肖像的图片库,我从 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.

提前致谢。

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

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