Skip to content

Commit

Permalink
Implement string check before calling
Browse files Browse the repository at this point in the history
Signed-off-by: Clint Johnson <50661551+nmbgeek@users.noreply.github.com>
  • Loading branch information
nmbgeek committed Sep 23, 2024
1 parent d56659a commit e64f278
Showing 1 changed file with 6 additions and 21 deletions.
27 changes: 6 additions & 21 deletions lib/Listener/ExifMetadataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ public function handle(Event $event): void {
$gps['longitude'] = $this->gpsDegreesToDecimal($rawExifData['GPS']['GPSLongitude'], $rawExifData['GPS']['GPSLongitudeRef']);
}

if (array_key_exists('GPSAltitude', $rawExifData['GPS']) && array_key_exists('GPSAltitudeRef', $rawExifData['GPS'])) {
$gps['altitude'] = ($rawExifData['GPS']['GPSAltitudeRef'] === "\u{0000}" ? 1 : -1) * $this->parseGPSData($rawExifData['GPS']['GPSAltitude']);
}
if (array_key_exists('GPSAltitude', $rawExifData['GPS']) && array_key_exists('GPSAltitudeRef', $rawExifData['GPS']) && is_string($rawExifData['GPS']['GPSAltitude'])) {
$gps['altitude'] = ($rawExifData['GPS']['GPSAltitudeRef'] === '1' ? -1 : 1) * $this->parseGPSData($rawExifData['GPS']['GPSAltitude']);
}

if (!empty($gps)) {
$event->getMetadata()->setArray('photos-gps', $gps);
Expand All @@ -109,15 +109,11 @@ public function handle(Event $event): void {
* @param array|string $coordinates
*/
private function gpsDegreesToDecimal($coordinates, ?string $hemisphere): float {
if (is_null($coordinates)) {
throw new \Exception('Coordinates are null');
}

if (is_string($coordinates)) {
$coordinates = array_map('trim', explode(',', $coordinates));
}

if (!is_array($coordinates) || count($coordinates) !== 3) {
if (count($coordinates) !== 3) {
throw new \Exception('Invalid coordinate format: ' . json_encode($coordinates));
}

Expand All @@ -126,19 +122,8 @@ private function gpsDegreesToDecimal($coordinates, ?string $hemisphere): float {
$sign = ($hemisphere === 'W' || $hemisphere === 'S') ? -1 : 1;
return $sign * ($degrees + $minutes / 60 + $seconds / 3600);
}

/**
* @param array|string $rawData
*/
private function parseGPSData($rawData): float {
if (is_array($rawData)) {
if (count($rawData) === 3) {
return floatval($rawData[0]) + floatval($rawData[1]) / 60 + floatval($rawData[2]) / 3600;
} else {
return 0; // Handle unexpected array structure
}
}


private function parseGPSData(string $rawData): float {
$parts = explode('/', $rawData);

if ($parts[1] === '0') {
Expand Down

0 comments on commit e64f278

Please sign in to comment.