doudou3935 2019-06-14 00:49
浏览 572
已采纳

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 05: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. :)

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部