Riordinare immagini leggendo la data di scatto

 

Semplice script PHP CLI che genera una gerarchia di cartelle ordinate per anno, mese e giorno di ogni fotografia trovata.

La soluzione usa l'EXIF trovato nelle jpeg o il timestamp salvato nel nome file da whatsapp:

 

#!/usr/bin/php
<?php
if( count($argv) != 3 ) die("\n\e[95mRe-order images by exif data\n\n\e[93mUsage: \e[91m(Use absolute paths!)\e[39m\n\e[93m$_SERVER[SCRIPT_NAME] /mnt/storage/source /mnt/storage/destination\e[39m\n\n");
if( !is_dir($argv[1]) ) die("\e[31mSource not found!\e[39m\n");
if( !is_dir($argv[2]) ) die("\e[31mDestination not found!\e[39m\n");

$source = rtrim($argv[1], '/');
$destination = rtrim($argv[2], '/');

echo "\n\e[92mScanning source...\e[39m\n";
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source));

$files = array();
foreach ($rii as $file) {
    if (!$file->isDir())
        $files[] = $file->getPathname();
}

$imagesFound   = 0;
$imagesCopied  = 0;
$unknownCopied = 0;
$unknownFolder = 'Data-sconosciuta';

foreach( $files as $file ){
        $ext = pathinfo($file, PATHINFO_EXTENSION);

        if( strtolower($ext) != 'jpg' and strtolower($ext) != 'jpeg' ) continue;
        $imagesFound++;

        $exif = @exif_read_data($file);
        // exif media
        if( isset($exif['DateTimeOriginal']) ) {
                $dataTime = explode(' ', $exif['DateTimeOriginal']);
                list($y,$m,$d) = explode(':',$dataTime[0]);
                $folderName = "$d-$m-$y";
                if( !(is_dir("$destination/$y/$folderName")) ) {
                        if( mkdir("$destination/$y/$folderName", 0777, true) ) echo "\e[92m$folderName created\e[39m\n";
                }
                if ( copy($file, "$destination/$y/$folderName/".basename($file)) ) {
                        echo "\e[95m". basename($file) ."\e[92m >>> \e[95m$y/$folderName/\n";
                        $imagesCopied++;
                }
        }elseif( strlen(basename($file)) == strlen("IMG-20140510-WA0004.jpg") AND strpos(basename($file), 'WA') !== false ) {
        // whatsapp media
                $year   = substr(basename($file), 4,4);
                $month  = substr(basename($file), 8,2);
                $day    = substr(basename($file), 10,2);
                $folderName = "$day-$month-$year";

                if( !(is_dir("$destination/$year/$folderName")) ) {
                        if( mkdir("$destination/$year/$folderName", 0777, true) ) echo "\e[92m$folderName created\e[39m\n";
                }
                if ( copy($file, "$destination/$year/$folderName/".basename($file)) ) {
                        echo "\e[95m". basename($file) ."\e[92m >>> \e[95m$year/$folderName/\n";
                        $imagesCopied++;
                }

        }else{
        // unknown time period :(
                if( !(is_dir("$destination/$unknownFolder")) ) {
                        if( mkdir("$destination/$unknownFolder") ) echo "$unknownFolder created\n";
                }
                if ( copy($file, "$destination/$unknownFolder/".basename($file)) ) {
                        echo "\e[95m". basename($file) ."\e[91m >>> \e[95m$unknownFolder\n";
                        $unknownCopied++;
                }
        }
}
echo "
\e[93mTotal files: \e[92m".count($files)."
\e[93mTotal images found: \e[92m$imagesFound

\e[93mIdentified images copied: \e[92m$imagesCopied
\e[93mUnknown data images copied: \e[92m$unknownCopied
\e[39m\n";

?>

 

Lascia un commento


 
 
 Informazioni

Questo sito web utilizza i cookie. Continuando ad utilizzare il sito web, acconsentite al l'uso dei cookies. Dimmi di più...