2016-06-22 05:49:20 +00:00
< ? php
namespace App\Console\Commands ;
use Log ;
use Illuminate\Console\Command ;
use App\Model\Person ;
use App\Model\PhotoPerson ;
use App\Model\Photo ;
use App\Model\PhotoTag ;
use App\Model\Tag ;
class Import extends Command
{
2016-07-01 02:19:17 +00:00
use \App\Traits\Files ;
2016-06-22 05:49:20 +00:00
/**
* The name and signature of the console command .
*
* @ var string
*/
protected $signature = ' photo : import
{ -- dir = : Directory to Parse }
{ -- file = : File to import }
{ -- ignoredupe : Ignore duplicate files }
{ -- deletedupe : Delete duplicate files }
{ -- people = : People to reference in photo }
{ -- tags = : Add tag to photo } ' ;
/**
* The console command description .
*
* @ var string
*/
protected $description = 'Import photos into the database' ;
/**
* Create a new command instance .
*
* @ return void
*/
public function __construct ()
{
parent :: __construct ();
}
/**
* Execute the console command .
*
* @ return mixed
*/
public function handle ()
{
2016-07-01 02:19:17 +00:00
$files = $this -> getFiles ([ 'dir' => $this -> option ( 'dir' ), 'file' => $this -> option ( 'file' )]);
if ( ! count ( $files ))
exit ;
2016-06-22 21:07:44 +00:00
2016-06-22 05:49:20 +00:00
// Show a progress bar
$bar = $this -> output -> createProgressBar ( count ( $files ));
$bar -> setFormat ( " %current%/%max% [%bar%] %percent:3s%% (%memory%) (%remaining%) " );
$tags = NULL ;
$t = $p = array ();
// Tags
if ( $this -> option ( 'tags' ))
{
$tags = explode ( ',' , $this -> option ( 'tags' ));
2016-07-01 05:59:22 +00:00
$t = Tag :: whereIn ( 'tag' , $tags ) -> pluck ( 'id' ) -> toArray ();
2016-06-22 05:49:20 +00:00
}
// People
if ( $this -> option ( 'people' ))
{
$tags = explode ( ',' , $this -> option ( 'people' ));
2016-07-01 05:59:22 +00:00
$p = Person :: whereIn ( 'tag' , $tags ) -> pluck ( 'id' ) -> toArray ();
2016-06-22 05:49:20 +00:00
}
$c = 0 ;
foreach ( $files as $file )
{
$bar -> advance ();
if ( preg_match ( '/@__thumb/' , $file ) OR preg_match ( '/\/._/' , $file ))
{
$this -> warn ( sprintf ( 'Ignoring file [%s]' , $file ));
continue ;
}
if ( ! in_array ( strtolower ( pathinfo ( $file , PATHINFO_EXTENSION )), config ( 'photo.import.accepted' )))
{
$this -> warn ( sprintf ( 'Ignoring [%s]' , $file ));
continue ;
}
if ( $this -> option ( 'verbose' ))
$this -> info ( sprintf ( 'Processing file [%s]' , $file ));
$c ++ ;
$po = Photo :: where ( 'filename' , $file ) -> first ();
if ( is_null ( $po ))
{
$po = new Photo ;
$po -> filename = $file ;
}
2016-07-01 05:59:22 +00:00
$po -> date_taken = strtotime ( $po -> property ( 'exif:DateTime' ) ? $po -> property ( 'exif:DateTime' ) : $po -> property ( 'exif:DateTimeOriginal' ));
2016-06-30 06:01:12 +00:00
$po -> subsectime = $po -> property ( 'exif:SubSecTimeOriginal' );
2016-06-22 05:49:20 +00:00
2016-06-30 06:01:12 +00:00
$po -> signature = $po -> property ( 'signature' );
2016-06-22 05:49:20 +00:00
2016-06-30 06:01:12 +00:00
$po -> make = $po -> property ( 'exif:Make' );
$po -> model = $po -> property ( 'exif:Model' );
2016-06-22 05:49:20 +00:00
2016-06-30 06:01:12 +00:00
$po -> height = $po -> property ( 'height' );
$po -> width = $po -> property ( 'width' );
$po -> orientation = $po -> property ( 'orientation' );
2016-06-22 05:49:20 +00:00
2016-06-30 06:01:12 +00:00
$po -> gps_lat = Photo :: latlon ( preg_split ( '/,\s?/' , $po -> property ( 'exif:GPSLatitude' )), $po -> property ( 'exif:GPSLatitudeRef' ));
$po -> gps_lon = Photo :: latlon ( preg_split ( '/,\s?/' , $po -> property ( 'exif:GPSLongitude' )), $po -> property ( 'exif:GPSLongitudeRef' ));
2016-06-22 05:49:20 +00:00
try {
$po -> thumbnail = exif_thumbnail ( $po -> file_path ());
2016-06-29 10:49:02 +00:00
} catch ( \Exception $e ) {
2016-06-22 05:49:20 +00:00
// @todo Couldnt get the thumbnail, so we should create one.
}
// If this is a duplicate
2016-06-30 06:01:12 +00:00
$x = Photo :: whereIN ( 'id' , $po -> list_duplicate ()) -> get ();
2016-06-22 05:49:20 +00:00
if ( count ( $x )) {
$skip = FALSE ;
foreach ( $x as $o ) {
// We'll only ignore based on the same signature.
if ( $po -> signature != $o -> signature AND ! $po -> exists )
continue ;
if ( $this -> option ( 'ignoredupe' ))
{
$skip = TRUE ;
$this -> warn ( sprintf ( " Ignoring file [%s], it's the same as [%s] with id %s " , $po -> file_path (), $o -> filename , $o -> id ));
break ;
}
elseif ( $this -> option ( 'deletedupe' ) AND ( $po -> filename != $o -> filename ))
{
$skip = TRUE ;
$this -> error ( sprintf ( " Deleting file [%s], it's the same as [%s] with id %s and signature [%s] \n " , $po -> file_path (), $o -> filename , $o -> id , $po -> signature ));
unlink ( $po -> file_path ());
}
}
if ( $skip )
continue ;
$po -> duplicate = '1' ;
$this -> warn ( sprintf ( 'Image [%s] marked as a duplicate' , $file ));
}
if ( $po -> exists )
$this -> warn ( sprintf ( 'Image [%s] already in DB: %s' , $file , $po -> id ));
$po -> save ();
if ( $po -> wasRecentlyCreated )
$this -> info ( sprintf ( 'Image [%s] stored in DB: %s' , $file , $po -> id ));
2016-07-01 04:37:55 +00:00
// Record our people and tags
2016-07-01 05:59:22 +00:00
if ( $p )
$po -> People () -> sync ( $p );
if ( $t )
$po -> Tags () -> sync ( $t );
2016-06-22 05:49:20 +00:00
}
$bar -> finish ();
return $this -> info ( sprintf ( 'Images processed: %s' , $c ));
}
}