Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Useful snippets for renaming photos from different cameras
<?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('../');
<?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('../');
@tomasfejfar

This comment has been minimized.

Show comment Hide comment
@tomasfejfar

tomasfejfar Jul 25, 2013

magic constant could be a parameter :P

magic constant could be a parameter :P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment