I'm trying to sort an array of image files by EXIF DateTime Original replacing the the original code used to sort the images by mtime using stat().
The original code used to sort the image files by mtime is as follows:
// display photos in album
$src_folder = $mainFolder.'/'.$_REQUEST['album'];
$src_files = array_diff(scandir($src_folder ), array('..', '.'));
$files = array();
/*** sort by most recent uploaded file ***/
foreach ($src_files as $key=>$img) {
$stat_folders = stat($src_folder .'/'. $img);
$file_time[$key] = $stat_folders['ctime'];
}
array_multisort($file_time, SORT_DESC, $src_files);
/*** end sort ***/
I've tested the following code that achieves the sorting of the array as required:
function getExifDate($filename)
{
$exif_odate = exif_read_data($filename, 0, true);
if($exif_odate === false){
return filemtime($filename);
}
if(isset($exif_odate['EXIF']['DateTimeOriginal'])){
return strtotime($exif_odate['EXIF']['DateTimeOriginal']);
} else {
return filemtime($filename);
}
}
$src_folder = 'folder/';
$src_files = glob($src_folder . '*.jpg');
array_multisort(array_map('getExifDate', $src_files), SORT_NUMERIC, SORT_DESC, $src_files);
However, when I try to integrate the new sort method using the following code:
// display photos in album
$src_folder = $mainFolder.'/'.$_REQUEST['album'];
$src_files = array_diff(scandir($src_folder ), array('..', '.'));
$files = array();
/*** sort files by date photo taken ***/
function getExifDate(){
global $src_folder, $src_files;
$src_images = array();
foreach ($src_files as $img){
$exif_odate = exif_read_data($img, 0, true);
// $exif_odate = exif_read_data($src_folder . $img, "FILE,COMPUTED,ANY_TAG,IFD0,THUMBNAIL,COMMENT,EXIF", true); <-- doesn't work either!
$src_date = strtotime($exif_odate['EXIF']['DateTimeOriginal']);
$src_images[$img] = $src_date;
}
}
array_multisort(array_map('getExifDate', $src_files), SORT_NUMERIC, SORT_DESC, $src_files);
/*** end sort ***/
When the page is loaded the following error message is generated for each of the image files attempted to be read:
Warning: exif_read_data(): Unable to open file in ...
referring to the following line of code:
$exif_odate = exif_read_data($img, 0, true);
Changing that line of code to the following makes no difference:
$exif_odate = exif_read_data($src_folder . $img, "FILE,COMPUTED,ANY_TAG,IFD0,THUMBNAIL,COMMENT,EXIF", true);
... and the image thumbnails are displayed unsorted ... inevitably.
Any guidance as to how to resolve this issue would be appreciated.