doudou3935 2019-06-14 08:49
浏览 570
已采纳

PHP exif_read_data():无法打开文件查询

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.

  • 写回答

1条回答 默认 最新

  • duanpai9945 2019-06-14 13:24
    关注

    Thanks to 04FS for helping me to resolve the issue:

    Simply replacing the following original code:

    /*** 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 ***/
    

    with:

    /*** sort files by date photo taken ***/
    foreach ($src_files as $key=>$img){       
        $exif_odate = exif_read_data($src_folder . '/' . $img, 0, true);
        $file_time[$key] = strtotime($exif_odate['EXIF']['DateTimeOriginal']);
    }
    array_multisort($file_time, SORT_NUMERIC, SORT_DESC, $src_files);
    /*** end sort ***/
    

    resolved the sorting issue. :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算