Skip to content

Instantly share code, notes, and snippets.

@UstymUkhman
Last active January 15, 2024 08:30
Show Gist options
  • Save UstymUkhman/9e4b947d20eb92e448279f251163c65f to your computer and use it in GitHub Desktop.
Save UstymUkhman/9e4b947d20eb92e448279f251163c65f to your computer and use it in GitHub Desktop.
PHP script to read coordinates from geolocated photos.
#!/usr/bin/php
<?php
set_time_limit(0);
ini_set('memory_limit', -1);
if (!isset($argv[1]) || $argv[1] == '--help') {
$mssg = "\n\nTYPE: php coordsReader.php <path to a ZIP with photos> <path to a folder where unzip>\n\n";
getMessage($mssg, 33);
return;
}
if (!isset($argv[2])) {
$mssg = 'You haven\'t specified any destination folder.';
getMessage($mssg, 31);
return;
}
$file = $argv[1];
$path = $argv[2];
$zip = new ZipArchive();
$zip->open($file) or die('Unable to unzip `' . $file . '`');
$zip->extractTo($path . '/');
$zip->close();
$photos = scandir($path);
for ($i = 2; $i < count($photos); $i++) {
echo "\n\n_________________________________________________________\n\n";
echo "\033[34m Filename \033[0m: \033[37m" . $photos[$i] . "\033[0m\n\n";
$imgPath = $path . '/' . $photos[$i];
$data = exif_read_data($imgPath, 'IFD0') or die('`' . $photos[$i] . '`: info not found in file\'s header.');
$data = exif_read_data($imgPath, 0, TRUE);
$lat0 = explode('/', $data["GPS"]["GPSLatitude"][0]);
$lat1 = explode('/', $data["GPS"]["GPSLatitude"][1]);
$lat2 = explode('/', $data["GPS"]["GPSLatitude"][2]);
$degrees = $lat0[0] / $lat0[1];
$minutes = $lat1[0] / $lat1[1];
$seconds = $lat2[0] / $lat2[1];
$latitude = calculateCoordinate($degrees, $minutes, $seconds);
$lng0 = explode('/', $data["GPS"]["GPSLongitude"][0]);
$lng1 = explode('/', $data["GPS"]["GPSLongitude"][1]);
$lng2 = explode('/', $data["GPS"]["GPSLongitude"][2]);
$degrees = $lng0[0] / $lng0[1];
$minutes = $lng1[0] / $lng1[1];
$seconds = $lng2[0] / $lng2[1];
$longitude = calculateCoordinate($degrees, $minutes, $seconds);
echo "\033[34m Latitude \033[0m: \033[37m" . $latitude . "\033[0m\n";
echo "\033[34m Longitude \033[0m: \033[37m" . $longitude . "\033[0m\n";
echo "_________________________________________________________\n\n\n";
}
function calculateCoordinate($deg, $min, $sec) {
$res = $deg + $min / 60 + $sec / 3600;
return round($res * pow(10, 8)) / pow(10, 8);
}
function getMessage($message, $color) {
echo "\033[" . $color . "m\n" . $message . "\033[0m\n\n";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment