Last active
December 20, 2015 05:29
-
-
Save mhujer/6078439 to your computer and use it in GitHub Desktop.
Useful snippets for renaming photos from different cameras
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
<?php | |
/** | |
* Useful snippets for renaming photos from different cameras | |
*/ | |
function fixUppercaseExtensions() | |
{ | |
foreach (glob('*.JPG') as $photo) { | |
$newName = str_replace('.JPG', '.jpg', $photo); | |
rename($photo, $newName); | |
echo sprintf('Renamed %s -> %s', $photo, $newName) . PHP_EOL; | |
} | |
} | |
function renameToDateTimeOriginal() | |
{ | |
foreach (glob('*.jpg') as $photo) { | |
$exifData = exif_read_data($photo); | |
$exifTime = str_replace(':', '-', $exifData['DateTimeOriginal']); | |
$exifTime = str_replace(' ', '_', $exifTime); | |
$newName = '../' . $exifTime . '_' . $photo; | |
rename($photo, $newName); | |
echo sprintf('Renamed %s -> %s', $photo, $newName) . PHP_EOL; | |
} | |
} | |
function renameToExitTimePlus(DateInterval $delta) | |
{ | |
foreach (glob('*.jpg') as $photo) { | |
$exifData = exif_read_data($photo); | |
$dt = new DateTime($exifData['DateTimeOriginal']); | |
$dt->add($delta); | |
$newName = '../' . $dt->format('Y-m-d_H-i-s') . '_' . $photo; | |
rename($photo, $newName); | |
echo sprintf('Renamed %s -> %s', $photo, $newName) . PHP_EOL; | |
} | |
} | |
function renameToExitTimeMinus(DateInterval $delta) | |
{ | |
foreach (glob('*.jpg') as $photo) { | |
$exifData = exif_read_data($photo); | |
$dt = new DateTime($exifData['DateTimeOriginal']); | |
$dt->sub($delta); | |
$newName = '../' . $dt->format('Y-m-d_H-i-s') . '_' . $photo; | |
rename($photo, $newName); | |
echo sprintf('Renamed %s -> %s', $photo, $newName) . PHP_EOL; | |
} | |
} | |
chdir('Tom'); | |
fixUppercaseExtensions(); | |
renameToExitTimePlus(new DateInterval('PT120S')); | |
chdir('../'); | |
chdir('Jakub'); | |
fixUppercaseExtensions(); | |
renameToExitTimeMinus(new DateInterval('P1DT19M40S')); | |
chdir('../'); | |
chdir('MartinH'); | |
renameToDateTimeOriginal(); | |
chdir('../'); | |
chdir('MartinV'); | |
renameToDateTimeOriginal(); | |
chdir('../'); |
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
<?php | |
/** | |
* Useful snippets for renaming photos from different cameras | |
*/ | |
function fixUppercaseExtensions() | |
{ | |
foreach (glob('*.JPG') as $photo) { | |
$newName = str_replace('.JPG', '.jpg', $photo); | |
rename($photo, $newName); | |
echo sprintf('Renamed %s -> %s', $photo, $newName) . PHP_EOL; | |
} | |
} | |
function renameToDateTimeOriginal() | |
{ | |
foreach (glob('*.jpg') as $photo) { | |
$exifData = exif_read_data($photo); | |
$exifTime = str_replace(':', '-', $exifData['DateTimeOriginal']); | |
$exifTime = str_replace(' ', '_', $exifTime); | |
$newName = $exifTime . '_' . $photo; | |
rename($photo, $newName); | |
echo sprintf('Renamed %s -> %s', $photo, $newName) . PHP_EOL; | |
} | |
} | |
function renameToFiletimePlus() | |
{ | |
foreach (glob('*.jpg') as $photo) { | |
$filetime = filemtime($photo); | |
//SELECT UNIX_TIMESTAMP('2013-07-17 09:37:56') - UNIX_TIMESTAMP('2007-01-03 15:38:07') | |
$filetime += (206211589 + 3600); //magic constant! | |
$filetime = date('Y-m-d_H-i-s', $filetime); | |
$newName = $filetime . '_' . $photo; | |
rename($photo, $newName); | |
echo sprintf('Renamed %s -> %s', $photo, $newName) . PHP_EOL; | |
} | |
} | |
// Šárka | |
chdir('Sarka'); | |
fixUppercaseExtensions(); | |
renameToDateTimeOriginal(); | |
chdir('../'); | |
// Pavel | |
chdir('Pavel'); | |
fixUppercaseExtensions(); | |
renameToDateTimeOriginal(); | |
chdir('../'); | |
//Petr mobil | |
chdir('Petr-mobil'); | |
renameToDateTimeOriginal(); | |
chdir('../'); | |
//Petr | |
chdir('Petr'); | |
fixUppercaseExtensions(); | |
renameToFiletimePlus(); | |
chdir('../'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
magic constant could be a parameter :P