From d022abee3896ea86a1b4795949027ce4a9dc0cdb Mon Sep 17 00:00:00 2001 From: Marek Szymczuk Date: Mon, 27 Jul 2015 17:03:00 +0200 Subject: [PATCH 1/6] Fixed normalization of zero degrees coordinates in the native mapper --- lib/PHPExif/Mapper/Native.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/PHPExif/Mapper/Native.php b/lib/PHPExif/Mapper/Native.php index fdc483b..ff39e98 100644 --- a/lib/PHPExif/Mapper/Native.php +++ b/lib/PHPExif/Mapper/Native.php @@ -225,6 +225,14 @@ protected function normalizeGPSComponent($component) { $parts = explode('/', $component); - return count($parts) === 1 ? $parts[0] : (int) reset($parts) / (int) end($parts); + if (count($parts) > 0) { + if ($parts[1]) { + return (int) $parts[0] / (int) $parts[1]; + } + + return 0; + } + + return $parts[0]; } } From 49b52782e35fd6c6cf576419e188c2829b55a8c6 Mon Sep 17 00:00:00 2001 From: Marek Szymczuk Date: Mon, 27 Jul 2015 17:04:02 +0200 Subject: [PATCH 2/6] Updated unit tests for GPS --- tests/PHPExif/Mapper/NativeMapperTest.php | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/tests/PHPExif/Mapper/NativeMapperTest.php b/tests/PHPExif/Mapper/NativeMapperTest.php index 5f2e11c..41e89df 100644 --- a/tests/PHPExif/Mapper/NativeMapperTest.php +++ b/tests/PHPExif/Mapper/NativeMapperTest.php @@ -175,17 +175,25 @@ 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', + ), ); - $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() From 040e0ebd335aff9c055dc212eadc5059dd591113 Mon Sep 17 00:00:00 2001 From: Marek Szymczuk Date: Mon, 27 Jul 2015 17:36:10 +0200 Subject: [PATCH 3/6] Added checks for "GPSLatitudeRef" and "GPSLongitudeRef". --- lib/PHPExif/Mapper/Exiftool.php | 27 +++++++++------------------ lib/PHPExif/Mapper/Native.php | 8 ++++++-- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/lib/PHPExif/Mapper/Exiftool.php b/lib/PHPExif/Mapper/Exiftool.php index 85de8ea..42cb39a 100644 --- a/lib/PHPExif/Mapper/Exiftool.php +++ b/lib/PHPExif/Mapper/Exiftool.php @@ -162,25 +162,16 @@ 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) { + $gpsLocation = sprintf( + '%s,%s', + (isset($data['GPSLatitudeRef'][0]) && strtoupper($data['GPSLatitudeRef'][0]) === 'S' ? -1 : 1) * $gpsData['lat'], + (isset($data['GPSLongitudeRef'][0]) && strtoupper($data['GPSLongitudeRef'][0]) === '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 ff39e98..7aa8812 100644 --- a/lib/PHPExif/Mapper/Native.php +++ b/lib/PHPExif/Mapper/Native.php @@ -179,13 +179,17 @@ public function mapRawData(array $data) $mappedData[$key] = $value; } + // add GPS coordinates, if available if (count($gpsData) === 2) { $gpsLocation = sprintf( '%s,%s', - (strtoupper($data['GPSLatitudeRef'][0]) === 'S' ? -1 : 1) * $gpsData['lat'], - (strtoupper($data['GPSLongitudeRef'][0]) === 'W' ? -1 : 1) * $gpsData['lon'] + (isset($data['GPSLatitudeRef'][0]) && strtoupper($data['GPSLatitudeRef'][0]) === 'S' ? -1 : 1) * $gpsData['lat'], + (isset($data['GPSLongitudeRef'][0]) && strtoupper($data['GPSLongitudeRef'][0]) === 'W' ? -1 : 1) * $gpsData['lon'] ); + $mappedData[Exif::GPS] = $gpsLocation; + } else { + unset($mappedData[Exif::GPS]); } return $mappedData; From 847dcf441514a2659f82627c6e5aaecc1abe4593 Mon Sep 17 00:00:00 2001 From: Marek Szymczuk Date: Mon, 27 Jul 2015 18:06:09 +0200 Subject: [PATCH 4/6] Fixed line length --- lib/PHPExif/Mapper/Exiftool.php | 7 +++++-- lib/PHPExif/Mapper/Native.php | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/PHPExif/Mapper/Exiftool.php b/lib/PHPExif/Mapper/Exiftool.php index 42cb39a..4db7aec 100644 --- a/lib/PHPExif/Mapper/Exiftool.php +++ b/lib/PHPExif/Mapper/Exiftool.php @@ -163,10 +163,13 @@ public function mapRawData(array $data) // add GPS coordinates, if available if (count($gpsData) === 2 && $gpsData['lat'] !== false && $gpsData['lon'] !== false) { + $latitudeRef = isset($data['GPSLatitudeRef'][0]) ? $data['GPSLatitudeRef'][0] : null; + $longitudeRef = isset($data['GPSLongitudeRef'][0]) ? $data['GPSLongitudeRef'][0] : null; + $gpsLocation = sprintf( '%s,%s', - (isset($data['GPSLatitudeRef'][0]) && strtoupper($data['GPSLatitudeRef'][0]) === 'S' ? -1 : 1) * $gpsData['lat'], - (isset($data['GPSLongitudeRef'][0]) && 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; diff --git a/lib/PHPExif/Mapper/Native.php b/lib/PHPExif/Mapper/Native.php index 7aa8812..c370905 100644 --- a/lib/PHPExif/Mapper/Native.php +++ b/lib/PHPExif/Mapper/Native.php @@ -181,10 +181,13 @@ public function mapRawData(array $data) // add GPS coordinates, if available if (count($gpsData) === 2) { + $latitudeRef = isset($data['GPSLatitudeRef'][0]) ? $data['GPSLatitudeRef'][0] : null; + $longitudeRef = isset($data['GPSLongitudeRef'][0]) ? $data['GPSLongitudeRef'][0] : null; + $gpsLocation = sprintf( '%s,%s', - (isset($data['GPSLatitudeRef'][0]) && strtoupper($data['GPSLatitudeRef'][0]) === 'S' ? -1 : 1) * $gpsData['lat'], - (isset($data['GPSLongitudeRef'][0]) && 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; From 8311c7ae68cf66afb084e380cf31e0982262f888 Mon Sep 17 00:00:00 2001 From: Marek Szymczuk Date: Tue, 28 Jul 2015 12:17:51 +0200 Subject: [PATCH 5/6] Updated conditions. --- lib/PHPExif/Mapper/Exiftool.php | 4 ++-- lib/PHPExif/Mapper/Native.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/PHPExif/Mapper/Exiftool.php b/lib/PHPExif/Mapper/Exiftool.php index 4db7aec..ab6fd4d 100644 --- a/lib/PHPExif/Mapper/Exiftool.php +++ b/lib/PHPExif/Mapper/Exiftool.php @@ -163,8 +163,8 @@ public function mapRawData(array $data) // add GPS coordinates, if available if (count($gpsData) === 2 && $gpsData['lat'] !== false && $gpsData['lon'] !== false) { - $latitudeRef = isset($data['GPSLatitudeRef'][0]) ? $data['GPSLatitudeRef'][0] : null; - $longitudeRef = isset($data['GPSLongitudeRef'][0]) ? $data['GPSLongitudeRef'][0] : null; + $latitudeRef = empty($data['GPSLatitudeRef'][0]) ? 'N' : $data['GPSLatitudeRef'][0]; + $longitudeRef = empty($data['GPSLongitudeRef'][0]) ? 'E' : $data['GPSLongitudeRef'][0]; $gpsLocation = sprintf( '%s,%s', diff --git a/lib/PHPExif/Mapper/Native.php b/lib/PHPExif/Mapper/Native.php index c370905..175a63b 100644 --- a/lib/PHPExif/Mapper/Native.php +++ b/lib/PHPExif/Mapper/Native.php @@ -181,8 +181,8 @@ public function mapRawData(array $data) // add GPS coordinates, if available if (count($gpsData) === 2) { - $latitudeRef = isset($data['GPSLatitudeRef'][0]) ? $data['GPSLatitudeRef'][0] : null; - $longitudeRef = isset($data['GPSLongitudeRef'][0]) ? $data['GPSLongitudeRef'][0] : null; + $latitudeRef = empty($data['GPSLatitudeRef'][0]) ? 'N' : $data['GPSLatitudeRef'][0]; + $longitudeRef = empty($data['GPSLongitudeRef'][0]) ? 'E' : $data['GPSLongitudeRef'][0]; $gpsLocation = sprintf( '%s,%s', From 3bf399f3706c6d1b51dbbe70a1a536b415e6256d Mon Sep 17 00:00:00 2001 From: Marek Szymczuk Date: Tue, 28 Jul 2015 17:42:42 +0200 Subject: [PATCH 6/6] Updated tests. --- lib/PHPExif/Mapper/Native.php | 12 ++++++++---- tests/PHPExif/Mapper/ExiftoolMapperTest.php | 15 +++++++++++++++ tests/PHPExif/Mapper/NativeMapperTest.php | 21 +++++++++++++++++++++ 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/lib/PHPExif/Mapper/Native.php b/lib/PHPExif/Mapper/Native.php index 175a63b..96eecad 100644 --- a/lib/PHPExif/Mapper/Native.php +++ b/lib/PHPExif/Mapper/Native.php @@ -219,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); } /** @@ -232,14 +236,14 @@ protected function normalizeGPSComponent($component) { $parts = explode('/', $component); - if (count($parts) > 0) { + if (count($parts) > 1) { if ($parts[1]) { - return (int) $parts[0] / (int) $parts[1]; + return intval($parts[0]) / intval($parts[1]); } return 0; } - return $parts[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 41e89df..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 @@ -188,6 +203,12 @@ public function testMapRawDataCorrectlyFormatsGPSData() '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', + ), ); foreach ($expected as $key => $value) {