6

I'm building a website for a client who requires a photo gallery and I was going to use the file name as the alt tag however he wants me to use the keywords he has put in the EXIF data - As I'm no photographer I really don't understand the technical side of this, but, I have a script working so far to get the filename and I'm hoping that it will be as simple as changing a few lines of code to get the EXIF instead of Filename. Here is my code:

<?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>
user3177012
  • 663
  • 2
  • 8
  • 19
  • 2
    Did you have a look at http://php.net/manual/en/book.exif.php? – Reeno Sep 08 '14 at 15:14
  • Or even this method if it helps? http://php.net/manual/en/function.exif-read-data.php – scrineym Sep 08 '14 at 15:14
  • Thanks @Reeno but I really don't know what I'm actually extracting - Any pointers for the example code I've given? – user3177012 Sep 08 '14 at 15:16
  • I don't know where your client has stored these keywords. You can start with the function @scrineym posted (and assuming the keywords are in the comments field): `$exif = exif_read_data($images[$i], 'COMMENT'); echo implode($exif['COMMENT'], ', ');` – Reeno Sep 08 '14 at 15:20

1 Answers1

5

Do this:

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

If FileName isn't in the exif data, $filename will contain the file name from the path.

The right name might be in a different variable than $filedata['ImageDescription']. It might also be in, for example, $filedata['FileName'] or $filedata['Title']. Just see for yourself which one works

Jonan
  • 2,485
  • 3
  • 24
  • 42
  • Thanks but I'm getting `Fatal error: Call to undefined function exif_read_data() in C:\AppServ\` - Do I have to enable this function in PHP? – user3177012 Sep 08 '14 at 15:27
  • @user3177012 in `php.ini` you need to remove the semi-colon in front of `extension=php_exif.dll` – Jonan Sep 08 '14 at 15:30
  • Yeah I checked that and there is no semi-colon in front of it - Any other reason for the error? – user3177012 Sep 08 '14 at 15:34
  • Just ran your edit and I'm getting `Parse error: syntax error, unexpected '}' in ` after `$filename = $filedata['ImageDescription']` – user3177012 Sep 08 '14 at 15:35
  • @user3177012 please see the accepted answer on this page: http://stackoverflow.com/questions/8573070/php-exif-read-data-not-defined. Does it work after you have done this? – Jonan Sep 08 '14 at 15:36
  • @user3177012 oops. forgat a semi-colon – Jonan Sep 08 '14 at 15:36
  • Both extensions are un semi-coloned but there is no mnetion of EXIF in PHPINFO – user3177012 Sep 08 '14 at 15:47
  • @user3177012 did you restart your server and edit the right file? – Jonan Sep 08 '14 at 16:10
  • Yes I found that I had to move `extension=php_mbstring.dll` to above `extension=php_exif.dll` - The data I need is under ImageDescription but it isn't showing using your example? – user3177012 Sep 08 '14 at 16:19
  • Ok, getting there - I've tested it with a few photo's and for ones without EXIF data it shows the FileName and for the one with EXIF data it shows a `M` - The description is actually `Mothercare` so I assume it's showing the first letter, how can I make it show the complete string? – user3177012 Sep 08 '14 at 16:25
  • @user3177012 that's because you're using `$filename[0]`. Instead, use `$filename` (I'll edit my post for the images without exif data) – Jonan Sep 08 '14 at 16:27