使用PHP提取EXIF数据

6

我正在为一个需要照片库的客户建立网站,原本打算使用文件名作为alt标签,但他要求我使用他在EXIF数据中输入的关键字。由于我不是摄影师,对此技术方面并不了解。但我已经编写了一个脚本来获取文件名,希望只需更改几行代码就能获取EXIF数据。以下是我的代码:

<?php
//The directory to your images folder, with trailing slash
$dir = "cms/gallery/photo/";

//Set the extensions you want to load, seperate by a comma.
$extensions = "jpeg,jpg";

//Set the number of images you want to display per page
$imagesPerPage = 3;

//Set the $page variable
if(!isset($_GET['page'])){
    $page = 1;
}else{
    $page = $_GET['page'];
}

//Load all images into an array
$images = glob($dir."*.{".$extensions."}", GLOB_BRACE);

//Count the number of images
$totalImages = count($images);

//Get the total pages
$totalPages = ceil($totalImages / $imagesPerPage);

//Make sure the page you are on is not greater then the total pages available.
if($page > $totalPages){
    //Set the currnet page to the total pages.
    $page = $totalPages;
}

//Now find where to start the loading from
$from = ($page * $imagesPerPage) - $imagesPerPage;

//Now start looping
for($i = $from; $i < ($from + $imagesPerPage); $i++){
    //We need to make sure that its within the range of totalImages.
    if($i < $totalImages){
        $filename = explode('.', basename($images[$i])); // GET EXIF DESCRIPTION AS $FILENAME
        //Now we can display the image!
        echo "

            <div class='galleryCellHolder'>
                <div class='galleryCell'>
                    <a class='fancybox' rel='group' href='{$images[$i]}'><img class='galleryPhoto' src='{$images[$i]}' alt='" . $filename[0] . "'></a>
                </div>
            </div>

        ";
    }
}

//Now to display the page numbers!
for($p = 1; $p <= $totalPages; $p++){
    if($p == $page){
        $tmp_pages[] = "<a class='noPagination'>{$p}</a>";
    }else{
        $tmp_pages[] = "<a class='pagination' href='?page={$p}'>{$p}</a>";
    }
}
?>
<div class="clearLeft"></div>
<div id="pagination">
    <?php

    //Now display pages, seperated by a hyphon.
    echo "<br />" . implode("", $tmp_pages);

    ?>
</div>

2
你看过 http://php.net/manual/zh/book.exif.php 吗? - Reeno
甚至可以尝试使用这种方法吗?http://php.net/manual/zh/function.exif-read-data.php - scrineym
谢谢@Reeno,但我真的不知道我正在提取什么 - 对于我给出的示例代码有任何指针吗? - user3177012
我不知道你的客户存储这些关键字的位置。您可以从@scrineym发布的函数开始(假设关键字在评论字段中):$exif = exif_read_data($images[$i], 'COMMENT'); echo implode($exif['COMMENT'], ', '); - Reeno
1个回答

5

请执行以下操作:

$filedata = exif_read_data($images[$i]);
if(is_array($filedata) && isset($filedata['ImageDescription'])){
    $filename = $filedata['ImageDescription'];
} else{
    $filename = explode('.', basename($images[$i]));
    $filename = $filename[0];
}

如果exif数据中没有FileName,那么$filename将包含路径中的文件名。
正确的名称可能在与$filedata['ImageDescription']不同的变量中。例如,它可能在$filedata['FileName']$filedata['Title']中。请自行查看哪个有效。

谢谢,但我遇到了“致命错误:在C:\ AppServ \中调用未定义的函数exif_read_data()” - 我需要在PHP中启用此功能吗? - user3177012
php.ini 文件中,您需要删除 extension=php_exif.dll 前面的分号。 - Jonan
是的,我检查了一下,它前面没有分号 - 是否还有其他导致错误的原因? - user3177012
刚刚运行了您的编辑,但是在$filename = $filedata['ImageDescription']之后出现了“解析错误:语法错误,意外的'}'”。 - user3177012
好的,接近成功了 - 我已经用几张照片测试过了,对于那些没有EXIF数据的照片,它显示文件名;而对于那些有EXIF数据的照片,它显示一个 M - 描述实际上是 Mothercare,所以我假设它只显示了第一个字母,如何让它显示完整的字符串? - user3177012
显示剩余6条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接