Created
December 11, 2024 12:46
-
-
Save janlis-ff/abff71b19933afa63b349e1a65ac2a0a to your computer and use it in GitHub Desktop.
A function to parse EXIF metadata from NIKON devices and NIKKOR lenses
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| * Reads metadata from EXIF data in a JPEG file. | |
| * | |
| * @param string $file_path Path to the JPEG file. | |
| * @return array Associative array of metadata. | |
| */ | |
| function read_meta_from_exif($path){ | |
| $exif = exif_read_data($path); | |
| $data = array(); | |
| // Get DateTime | |
| if(!isset($exif['DateTimeOriginal'])){ | |
| $data['date_time'] = null; | |
| }else{ | |
| // save as unix | |
| $data['date_time'] = strtotime($exif['DateTimeOriginal']); | |
| } | |
| // Get camera manufacturer | |
| if(!isset($exif['Make'])){ | |
| $data['make'] = null; | |
| }else{ | |
| $data['make'] = $exif['Make']; | |
| } | |
| // Get camera model | |
| if(!isset($exif['Model'])){ | |
| $data['model'] = null; | |
| }else{ | |
| $data['model'] = $exif['Model']; | |
| } | |
| // handle Xiaomi models | |
| if($data['make'] == "Xiaomi"){ | |
| if(isset($exif['UndefinedTag:0x9A00'])){ | |
| $data['model'] = $exif['UndefinedTag:0x9A00']; | |
| } | |
| } | |
| // Get lens manufacturer and model | |
| if($data['make'] == "NIKON CORPORATION"){ | |
| if(!isset($exif['UndefinedTag:0xA434'])){ | |
| $data['lens'] = null; | |
| }else{ | |
| $data['lens'] = $exif['UndefinedTag:0xA434']; | |
| } | |
| }else{ | |
| $data['lens'] = null; | |
| } | |
| // Get focal length | |
| if(isset($exif['FocalLength'])){ | |
| if(is_numeric($exif['FocalLength'])){ | |
| $data['focal_length'] = $exif['FocalLength']; | |
| }elseif(count(explode("/", $exif['FocalLength'])) == 2){ | |
| $parts = explode("/", $exif['FocalLength']); | |
| if(is_numeric($parts[0]) && is_numeric($parts[1])){ | |
| $data['focal_length'] = $parts[0] / $parts[1]; | |
| }else{ | |
| $data['focal_length'] = null; | |
| } | |
| }else{ | |
| $data['focal_length'] = null; | |
| } | |
| }elseif(isset($exif['FocalLengthIn35mmFilm'])){ | |
| if(is_numeric($exif['FocalLengthIn35mmFilm'])){ | |
| $data['focal_length'] = $exif['FocalLengthIn35mmFilm']; | |
| }elseif(count(explode("/", $exif['FocalLengthIn35mmFilm'])) == 2){ | |
| $parts = explode("/", $exif['FocalLengthIn35mmFilm']); | |
| if(is_numeric($parts[0]) && is_numeric($parts[1])){ | |
| $data['focal_length'] = $parts[0] / $parts[1]; | |
| }else{ | |
| $data['focal_length'] = null; | |
| } | |
| }else{ | |
| $data['focal_length'] = null; | |
| } | |
| }else{ | |
| $data['focal_length'] = null; | |
| } | |
| // Get exposure time | |
| if(!isset($exif['ExposureTime'])){ | |
| $data['exposure_time'] = null; | |
| }else{ | |
| $data['exposure_time'] = $exif['ExposureTime']; | |
| } | |
| // Format exposure time so that if it was e.g. 10/32000, it becomes 1/3200 | |
| if($data['exposure_time'] !== null){ | |
| $exposure_parts = explode("/", $data['exposure_time']); | |
| if(count($exposure_parts) == 2 && is_numeric($exposure_parts[0]) && is_numeric($exposure_parts[1])){ | |
| $exposure_parts[0] = intval($exposure_parts[0]); | |
| $exposure_parts[1] = intval($exposure_parts[1]); | |
| while($exposure_parts[0] % 10 == 0 && $exposure_parts[1] % 10 == 0){ | |
| $exposure_parts[0] /= 10; | |
| $exposure_parts[1] /= 10; | |
| } | |
| while($exposure_parts[0] % 5 == 0 && $exposure_parts[1] % 5 == 0){ | |
| $exposure_parts[0] /= 5; | |
| $exposure_parts[1] /= 5; | |
| } | |
| while($exposure_parts[0] % 2 == 0 && $exposure_parts[1] % 2 == 0){ | |
| $exposure_parts[0] /= 2; | |
| $exposure_parts[1] /= 2; | |
| } | |
| $data['exposure_time'] = $exposure_parts[0] . "/" . $exposure_parts[1]; | |
| } | |
| } | |
| // Get aperture | |
| if(!isset($exif['COMPUTED']) || !isset($exif['COMPUTED']['ApertureFNumber'])){ | |
| $data['aperture'] = null; | |
| }else{ | |
| $data['aperture'] = $exif['COMPUTED']['ApertureFNumber']; | |
| } | |
| // Get ISO | |
| if(!isset($exif['ISOSpeedRatings'])){ | |
| $data['iso'] = null; | |
| }else{ | |
| $data['iso'] = $exif['ISOSpeedRatings']; | |
| } | |
| // Get flash status | |
| if(!isset($exif['Flash'])){ | |
| $data['flash'] = null; | |
| }else{ | |
| if(is_numeric($exif['Flash'])){ | |
| $data['flash'] = $exif['Flash'] > 0; | |
| }else{ | |
| if(is_bool($exif['Flash'])){ | |
| $data['flash'] = $exif['Flash']; | |
| }else{ | |
| $data['flash'] = null; | |
| } | |
| } | |
| } | |
| return $data; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment