From 9c56abbb83145488974f5e92f84957c1eee5a38e Mon Sep 17 00:00:00 2001 From: Rik Veenboer Date: Sun, 20 Dec 2015 01:00:28 +0000 Subject: [PATCH] Add tool for copying GPS data from JPG to DNG files --- composer.json | 3 +- gps-jpg2raw.php | 101 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 gps-jpg2raw.php diff --git a/composer.json b/composer.json index fe66677..7057684 100644 --- a/composer.json +++ b/composer.json @@ -2,6 +2,7 @@ "require": { "imagine/imagine": "0.5.0", "symfony/console": "2.4.3", - "symfony/yaml": "2.4.3" + "symfony/yaml": "2.4.3", + "phpexiftool/phpexiftool": "0.4.1" } } diff --git a/gps-jpg2raw.php b/gps-jpg2raw.php new file mode 100644 index 0000000..ee5f79d --- /dev/null +++ b/gps-jpg2raw.php @@ -0,0 +1,101 @@ + $sFile) { + printf("Processing %s (%d/%d)...\n", $sFile, $i, $iFiles); + + // Regenerate JPG and DNG file paths + $sJpgPath = sprintf('%s%s%s.jpg', $sDir, DIRECTORY_SEPARATOR, $sFile); + $sDngPath = sprintf('%s%s%s.dng', $sDir, DIRECTORY_SEPARATOR, $sFile); + + // Read from JPG file + printf(" > Reading from JPG file.\n"); + $oFileEntity = $oReader->files($sJpgPath)->first(); + $oReader->reset(); + + // Extract desired GPS tags + $aData = $aMetadataBag = array(); + $aKeys = array( + 'GPS:GPSAltitude' => 'altitude', + 'GPS:GPSLongitude' => 'longitude', + 'GPS:GPSLatitude' => 'latitude', + 'GPS:GPSImgDirection' => 'direction'); + + // Loop over all metadata + printf(" > Parsing metadata.\n"); + foreach ($oFileEntity as $oMetaData) { + $oTag = $oMetaData->getTag(); + $sTag = $oTag->getTagname(); + $oValue = $oMetaData->getValue(); + + // Store desired tags + if (array_key_exists($sTag, $aKeys)) { + $aData[$aKeys[$sTag]] = $oValue; + } + + // Store all GPS tags in metadata bag + if (strpos($sTag, 'GPS:') !== false) { + $aMetadataBag[] = $oMetaData; + } + } + + // Check presence of GPS data + if (empty($aData)) { + printf(" > No GPS data present!\n"); + continue; + } + + // Convert values to floats + $aData = array_map(function ($sValue) { + return floatval($sValue->asString()); + }, $aData); + + // Write to DNG file + printf(" > Writing to DNG file.\n"); + $oWriter->write($sDngPath, new MetadataBag($aMetadataBag)); +} \ No newline at end of file