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

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

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP