Skip to content

Instantly share code, notes, and snippets.

@mhujer
Last active December 20, 2015 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhujer/6078439 to your computer and use it in GitHub Desktop.
Save mhujer/6078439 to your computer and use it in GitHub Desktop.
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
Copy link

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