diff --git a/leaf.php b/leaf.php index fb232c8..4075ada 100644 --- a/leaf.php +++ b/leaf.php @@ -1,8 +1,7 @@ -#!/usr/bin/php png size multiplier -padjust= png levels adjustment (otherwise calculated from adjust) makepdf combine images into a pdf with img2pdf +match move files and set crop values to match source dir + -source= read FROM dir profile apply xmp profile to images (requires exiv2 > 0.25) -file= xmp profile resort reorder image sequence by adding a new image @@ -207,6 +208,53 @@ strip strip exif crop values from images with exiftool echo $help; fin(); +//////////////////////////////////////////////////////////////////////////////////////////////// +// Note: Match +////////////////////// +} elseif (args("app") == "match") { +echo Welcome("Match directories"); +///////////////////////////////////////////////////////////////////////// + +$source = chop(args("source"), "/")."/"; +$files = glob($source."[0-9][0-9][0-9]-*"); +if (!$files) { + msg("No valid files in source dir!",1); + } + +foreach ($files as $file) { + $ext = pathinfo($file,PATHINFO_EXTENSION); + $key = substr(basename($file), 0, 3); + $match = glob(args("dir")."*".substr(basename($file, ".".$ext), 4)."*"); + if (isset($match[0]) && file_exists($match[0])) { + $dest = $match[0]; + $dparts = pathinfo($dest); + if (basename($file,$ext) != basename($dest,$dparts['extension'])) { + $newdest = $dparts['dirname']."/".$key."-".$dparts['basename']; + msg("Moving ".$dest." to ".$newdest); + //rename($dest,$newdest); + } + $parts = chop(shell_exec("exiftool -s -s -s -XMP-crs:CropTop -XMP-crs:CropRight -XMP-crs:CropLeft -XMP-crs:CropBottom -XMP-crs:CropAngle ".$file." 2>&1")); + @list($top, $right, $left, $bottom, $angle) = explode("\n", $parts); + if (isset($top) || isset($right) || isset($left) || isset($bottom) || isset($angle)) { + $info = "Writing crop information from ".$file." to ".$newdest; + $info .= " Top: ".bashcolor($top,"green"); + $info .= " Right: ".bashcolor($right,"green"); + $info .= " Left: ".bashcolor($left,"green"); + $info .= " Bottom: ".bashcolor($bottom,"green"); + $info .= " Angle: ".bashcolor($angle,"green"); + msg($info); + //exec("exiftool -overwrite_original -XMP-crs:HasCrop=1 -XMP-crs:AlreadyApplied=0 -XMP-crs:CropTop=".$top." -XMP-crs:CropRight=".$right." -XMP-crs:CropLeft=".$left." -XMP-crs:CropBottom=".$bottom." -XMP-crs:CropAngle=".$angle." ".$file); + } else { + msg("Source file ".$file." does not contain crop information"); + } + } else { + msg("No match found for ".$file." in destination"); + } + + } + +fin(); + //////////////////////////////////////////////////////////////////////////////////////////////// // Note: Generate ////////////////////// @@ -276,19 +324,19 @@ foreach ($files as $file) { echo "\n\n"; echo "Bitmap files: "; -if (is_array($bitmap)) { echo count($bitmap); } else { echo "None"; } +if (isset($bitmap)) { echo count($bitmap); } else { echo "None"; } echo "\n"; echo "Greyscale files: "; -if (is_array($grey)) { echo count($grey); } else { echo "None"; } +if (isset($grey)) { echo count($grey); } else { echo "None"; } echo "\n"; echo "Color files: "; -if (is_array($color)) { echo count($color); } else { echo "None"; } +if (isset($color)) { echo count($color); } else { echo "None"; } echo "\n"; echo "Spine files: "; -if (is_array($spine)) { echo count($spine); } else { echo "None"; } +if (isset($spine)) { echo count($spine); } else { echo "None"; } echo "\n\n"; if (count($spine) > 1) { @@ -310,8 +358,8 @@ if ($ratioavg > 1) { // page is tall $paper_height = number_format($paper_width*$ratioavg,2); } -$dpi = $page_height / $paper_height; -$canonicalpngdpi = $dpi*$multiplier; +$dpi = number_format($page_height/$paper_height,2); +$canonicalpngdpi = number_format($dpi*$multiplier,2); $pngdpi = $canonicalpngdpi*39.37007874016; $dims = $page_width."x".$page_height; $pngdims = ($page_width*$multiplier)."x".($page_height*$multiplier); @@ -327,12 +375,14 @@ foreach ($color as $parts) { $jpg[] = $parts; } -foreach ($grey as $parts) { - $parts[] = 1; - $jpg[] = $parts; +if (isset($grey)) { + foreach ($grey as $parts) { + $parts[] = 1; + $jpg[] = $parts; + } } -if (is_array($spine)) { +if (isset($spine)) { $spine[0][] = 2; $jpg[] = $spine[0]; } @@ -380,49 +430,53 @@ echo "\n"; msg("Setting DPI: "); multiexec($thread_b,$xt); -foreach ($bitmap as $parts) { - - $file = $parts[0]; - $ext = pathinfo($file,PATHINFO_EXTENSION); - $output = $dest."/".basename($file,$ext)."png"; - - $msg = "Processing bitmap page ".$file." @ ".$pngdims."!"; - $cmd = "convert -resize ".$pngdims."\! "; - - if (args("adjust")) { - $adjust = args("adjust"); - } else { - $adjust = "0%,100%,1"; - } +if (isset($bitmap)) { + + foreach ($bitmap as $parts) { + + $file = $parts[0]; + $ext = pathinfo($file,PATHINFO_EXTENSION); + $output = $dest."/".basename($file,$ext)."png"; + + $msg = "Processing bitmap page ".$file." @ ".$pngdims."!"; + $cmd = "convert -resize ".$pngdims."\! "; + + if (args("adjust")) { + $adjust = args("adjust"); + } else { + $adjust = "0%,100%,1"; + } + + $parts = explode(",", $adjust); + $black = rtrim($parts[0],"%"); + $white = rtrim($parts[1],"%"); + $gamma = $parts[2]; + + if (args("padjust")) { + $radjust = args("padjust"); + } else { + $radjust = ($black+60)."%".",".($white-15)."%".",".($gamma-.7); + } + + $msg .= " (".$radjust.")"; + $cmd .= "-level ".$radjust." "; + + $cmd .= "-monochrome -colors 2 -depth 1 -negate ".$file." ".$output; + + $thread_c[] = array($msg, $cmd); + $thread_d[] = array(".","exiftool -overwrite_original -PixelsPerUnitX=".$pngdpi." -PixelsPerUnitY=".$pngdpi." ".$output); - $parts = explode(",", $adjust); - $black = rtrim($parts[0],"%"); - $white = rtrim($parts[1],"%"); - $gamma = $parts[2]; - - if (args("padjust")) { - $radjust = args("padjust"); - } else { - $radjust = ($black+60)."%".",".($white-15)."%".",".($gamma-.7); } - $msg .= " (".$radjust.")"; - $cmd .= "-level ".$radjust." "; - - $cmd .= "-monochrome -colors 2 -depth 1 -negate ".$file." ".$output; - - $thread_c[] = array($msg, $cmd); - $thread_d[] = array(".","exiftool -overwrite_original -PixelsPerUnitX=".$pngdpi." -PixelsPerUnitY=".$pngdpi." ".$output); + echo "\n\n"; + msg("Beginning multithreaded convert with ".$xt." threads"); + multiexec($thread_c,$xt); + echo "\n"; + msg("Setting DPI: "); + multiexec($thread_d,$xt); } -echo "\n\n"; -msg("Beginning multithreaded convert with ".$xt." threads"); -multiexec($thread_c,$xt); -echo "\n"; -msg("Setting DPI: "); -multiexec($thread_d,$xt); - fin(); //////////////////////////////////////////////////////////////////////////////////////////////// @@ -436,9 +490,7 @@ msg("Remove scratch files?",2); if (!is_dir("scratch")) { msg("Problem with scratch dir",2); } else { - foreach (glob("scratch/*.*") as $file) { - unlink($file); - } + exec("/opt/local/bin/trash -a scratch/"); } fin(); @@ -1007,7 +1059,7 @@ if (file_exists($dest)) { if (file_exists($dest)) { msg("Freak accident",1); } } echo "Creating pdf...\n\n"; -exec("img2pdf --verbose --viewer-page-layout twocolumnright --creator 'Leaf ".$version."' --output ".$dest." ".$input); +exec("img2pdf --verbose --viewer-page-layout twocolumnright --author 'Leaf ".$version."' --output ".$dest." ".$input); echo "\n"; msg("Press return to open in Acrobat",2); exec("open ".$dest." -b com.adobe.Acrobat.Pro"); @@ -1088,7 +1140,7 @@ if (!args("map")) { if (!file_exists($map)) { @exec("convert -".$method." -colorspace gray ".args("map")." ".$map); } - } elseif (substr(args("map"), -3, 3) == "jpg") { + } elseif (substr(args("map"), -3, 3) == "jpg" | substr(args("map"), -3, 3) == "tif") { $map = args("map"); } @@ -1144,7 +1196,7 @@ foreach ($files as $file) { } list ($width, $height) = getimagesize($use); list ($twidth, $theight) = getimagesize($file); - if ($width != $twidth | $height != $theight) { + if ($width != $twidth || $height != $theight) { $tmap = $use."'[".$twidth."x".$theight."!]'"; $msg .= " (resize map) "; } else { @@ -1201,7 +1253,7 @@ foreach ($files as $file) { list ($imwidth, $imheight) = getimagesize($file); $x = floor($imheight/$height); } - if ($ext == "jpg" | $ext == "JPG") { + if ($ext == "jpg" || $ext == "JPG") { exec("exiftool -overwrite_original -Xresolution=".$x." -Yresolution=".$x." -jfif:Xresolution=".$x." -jfif:Yresolution=".$x." ".$file); echo "set DPI to ".$x; } elseif ($ext == "DNG") { @@ -1231,11 +1283,11 @@ $files = glob(args("dir")."*.*"); foreach ($files as $file) { echo "Processing ".$file.": "; $ext = pathinfo($file, PATHINFO_EXTENSION); - if ($ext == "jpg" | $ext == "JPG" | $ext == "DNG") { + if ($ext == "jpg" || $ext == "JPG" || $ext == "DNG") { $parts = chop(shell_exec("exiftool -s -s -s -XMP-crs:CropTop -XMP-crs:CropRight -XMP-crs:CropLeft -XMP-crs:CropBottom -XMP-crs:CropAngle ".$file." 2>&1")); if (strlen($parts) > 1) { @list($top, $right, $left, $bottom, $angle) = explode("\n", $parts); } - if (isset($top) | isset($right) | isset($left) | isset($bottom) | isset($angle)) { - shell_exec("exiftool -overwrite_original -XMP-crs:CropTop= -XMP-crs:CropRight= -XMP-crs:CropLeft= -XMP-crs:CropBottom= -XMP-crs:CropBottom= ".$file." 2>&1"); + if (isset($top) || isset($right) || isset($left) || isset($bottom) || isset($angle)) { + shell_exec("exiftool -overwrite_original -XMP-crs:CropTop= -XMP-crs:CropRight= -XMP-crs:CropLeft= -XMP-crs:CropBottom= -XMP-crs:CropAngle= ".$file." 2>&1"); echo "removed"; } else { echo "no crop found"; @@ -1294,7 +1346,7 @@ if (!args("nomap")) { $parts = chop(shell_exec("exiftool -s -s -s -XMP-crs:CropTop -XMP-crs:CropRight -XMP-crs:CropLeft -XMP-crs:CropBottom -ImageWidth -ImageHeight ".$file." 2>&1")); @list($top, $right, $left, $bottom, $check_width, $check_height) = explode("\n", $parts); - if (!$top | !$right | !$left | !$bottom) { msg($file." missing crop information!",1); } + if (!$top || !$right || !$left || !$bottom) { msg($file." missing crop information!",1); } $jpeg_width = $check_height; $jpeg_height = $check_width; @@ -1393,7 +1445,7 @@ foreach ($files as $file) { $msg = $file." = ".$angle; - if ($angle > $deskew_max_angle | $angle < ($deskew_max_angle*-1)) { + if ($angle > $deskew_max_angle || $angle < ($deskew_max_angle*-1)) { $msg .= " (too big)"; } else { $angles[$file] = $angle;