diff --git a/lib/PHPExif/Mapper/Exiftool.php b/lib/PHPExif/Mapper/Exiftool.php index 85de8ea..ab6fd4d 100644 --- a/lib/PHPExif/Mapper/Exiftool.php +++ b/lib/PHPExif/Mapper/Exiftool.php @@ -162,25 +162,19 @@ public function mapRawData(array $data) } // add GPS coordinates, if available - if (count($gpsData) === 2) { - $latitude = $gpsData['lat']; - $longitude = $gpsData['lon']; - - if ($latitude !== false && $longitude !== false) { - $gpsLocation = sprintf( - '%s,%s', - (strtoupper($data['GPSLatitudeRef'][0]) === 'S' ? -1 : 1) * $latitude, - (strtoupper($data['GPSLongitudeRef'][0]) === 'W' ? -1 : 1) * $longitude - ); - - $key = $this->map[self::GPSLATITUDE]; - - $mappedData[$key] = $gpsLocation; - } else { - unset($mappedData[$this->map[self::GPSLATITUDE]]); - } + if (count($gpsData) === 2 && $gpsData['lat'] !== false && $gpsData['lon'] !== false) { + $latitudeRef = empty($data['GPSLatitudeRef'][0]) ? 'N' : $data['GPSLatitudeRef'][0]; + $longitudeRef = empty($data['GPSLongitudeRef'][0]) ? 'E' : $data['GPSLongitudeRef'][0]; + + $gpsLocation = sprintf( + '%s,%s', + (strtoupper($latitudeRef) === 'S' ? -1 : 1) * $gpsData['lat'], + (strtoupper($longitudeRef) === 'W' ? -1 : 1) * $gpsData['lon'] + ); + + $mappedData[Exif::GPS] = $gpsLocation; } else { - unset($mappedData[$this->map[self::GPSLATITUDE]]); + unset($mappedData[Exif::GPS]); } return $mappedData; diff --git a/lib/PHPExif/Mapper/Native.php b/lib/PHPExif/Mapper/Native.php index fdc483b..96eecad 100644 --- a/lib/PHPExif/Mapper/Native.php +++ b/lib/PHPExif/Mapper/Native.php @@ -179,13 +179,20 @@ public function mapRawData(array $data) $mappedData[$key] = $value; } + // add GPS coordinates, if available if (count($gpsData) === 2) { + $latitudeRef = empty($data['GPSLatitudeRef'][0]) ? 'N' : $data['GPSLatitudeRef'][0]; + $longitudeRef = empty($data['GPSLongitudeRef'][0]) ? 'E' : $data['GPSLongitudeRef'][0]; + $gpsLocation = sprintf( '%s,%s', - (strtoupper($data['GPSLatitudeRef'][0]) === 'S' ? -1 : 1) * $gpsData['lat'], - (strtoupper($data['GPSLongitudeRef'][0]) === 'W' ? -1 : 1) * $gpsData['lon'] + (strtoupper($latitudeRef) === 'S' ? -1 : 1) * $gpsData['lat'], + (strtoupper($longitudeRef) === 'W' ? -1 : 1) * $gpsData['lon'] ); + $mappedData[Exif::GPS] = $gpsLocation; + } else { + unset($mappedData[Exif::GPS]); } return $mappedData; @@ -212,7 +219,11 @@ protected function extractGPSCoordinate(array $components) { $components = array_map(array($this, 'normalizeGPSComponent'), $components); - return intval($components[0]) + (intval($components[1]) / 60) + (floatval($components[2]) / 3600); + if (count($components) > 2) { + return intval($components[0]) + (intval($components[1]) / 60) + (floatval($components[2]) / 3600); + } + + return reset($components); } /** @@ -225,6 +236,14 @@ protected function normalizeGPSComponent($component) { $parts = explode('/', $component); - return count($parts) === 1 ? $parts[0] : (int) reset($parts) / (int) end($parts); + if (count($parts) > 1) { + if ($parts[1]) { + return intval($parts[0]) / intval($parts[1]); + } + + return 0; + } + + return floatval(reset($parts)); } } diff --git a/tests/PHPExif/Mapper/ExiftoolMapperTest.php b/tests/PHPExif/Mapper/ExiftoolMapperTest.php index 5de1db1..e4c1e87 100644 --- a/tests/PHPExif/Mapper/ExiftoolMapperTest.php +++ b/tests/PHPExif/Mapper/ExiftoolMapperTest.php @@ -116,6 +116,21 @@ public function testMapRawDataCorrectlyFormatsCreationDate() ); } + /** + * @group mapper + * @covers \PHPExif\Mapper\Exiftool::mapRawData + */ + public function testMapRawDataCorrectlyIgnoresIncorrectCreationDate() + { + $rawData = array( + \PHPExif\Mapper\Exiftool::CREATEDATE => '2015:04:01', + ); + + $mapped = $this->mapper->mapRawData($rawData); + + $this->assertEquals(false, reset($mapped)); + } + /** * @group mapper * @covers \PHPExif\Mapper\Exiftool::mapRawData diff --git a/tests/PHPExif/Mapper/NativeMapperTest.php b/tests/PHPExif/Mapper/NativeMapperTest.php index 5f2e11c..5eaff51 100644 --- a/tests/PHPExif/Mapper/NativeMapperTest.php +++ b/tests/PHPExif/Mapper/NativeMapperTest.php @@ -86,6 +86,21 @@ public function testMapRawDataCorrectlyFormatsDateTimeOriginal() ); } + /** + * @group mapper + * @covers \PHPExif\Mapper\Native::mapRawData + */ + public function testMapRawDataCorrectlyIgnoresIncorrectDateTimeOriginal() + { + $rawData = array( + \PHPExif\Mapper\Native::DATETIMEORIGINAL => '2015:04:01', + ); + + $mapped = $this->mapper->mapRawData($rawData); + + $this->assertEquals(false, reset($mapped)); + } + /** * @group mapper * @covers \PHPExif\Mapper\Native::mapRawData @@ -175,17 +190,31 @@ public function testMapRawDataFlattensRawDataWithSections() */ public function testMapRawDataCorrectlyFormatsGPSData() { - $result = $this->mapper->mapRawData( - array( + $expected = array( + '40.333452380952,-20.167314814815' => array( 'GPSLatitude' => array('40/1', '20/1', '15/35'), 'GPSLatitudeRef' => 'N', 'GPSLongitude' => array('20/1', '10/1', '35/15'), 'GPSLongitudeRef' => 'W', - ) + ), + '0,-0' => array( + 'GPSLatitude' => array('0/0', '0/0', '0/0'), + 'GPSLatitudeRef' => 'N', + 'GPSLongitude' => array('0/0', '0/0', '0/0'), + 'GPSLongitudeRef' => 'W', + ), + '71.706936,-42.604303' => array( + 'GPSLatitude' => array('71.706936'), + 'GPSLatitudeRef' => 'N', + 'GPSLongitude' => array('42.604303'), + 'GPSLongitudeRef' => 'W', + ), ); - $expected = '40.333452380952,-20.167314814815'; - $this->assertEquals($expected, reset($result)); + foreach ($expected as $key => $value) { + $result = $this->mapper->mapRawData($value); + $this->assertEquals($key, reset($result)); + } } public function testMapRawDataCorrectlyFormatsDifferentDateTimeString()