Search code examples
phpimagegettitlecaption

how to get image caption using php


I'm using PHP to put all the images in a given folder into a slideshow. This works fine in the following code:

<?php
//get all image files with a .jpg extension.
$images = glob("" . $directory . "*.jpg");

$imgs = array();
// create array
foreach($images as $image){ $imgs[] = "$image"; }

/****** Display Images ******/
foreach ($imgs as $img) {
    //I would like to get the image title here, to put in the echo below    
    echo "<div><img src='$img' border='0' width=\"100%\" height=\"100%\"/ /></div>"; 
    }
?>

This all goes quite easy, but since I would now also like to add the Titles of the pictures as captions, I need to extract/get this information from the image.

I'm thinking I can get it with something along the lines of the function exif_read_data , but I'm not quite sure how to get the title and not all the meta data . . .


With a little help from the smart people using stackoverflow, this is the final and functional result, as seen in my answer below as well and again, made with bits and pieces from several answers.

<?php
/*
This script will print the image files in a given folder, along with their image description (title in windows file explorer).
*/

// Set the directory where the files reside
$directory = $GLOBALS['directory'];

//get all image files with a .jpg extension.
$imagenpath = glob("" . $directory . "*.jpg");

$imgs = array();
// create array
foreach($imagenpath as $image){ $imgs[] = "$image"; }

// Print each image file with the ImageDescription (the title/caption) in the ALT field
foreach ($imgs as $img) {
    $exif_data = exif_read_data($img, 'IFD0');
    $exif_description = "";
    if (!empty($exif_data['ImageDescription'])) 
        $exif_description = $exif_data['ImageDescription'];

    echo "<div><img src='$img' alt='$exif_description' border='0' width=\"100%\" height=\"100%\"/ /></div>";
}
?>

Solution

  • thats everything that can be done, because most pictures are not supported with this function, though, you are doing it wrong because the error that you are getting is because the file is not found, the error you get when the file is found and is not supported is this:

    Warning: exif_read_data(file.png): File not supported in /path/to/file/file.php on line x

    and its because you single quoted the variable in

        $exif_data = exif_read_data($img, 'IFD0');
    

    the code could be less characters, this is my solution:

    <?php
    /*
    This script will print the image files in a given folder, along with their image description (title in windows file explorer).
    */
    
    // Set the directory where the files reside
    $directory = $GLOBALS['directory'];
    
    //get all image files with a .jpg extension.
    $imgs = glob($directory . "*.jpg");
    
    // Print each image file with the ImageDescription (the title/caption) in the ALT field
    foreach ($imgs as $img) {
        $exif_data = exif_read_data($img, 'IFD0');
        if (!empty($exif_data['ImageDescription'])) 
            $exif_description = $exif_data['ImageDescription'];
    
        echo "<div><img src=\"$img\" alt=\"$exif_description\" border=\"0\" width=\"100%\" height=\"100%\"/ /></div>";
    }
    ?>