-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* wip * Refactoring
- Loading branch information
Showing
11 changed files
with
289 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
namespace App\Actions\Photos; | ||
|
||
use Illuminate\Http\UploadedFile; | ||
use Intervention\Image\Drivers\Gd\Driver; | ||
use Intervention\Image\ImageManager; | ||
|
||
class ExtractLocationFromPhotoAction implements ExtractsLocationFromPhoto | ||
{ | ||
public function run(UploadedFile $photo): array | ||
{ | ||
$manager = new ImageManager(new Driver()); | ||
$image = $manager->read($photo); | ||
$exif = $image->exif('GPS'); | ||
|
||
if ($this->isExifInvalid($exif)) { | ||
return []; | ||
} | ||
|
||
$result = $this->convertArrayToLatLng((array) $exif); | ||
|
||
if ($result['latitude'] === 0.0 && $result['longitude'] === 0.0) { | ||
return []; | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
private function convertArrayToLatLng(array $exif): array | ||
{ | ||
$GPSLatitudeRef = $exif['GPSLatitudeRef']; | ||
$GPSLatitude = $exif['GPSLatitude']; | ||
$GPSLongitudeRef = $exif['GPSLongitudeRef']; | ||
$GPSLongitude = $exif['GPSLongitude']; | ||
|
||
$latDegrees = $this->gpsToNumeric($GPSLatitude[0] ?? null); | ||
$latMinutes = $this->gpsToNumeric($GPSLatitude[1] ?? null); | ||
$latSeconds = $this->gpsToNumeric($GPSLatitude[2] ?? null); | ||
|
||
$lonDegrees = $this->gpsToNumeric($GPSLongitude[0] ?? null); | ||
$lonMinutes = $this->gpsToNumeric($GPSLongitude[1] ?? null); | ||
$lonSeconds = $this->gpsToNumeric($GPSLongitude[2] ?? null); | ||
|
||
$latDirection = ($GPSLatitudeRef === 'W' || $GPSLatitudeRef === 'S') ? -1 : 1; | ||
$lonDirection = ($GPSLongitudeRef === 'W' || $GPSLongitudeRef === 'S') ? -1 : 1; | ||
|
||
$latitude = $latDirection * ($latDegrees + ($latMinutes / 60) + ($latSeconds / (60 * 60))); | ||
$longitude = $lonDirection * ($lonDegrees + ($lonMinutes / 60) + ($lonSeconds / (60 * 60))); | ||
|
||
return ['latitude' => $latitude, 'longitude' => $longitude]; | ||
} | ||
|
||
private function gpsToNumeric(?string $coordinates): float | ||
{ | ||
if (! $coordinates) { | ||
return 0.0; | ||
} | ||
|
||
$parts = explode('/', $coordinates); | ||
|
||
if ($parts === []) { | ||
return 0.0; | ||
} | ||
|
||
if (count($parts) === 1) { | ||
return (float) $parts[0]; | ||
} | ||
|
||
if ((float) $parts[1] === 0.0) { | ||
return 0.0; | ||
} | ||
|
||
return (float) $parts[0] / (float) $parts[1]; | ||
} | ||
|
||
private function isExifInvalid(mixed $exif): bool | ||
{ | ||
return ! $exif || | ||
! isset($exif['GPSLatitudeRef']) || | ||
! isset($exif['GPSLatitude']) || | ||
! isset($exif['GPSLongitudeRef']) || | ||
! isset($exif['GPSLongitude']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace App\Actions\Photos; | ||
|
||
use Illuminate\Http\UploadedFile; | ||
|
||
interface ExtractsLocationFromPhoto | ||
{ | ||
public function run(UploadedFile $photo): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
16 changes: 16 additions & 0 deletions
16
database/migrations/2024_01_10_143605_add_location_info_to_photos.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
public function up(): void | ||
{ | ||
Schema::table('photos', function (Blueprint $table) { | ||
$table->decimal('latitude', 10, 8)->after('path')->nullable(); | ||
$table->decimal('longitude', 11, 8)->after('latitude')->nullable(); | ||
}); | ||
} | ||
}; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Tests\Doubles; | ||
|
||
use App\Actions\Photos\ExtractsLocationFromPhoto; | ||
use Illuminate\Http\UploadedFile; | ||
|
||
class FakeExtractLocationFromPhotoAction implements ExtractsLocationFromPhoto | ||
{ | ||
public function run(UploadedFile $photo): array | ||
{ | ||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters