简体   繁体   中英

Get image src from an Array Key value and display in PHP

I've used the following code example ( https://github.com/josephtinsley/JQueryEXIFImageTags/blob/master/process.php ) and modified it to scan a directory of images and place into an array with matching exif of DateTimeOriginal.

    <?php

    $date = new DateTime();
    $date->setDate(2015, 9, 11);
    $timenow = $date->format('Y:m:d H:i:s');

    $dir = './img/';
    $files = scandir($dir);
    $ext_list = ['jpg', 'gif', 'png'];
    foreach($files as $image_file)
    {
        $l = strtolower($image_file);
        $parse_file_name = explode(".", $l);
        $file_ext = end($parse_file_name); 

        if(in_array($file_ext, $ext_list) )
        {
            $exif_data = exif_read_data($dir.$image_file);
            $photos[] = [
                'FileName'          =>$exif_data['FileName'],
                'DateTimeOriginal'  =>$exif_data['DateTimeOriginal'],
                'TimeNow'           =>$timenow,
            ];
        } 
    }
?>

Ive then wrote an array_search with an if statement to output the key number if there is a match between the exif data of 'DateTimeOriginal' and $timenow (which ive set as a fixed date) on browser refresh, or say 'nope' if no match

<?php

if ($key = array_search($timenow, array_column($photos, 'DateTimeOriginal')))
{
  echo $key;
} else {
  echo "nope";
} 

?>

This works and the behaviour is as expected. however, what i ultimately want it to do is to display the image that matches with the $key value of the array but im stuggling to understand how to do this.

I would appreciate any help in the right direction.

Probably not the best way but i solved this with

<img src="<?php echo $dir.$photos[$key]['FileName']; ?>">

my inexperience with php and arrays caused this, but after a bit reading ive bodged it and its works.

thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM