diff --git a/app/AlbumSources/IAlbumSource.php b/app/AlbumSources/IAlbumSource.php index 645d36f..c079100 100644 --- a/app/AlbumSources/IAlbumSource.php +++ b/app/AlbumSources/IAlbumSource.php @@ -8,6 +8,12 @@ use Symfony\Component\HttpFoundation\File\File; interface IAlbumSource { + /** + * Gets the absolute path or URL to the given photo file. + * @param Album $album + * @param Photo $photo + * @return string + */ function getPathToPhoto(Album $album, Photo $photo); /** diff --git a/app/Console/Commands/ProcessUploadCommand.php b/app/Console/Commands/ProcessUploadCommand.php index a00a3fa..8d08b46 100644 --- a/app/Console/Commands/ProcessUploadCommand.php +++ b/app/Console/Commands/ProcessUploadCommand.php @@ -12,6 +12,8 @@ use Illuminate\Support\Facades\Storage; class ProcessUploadCommand extends Command { + const METADATA_VERSION = 1; + /** * The name and signature of the console command. * @@ -68,6 +70,7 @@ class ProcessUploadCommand extends Command private function handlePhoto(UploadPhoto $uploadPhoto) { + /** @var Photo $photo */ $photo = $uploadPhoto->photo; $this->output->writeln(sprintf('Analysing photo #%d: %s', $photo->id, $photo->name)); @@ -77,6 +80,60 @@ class ProcessUploadCommand extends Command $photoFile = $albumSource->getPathToPhoto($album, $photo); - dump(@exif_read_data($photoFile)); + $exifData = @exif_read_data($photoFile); + dump($exifData); + + $photo->metadata_version = ProcessUploadCommand::METADATA_VERSION; + $photo->taken_at = $this->metadataDateTime($exifData); + $photo->camera_make = $this->metadataCameraMake($exifData); + $photo->camera_model = $this->metadataCameraModel($exifData); + $photo->camera_software = $this->metadataCameraSoftware($exifData); + $photo->save(); + } + + private function metadataCameraMake(array $exifData) + { + if (isset($exifData['Make'])) + { + return $exifData['Make']; + } + + return null; + } + + private function metadataCameraModel(array $exifData) + { + if (isset($exifData['Model'])) + { + return $exifData['Model']; + } + + return null; + } + + private function metadataCameraSoftware(array $exifData) + { + if (isset($exifData['Software'])) + { + return $exifData['Software']; + } + + return null; + } + + private function metadataDateTime(array $exifData) + { + $dateTime = null; + if (isset($exifData['DateTime'])) + { + $dateTime = $exifData['DateTime']; + } + + if (!is_null($dateTime)) + { + $dateTime = preg_replace('/^([\d]{4}):([\d]{2}):([\d]{2})/', '$1-$2-$3', $dateTime); + } + + return $dateTime; } } diff --git a/app/Photo.php b/app/Photo.php index 038d209..497757d 100644 --- a/app/Photo.php +++ b/app/Photo.php @@ -15,7 +15,17 @@ class Photo extends Model * @var array */ protected $fillable = [ - 'album_id', 'name', 'description', 'file_name', 'mime_type', 'file_size' + 'album_id', + 'name', + 'description', + 'file_name', + 'mime_type', + 'file_size', + 'metadata_version', + 'taken_at', + 'camera_make', + 'camera_model', + 'camera_software' ]; /** diff --git a/database/migrations/2016_09_02_220323_add_photo_metadata_columns.php b/database/migrations/2016_09_02_220323_add_photo_metadata_columns.php new file mode 100644 index 0000000..a005f0a --- /dev/null +++ b/database/migrations/2016_09_02_220323_add_photo_metadata_columns.php @@ -0,0 +1,40 @@ +dateTime('taken_at')->nullable(); + $table->integer('metadata_version')->nullable(); + $table->string('camera_make')->nullable(); + $table->string('camera_model')->nullable(); + $table->string('camera_software')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('photos', function (Blueprint $table) { + $table->dropColumn('taken_at'); + $table->dropColumn('metadata_version'); + $table->dropColumn('camera_make'); + $table->dropColumn('camera_model'); + $table->dropColumn('camera_software'); + }); + } +}