diff --git a/leaf.php b/leaf.php index 41c0898..d3d7dae 100644 --- a/leaf.php +++ b/leaf.php @@ -1,7 +1,7 @@ levels adjustment for resulting image (ex. \"0-253,0-2,0-255\") build combine images into a pdf with img2pdf clean remove scratch files crop define EXIF crop values using template files @@ -336,34 +337,71 @@ foreach ($picks as $file) { echo Welcome("Crop using row/column brightness method"); ///////////////////////////////////////////////////////////////////////// +if (args("levels")) { + $adjust = args("levels"); + } + msg("Checking crops"); $files = glob(args("dir")."*.*"); foreach ($files as $file) { - $parts = chop(shell_exec("exiftool -s -s -s -XMP-crs:CropTop -XMP-crs:CropRight -XMP-crs:CropLeft -XMP-crs:CropBottom ".$file." 2>&1")); - @list($top, $right, $left, $bottom) = explode("\n", $parts); - echo "\nExisting crop values for ".$file.": "; - echo "\tTop: ".bashcolor($top, "light blue"); - echo "\tRight: ".bashcolor($right, "light green"); - echo "\tLeft: ".bashcolor($left, "light cyan"); - echo "\tBottom: ".bashcolor($bottom, "light red"); + $tags = chop(shell_exec("exiftool -XMP-crs:CropTop -XMP-crs:CropRight -XMP-crs:CropLeft -XMP-crs:CropBottom -ImageWidth -ImageHeight ".$file." 2>&1")); + foreach (explode("\n", trim($tags)) as $tag) { + $parts = explode(":", $tag); + $attr[trim($parts[0])] = trim($parts[1]); + } + + $width = $attr['Image Width']; + $height = $attr['Image Height']; + + if (isset($attr['Crop Top']) && isset($attr['Crop Right']) && isset($attr['Crop Left']) && isset($attr['Crop Bottom'])) { + echo "\nExisting crop values for ".$file.": "; + echo "\tTop: ".bashcolor($attr['Crop Top'], "light blue"); + echo "\tRight: ".bashcolor($attr['Crop Right'], "light green"); + echo "\tLeft: ".bashcolor($attr['Crop Left'], "light cyan"); + echo "\tBottom: ".bashcolor($attr['Crop Bottom'], "light red"); + } else { + echo "\nNo existing crop values for ".$file; + } - $detectx = explode("\n",chop(shell_exec("convert \( $file +repage -scale x1! -bordercolor black -border 1 -fuzz 30% -trim \) -format \"%w\n%O\" info:"))); - $detecty = explode("\n",chop(shell_exec("convert \( $file +repage -scale 1x! -bordercolor black -border 1 -fuzz 60% -trim \) -format \"%h\n%O\" info:"))); + if (isset($adjust)) { + $adjfile = "\( ".$file." -level ".$adjust." \)"; + } else { + $adjfile = $file; + } + + $detectx = explode("\n",chop(@shell_exec("convert \( ".$adjfile." +repage -scale x1! -bordercolor black -border 1 -fuzz 30% -trim \) -format \"%w\n%O\" info: 2>&1"))); + $detecty = explode("\n",chop(@shell_exec("convert \( ".$adjfile." +repage -scale 1x! -bordercolor black -border 1 -fuzz 60% -trim \) -format \"%h\n%O\" info: 2>&1"))); $detectxparts = explode("+", $detectx[1]); $detectyparts = explode("+", $detecty[1]); - - list($ntop, $nright, $nleft, $nbottom) = crop_imgk_to_xmp($detectx[0], $detecty[0], $detectxparts[1], $detectxparts[2]); - echo "\nNew crop values for ".$file.": "; - echo "\tTop: ".bashcolor($ntop, "blue"); - echo "\tRight: ".bashcolor($nright, "green"); - echo "\tLeft: ".bashcolor($nleft, "cyan"); - echo "\tBottom: ".bashcolor($nbottom, "red"); + if ($debug) { echo "\n".bashcolor("imgk values: cwidth: ".$detectx[0].", cheight: ".$detecty[0].", xoff: ".$detectxparts[1].", yoff: ".$detectxparts[2].", width: ".$width.", height: ".$height, "white","light purple"); } - $msg = "Setting crop values for ".$file; - $cmd = "exiftool -overwrite_original -XMP-crs:HasCrop=1 -XMP-crs:AlreadyApplied=0 -XMP-crs:CropTop=".$ntop." -XMP-crs:CropRight=".$nright." -XMP-crs:CropLeft=".$nleft." -XMP-crs:CropBottom=".$nbottom." ".$file; + if (isset($detectx[0]) && $detectx[0] > 1) { $cwidth = $detectx[0]; } else { $cwidth = null; } + if (isset($detecty[0]) && $detecty[0] > 1) { $cheight = $detecty[0]; } else { $cheight = null; } + if (isset($detectxparts[1]) && $detectxparts[1] > 1) { $xoff = $detectxparts[1]; } else { $xoff = null; } + if (isset($detectxparts[2])) { $yoff = $detectxparts[2]; } else { $yoff = null; } + + if ($cwidth && $cheight && $xoff && $yoff) { + + list($ntop, $nright, $nleft, $nbottom) = crop_imgk_to_xmp($cwidth, $cheight, $xoff, $yoff, $width, $height); + + echo "\nNew crop values for ".$file.": "; + echo "\tTop: ".bashcolor($ntop, "blue"); + echo "\tRight: ".bashcolor($nright, "green"); + echo "\tLeft: ".bashcolor($nleft, "cyan"); + echo "\tBottom: ".bashcolor($nbottom, "red"); + + $msg = "Setting crop values for ".$file; + $cmd = "exiftool -overwrite_original -XMP-crs:HasCrop=1 -XMP-crs:AlreadyApplied=0 -XMP-crs:CropTop=".$ntop." -XMP-crs:CropRight=".$nright." -XMP-crs:CropLeft=".$nleft." -XMP-crs:CropBottom=".$nbottom." ".$file; + + } else { + + $msg = "Incomplete crop values for ".$file.", skipping..."; + $cmd = "true"; + + } $thread[] = array($msg, $cmd); @@ -1343,7 +1381,7 @@ fin(); echo Welcome("Insert image into numbered sequence"); ///////////////////////////////////////////////////////////////////////// -$files = glob(args("dir")."[0-9][0-9][0-9]-*.jpg"); +$files = glob(args("dir")."[0-9][0-9][0-9]-*.*"); $count = count($files); if (!$count) { @@ -1387,7 +1425,7 @@ foreach ($ops as $parts) { rename($parts[0], $parts[1]); } -fin("Starting file count: ".$count."; Ending file count: ".count(glob(args("dir")."[0-9][0-9][0-9]-*.jpg"))); +fin("Starting file count: ".$count."; Ending file count: ".count(glob(args("dir")."[0-9][0-9][0-9]-*.*"))); //////////////////////////////////////////////////////////////////////////////////////////////// // Note: Review