1

Can you please tell me how can I modify the following script so it outputs gallery with pictures sorted starting from the most recent files modified/added? The script uses GD library to work. Here is the script:

function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth ) 
{
  // open the directory
  $dir = opendir( $pathToImages );

  // loop through it, looking for any/all JPG files:
  while (false !== ($fname = readdir( $dir ))) {
// parse path for the extension
$info = pathinfo($pathToImages . $fname);
// continue only if this is a JPEG image
if ( strtolower($info['extension']) == 'jpg' ) 
{
  echo "Creating thumbnail for {$fname} <br />";

  // load image and get image size
  $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
  $width = imagesx( $img );
  $height = imagesy( $img );

  // calculate thumbnail size
  $new_width = $thumbWidth;
  $new_height = floor( $height * ( $thumbWidth / $width ) );

  // create a new tempopary image
  $tmp_img = imagecreatetruecolor( $new_width, $new_height );

  // copy and resize old image into new image 
  imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

  // save thumbnail into a file
  imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
}
  }
  // close the directory
  closedir( $dir );
}

function createGallery( $pathToImages, $pathToThumbs ) 
{
  echo "Creating gallery.html <br />";

  $output = "<html>";
  $output .= "<head><title>Thumbnails</title></head>";
  $output .= "<body>";
  $output .= "<table cellspacing=\"0\" cellpadding=\"2\" width=\"500\">";
  $output .= "<tr>";

  // open the directory
  $dir = opendir( $pathToThumbs );

  $counter = 0;
  // loop through the directory
  while (false !== ($fname = readdir($dir)))
  {
    // strip the . and .. entries out
    if ($fname != '.' && $fname != '..') 
    {
      $output .= "<td valign=\"middle\" align=\"center\"><a href=\"{$pathToImages}{$fname}\">";
      $output .= "<img src=\"{$pathToThumbs}{$fname}\" border=\"0\" />";
      $output .= "</a></td>";

      $counter += 1;
      if ( $counter % 4 == 0 ) { $output .= "</tr><tr>"; }
    }
  }
  // close the directory
  closedir( $dir );

  $output .= "</tr>";
  $output .= "</table>";
  $output .= "</body>";
  $output .= "</html>";

  // open the file
  $fhandle = fopen( "gallery.html", "w" );
  // write the contents of the $output variable to the file
  fwrite( $fhandle, $output ); 
  // close the file
  fclose( $fhandle );
}

// call createThumb function and pass to it as parameters the path 
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width. 
// We are assuming that the path will be a relative path working 
// both in the filesystem, and through the web for links
createThumbs("images/","thumbs/",100);
// call createGallery function and pass to it as parameters the path 
// to the directory that contains images and the path to the directory
// in which thumbnails will be placed. We are assuming that 
// the path will be a relative path working 
// both in the filesystem, and through the web for links
createGallery("images/","thumbs/"); 

Thanks.

xbakesx
  • 13,202
  • 6
  • 48
  • 76
  • possible duplicate of [glob() - sort by date](http://stackoverflow.com/questions/124958/glob-sort-by-date) – jeroen Jan 02 '14 at 20:01
  • Or http://stackoverflow.com/questions/2667065/sort-files-by-date-in-php – jeroen Jan 02 '14 at 20:02
  • By the way, I would generate the thumbnails once, when the image is uploaded, and not on every pageload of the gallery. – jeroen Jan 02 '14 at 20:03

1 Answers1

0

You need to modify createGallery and add another loop. In the first loop you read the directory to an array which keys are modification date. Then you sort it. Then you build the HTML based on a sorted array in the second loop.

I haven't had a chance to test it but this should give you an idea:

$thumbArray=array();
while (false !== ($fname = readdir($dir)))
  {
    // strip the . and .. entries out
    if ($fname != '.' && $fname != '..') 
    {

      $thumbArray[filemtime($fname)]=$fname;

    }
  }

  krsort($thumbArray);

  foreach($thumbArray as $file=>$path){
      $output .= "<td valign=\"middle\" align=\"center\"><a href=\"{$pathToImages}{$path}\">";
      $output .= "<img src=\"{$pathToThumbs}{$path}\" border=\"0\" />";
      $output .= "</a></td>";

      $counter += 1;
      if ( $counter % 4 == 0 ) { $output .= "</tr><tr>"; }

  }
gh0st
  • 214
  • 3
  • 15
  • Thank you for your answer. I modified this function but the line – user3154882 Jan 03 '14 at 04:22
  • Thank you for your answer. I modified this function but the line: $thumbArray[filemtime($fname)]=$fname; gives me the error: Warning: filemtime(): stat failed for 01.jpg in C:\xampp\htdocs\ANEW77\EUROCRAFT\GALLERIES\carving\create_thumbs.php on line 83. The last file in the directory is displayed though. Would you have any suggestions what`s the problem? – user3154882 Jan 03 '14 at 04:28
  • I would make sure that apache has privileges to read the file. You can add file_exists before that line to make sure. – gh0st Jan 03 '14 at 15:15
  • Thank you for all you suggestions. I really appreciate that. – user3154882 Jan 03 '14 at 15:29