title: how to get image caption using php
body: <p>I'm using PHP to put all the images in a given folder into a slideshow.
  This works fine in the following code:</p>
  
  <pre><code>&lt;?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 "&lt;div&gt;&lt;img src='$img' border='0' width=\"100%\" height=\"100%\"/ /&gt;&lt;/div&gt;"; 
      }
  ?&gt;
  </code></pre>
  
  <p>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.</p>
  
  <p>I'm thinking I can get it with something along the lines of the function <code>exif_read_data</code> , but I'm not quite sure how to get the title and not all the meta data . . .</p>
  
  <hr>
  
  <p>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.</p>
  
  <pre><code>&lt;?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 "&lt;div&gt;&lt;img src='$img' alt='$exif_description' border='0' width=\"100%\" height=\"100%\"/ /&gt;&lt;/div&gt;";
  }
  ?&gt;
  </code></pre>