Извлечение Данных EXIF С помощью PHP


Я создаю веб-сайт для клиента, которому требуется фотогалерея, и я собирался использовать имя файла в качестве тега 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>
Author: user3177012, 2014-09-08

1 answers

Сделайте это:

$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];
}

Если FileName отсутствует в данных exif, $filename будет содержать имя файла из пути.

Правильное имя может быть в переменной, отличной от $filedata['ImageDescription']. Это также может быть, например, $filedata['FileName'] или $filedata['Title']. Просто посмотрите сами, какой из них работает

 1
Author: Jonan, 2014-09-08 16:27:34