1 ? 'ies' : 'y';
out(count($arrFnDir) ." director$s to process.");
// counters
$i = 0; // dir
$j = 0; // img file updated for GPS
$k = 0; // files updated for keywords
$l = 0; // files updated for caption
$m = 0; // files updated for star
// For each directory
foreach ($arrFnDir as $fnDir) {
$i++;
if (is_dir($fnDir)) {
out("\n$i/$intDir - Processing $fnDir");
}
else {
out("\n$i/$intDir - $fnDir is not a directory");
continue;
}
// Check which ini file to use
$fnIni = '';
foreach ($arrPicasaIni as $picasaIni) {
if (file_exists($fnDir . $picasaIni)) {
$fnIni = $fnDir . $picasaIni;
out(' Using '. $picasaIni);
break;
}
}
if ($fnIni == '') {
out(' No picasa ini file found. Skipping.');
}
else {
// Parsing ini file
out(' Parsing '. $fnIni);
$arrIni = new_parse_ini_file($fnIni, true);
if (!$arrIni) {
out("Unable to read $fnIni");
continue;
}
// For each entry in the picasa ini file
foreach($arrIni as $strImageName => $arrImgProperties) {
$arrPathParts = pathinfo($fnDir . $strImageName);
// Processing only required file types...
if (in_array($arrPathParts['extension'], $arrExt)) {
// ...and existing files
if (!file_exists($fnDir . $strImageName)) {
out("$strImageName entry. File not found");
continue;
}
$strCommand = "$fnExifTool $strETOptions";
// If we process GPS tags
if ($booProcessGps) {
if (array_key_exists('geotag', $arrImgProperties)) {
$j++;
$strGeotag = trim($arrImgProperties['geotag']);
out("\n * Found $strImageName entry with geotag : $strGeotag");
$arrGeotag = explode(',', $strGeotag);
$x = $arrGeotag[1];
$y = $arrGeotag[0];
$lonRef = $x >= 0 ? 'E' : 'W';
$latRef = $y >= 0 ? 'N' : 'S';
$lon = abs($x);
$lat = abs($y);
out(" Preparing $strImageName : $latRef$lat $lonRef$lon");
$strCommand .= " -GPSLatitude=$lat -GPSLatitudeRef=$latRef -GPSLongitude=$lon -GPSLongitudeRef=$lonRef -GPSAltitude=0 -GPSAltitudeRef#=0";
}
}
// If we process keywords
if ($booProcessKeywords) {
if (array_key_exists('keywords', $arrImgProperties)) {
$k++;
$strKeywords = trim($arrImgProperties['keywords']);
out("\n * Found $strImageName entry with keywords : $strKeywords");
$arrKeywords = explode(',', $strKeywords);
array_walk($arrKeywords, 'addQuotes');
$strKeywordsExiftool = ' -keywords='. implode(' -keywords=', $arrKeywords);
out(" Preparing $strImageName : $strKeywordsExiftool");
$strCommand .= " $strKeywordsExiftool";
}
}
// If we process caption tags
if ($booProcessCaption) {
if (array_key_exists('caption', $arrImgProperties)) {
$l++;
$strCaption = trim($arrImgProperties['caption']);
out("\n * Found $strImageName entry with caption : $strCaption");
out(" Preparing $strImageName : $strCaption");
$strCommand .= " -title=\"$strCaption\" \"$fnDir$strImageName\"";
}
}
// If we process star tags
if ($booProcessStar) {
if (array_key_exists('star', $arrImgProperties)) {
$strStar = trim($arrImgProperties['star']);
out("\n * Found $strImageName entry with star : $strStar");
if ($strStar == 'yes') {
$m++;
out(" Preparing $strImageName : 5");
$strCommand .= " -Rating=5";
}
}
}
// Run the command
$strCommand .= ' '. $fnDir . $strImageName;
out(' Running command '. $strCommand);
system($strCommand);
}
}
}
}
out("\n$i director$s processed");
$s2 = $j > 1 ? 's' : '';
out("GPS : $j image file$s2 updated");
$s3 = $k > 1 ? 's' : '';
out("Keywords : $k image file$s3 updated");
$s4 = $l > 1 ? 's' : '';
out("Caption : $l image file$s4 updated");
$s5 = $m > 1 ? 's' : '';
out("Star : $m image file$s5 updated");
/* Helper functions
---------------------*/
/* Print messages */
function out($strMessage, $strFormat = STR_OUTPUT_LOG_FORMAT) {
if ($strFormat == 'html') {
echo(str_replace("\n", '
', $strMessage) .'
');
}
else {
//echo(iconv('UTF-8', 'CP850', $strMessage) ."\n"); // windows
echo($strMessage ."\n");
}
}
/* read an ini file */
function new_parse_ini_file($f) {
// if cannot open file, return false
if (!is_file($f)) {
return false;
}
$ini = file($f);
// to hold the categories, and within them the entries
$cats = array();
foreach ($ini as $i) {
if (@preg_match('/\[(.+)\]/', $i, $matches)) {
$last = $matches[1];
}
elseif (@preg_match('/(.+)=(.+)/', $i, $matches)) {
$cats[$last][$matches[1]] = $matches[2];
}
}
return $cats;
}
/* An array of sub-directories */
function directoryToArray($directory) {
$array_items = array();
if ($handle = opendir($directory)) {
while (false !== ($file = readdir($handle))) {
if ($file != '.' && $file != '..') {
$file = $directory . $file .'/';
if (is_dir($file)) {
$array_items = array_merge($array_items, directoryToArray($file));
$array_items[] = $file;
}
}
}
closedir($handle);
}
return array_reverse($array_items);
}
/* Add quotes */
function addQuotes(&$item) {
$item = "\"". $item ."\"";
}
function getInput(){
$stdin = fopen("php://stdin", 'r');
$input = fgets($stdin, 1024);
$input = trim($input);
fclose($stdin);
return $input;
}
?>