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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。