Sort pictures by date, newest first in PHP -
i found php script online helps me automatically display images in image folder, including thumbnails. works well, want sort pictures newest pictures first in webpage. can me this? many in advance!
<?php /* function: generates thumbnail */ function make_thumb($src,$dest,$desired_width) { /* read source image */ $source_image = imagecreatefromjpeg($src); $width = imagesx($source_image); $height = imagesy($source_image); /* find "desired height" of thumbnail, relative desired width */ $desired_height = floor($height*($desired_width/$width)); /* create new, "virtual" image */ $virtual_image = imagecreatetruecolor($desired_width,$desired_height); /* copy source image @ resized size */ imagecopyresized($virtual_image,$source_image,0,0,0,0,$desired_width,$desired_height,$width,$height); /* create physical thumbnail image destination */ imagejpeg($virtual_image,$dest); } /* function: returns files dir */ function get_files($images_dir,$exts = array('jpg')) { $files = array(); if($handle = opendir($images_dir)) { while(false !== ($file = readdir($handle))) { $extension = strtolower(get_file_extension($file)); if($extension && in_array($extension,$exts)) { $files[] = $file; } } closedir($handle); } return $files; } /* function: returns file's extension */ function get_file_extension($file_name) { return substr(strrchr($file_name,'.'),1); } /** settings **/ $images_dir = 'august/'; $thumbs_dir = 'august thumbnails/'; $thumbs_width = 200; $images_per_row = 4; /** generate photo gallery **/ $image_files = get_files($images_dir); if(count($image_files)) { $index = 0; foreach($image_files $index=>$file) { $index++; $thumbnail_image = $thumbs_dir.$file; if(!file_exists($thumbnail_image)) { $extension = get_file_extension($thumbnail_image); if($extension) { make_thumb($images_dir.$file,$thumbnail_image,$thumbs_width); } } echo '<a href="',$images_dir.$file,'"><img src="',$thumbnail_image,'" /></a>'; if($index % $images_per_row == 0) { echo '<div class="clear"></div>'; } } echo '<div class="clear"></div>'; } else { echo '<p>hang on! still growing.</p>'; } ?> if have questions, or need me clarify let me know.
jerome
use filemtime()
function get_files($images_dir,$exts = array('jpg')) { $files = array(); $times = array(); if($handle = opendir($images_dir)) { while(false !== ($file = readdir($handle))) { $extension = strtolower(get_file_extension($file)); if($extension && in_array($extension,$exts)) { $files[] = $file; $times[] = filemtime($images_dir . '/' . $file); } } closedir($handle); } array_multisort($files, sort_desc, $times); return $files; }
Comments
Post a Comment