-
<?php
-
/**
-
* This PHP5 CLI script iterates recursively through a directory tree and looks
-
* for image files, which it then tries to find in an Adobe Lightroom 2 catalog.
-
* If image is not found in Lightroom, its path and filename are printed out.
-
*
-
* Note that I have a dualbooting computer. I use Lightroom on Windows but I run
-
* this script on Linux.
-
* My Linux mount points '/media/win' and '/media/fat' equal to Windows drive
-
* letters 'C:' and 'E:'.
-
*
-
* @author Teemu Mäntynen
-
* @link http://www.mantynen.com/teemu/
-
* @license BSD http://www.opensource.org/licenses/bsd-license.php
-
*
-
* This software is provided "AS IS". NO WARRANTY. USE AT YOUR OWN RISK.
-
*/
-
-
//Path to root of photo directories
-
$path = realpath('/media/fat/kuvat/foto/raw');
-
-
//Adobe Lightroom SQLite3 catalog file
-
$db = new PDO('sqlite:/media/win/Documents and Settings/teemu/My Documents/My Pictures/Lightroom/Lightroom 2 Catalog.lrcat');
-
-
//Image file filename extensions
-
$imgFileExtensions = array(
-
'jpg', 'jpeg', 'tif', 'tiff', 'psd', 'bmp', '3fr', 'raf', 'crw', 'cr2', 'k25',
-
'kdc', 'dcs', 'dcr', 'drf', 'mrw', 'nef', 'nrw', 'orf', 'dng', 'ptx', 'pef',
-
'arw', 'srf', 'sr2', 'x3f', 'erf', 'mef', 'mos', 'raw', 'rw2', 'cap', 'iiq',
-
'r3d', 'fff', 'pxn', 'bay', 'rwz');
-
-
$ngImages = 0; $okImages = 0; $nonImages = 0;
-
-
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
-
-
foreach ($files as $name => $file) {
-
if (in_array(getExtension($file), $imgFileExtensions)) {
-
//Modify Linux path to Windows path ('/media/fat' is 'E:' and '/' is '\')
-
$pathname = str_replace('/', '\\', str_replace('/media/fat', 'E:', $name));
-
//'W' in the beginning of value of robustRepresentation stands for 'Windows'?
-
$sql = "SELECT COUNT(*) FROM AgLibraryFile WHERE robustRepresentation='W$pathname'";
-
if ($db->query($sql)->fetchColumn() == 0) {
-
//Image not found in Lightroom. Echo path and filename.
-
echo $file->getPathname()."\n";
-
//Be careful – unlink deletes the file! unlink($file->getPathname());
-
$ngImages++;
-
} else { $okImages++; }
-
} else {
-
//echo 'Not an image: '.$file->getPathname()."\n";
-
$nonImages++;
-
}
-
}
-
-
//echo 'Finished. OK: '.$okImages.' NG: '.$ngImages.' Non image: '.$nonImages."\n";
-
exit(0);
-
-
function getExtension(SplFileInfo $fileInfo)
-
{
-
$name = $fileInfo->GetFilename();
-
$extension = strrpos($name, ".", 1) + 1;
-
if ($extension != false) {
-
return strtolower(substr($name, $extension, strlen($name)-$extension));
-
} else { return ''; }
-
}
-
?>