Code: Select all
"CROP PHOTO to ASPECT RATIO" //|D:\Graphics\Icons\k321-icon-219.ico" // see sub "_ExecuteIV" to modify Irfanview path to suit your installation
load self(file);
" 16x9 (e.g., 1920x1080)|D:\Graphics\Icons\k321-icon-218.ico"; // maintains the highest possible file size and only crops to achieve a 16x9 aspect ratio;
global $x_crop, $y_crop, $xy, $rotate, $orig_width, $orig_height, $orig_ratio, $width, $height;
global $file;
foreach($token, <get selecteditemspathnames |>) {
$file = $token;
$xy = "16x9";
sub "_FileChk";
// when going "to" 16x9 we will always be cropping in the y axis at least for standard photography
$height = ($orig_width * 9) / 16; // echo "Test 1: New height is ". $height ."";
$y_total_crop = $orig_height - $height;
$y_crop = round ($y_total_crop / 2); // msg "Crop amount is ". $y_crop ."";
$y_crop = input("Crop offset [y] vertically down from upper left corner 0,0.","Enter number from 1 to ". $y_total_crop .", e.g., ". $y_crop ." centers crop." , $y_crop, "s", ,300,250);
$width = $orig_width; $x_crop = "0"; sub "_ExecuteIV";
}
" 8x5 (e.g., 1920x1200)|D:\Graphics\Icons\k321-icon-219.ico"; // maintains the highest possible file size and only crops to 8x5 aspect ratio
global $x_crop, $y_crop, $xy, $rotate, $orig_width, $orig_height, $orig_ratio, $width, $height;
global $file;
foreach($token, <get selecteditemspathnames |>) {
$file = $token;
$xy = "8x5";
sub "_FileChk";
if ($orig_ratio > 1.6) { // If ratio is > 1.6 which is an 8x5, we must crop on the x axis (e.g., Panasonic camera 4320 x 2430 (16x9) to a 8x5)
$width = round (($orig_height * 8) / 5);
$x_total_crop = $orig_width - $width;
$x_crop = round ($x_total_crop / 2); // msg "Crop amount is ". $x_crop ."";
$x_crop = input("Crop offset [x] horizontally across from upper left corner 0,0.","Enter number from 1 to ". $x_total_crop .", e.g., ". $x_crop ." centers crop." , $x_crop, "s", ,300,250);
$height = $orig_height; $y_crop = "0"; sub "_ExecuteIV"; //$height is actually the same in this case since we are cropping [x]
}
else {
$height = ($orig_width * 5) / 8; //echo "Test 1: New height is ". $height ."";
$y_total_crop = $orig_height - $height;
$y_crop = round ($y_total_crop / 2); //msg "Crop amount is ". $y_crop ."";
$y_crop = input("Crop offset [y] vertically down from upper left corner 0,0.","Enter number from 1 to ". $y_total_crop .", e.g., ". $y_crop ." centers crop." , $y_crop, "s", ,300,250);
$width = $orig_width; $x_crop = "0"; sub "_ExecuteIV";
}
}
" 4x3 (e.g., 1600x1200)|:viewthumbs"; // maintains the highest possible file size and only crops to 4x3 aspect ratio
global $x_crop, $y_crop, $xy, $orig_width, $orig_height, $orig_ratio, $width, $height;
global $file;
foreach($token, <get selecteditemspathnames |>) {
$file = $token;
$xy = "4x3";
sub "_FileChk";
if ($orig_ratio > 1.33) { // If ratio is > 1.33 which is an 4x3, we must crop on the x axis (e.g., Panasonic camera 4320 x 2430 (16x9) to a 8x5)
$width = round (($orig_height * 4) / 3);
$x_total_crop = $orig_width - $width;
$x_crop = round ($x_total_crop / 2); //msg "Crop amount is ". $x_crop ."";
$x_crop = input("Crop offset [x] horizontally across from upper left corner 0,0.","Enter number from 1 to ". $x_total_crop .", e.g., ". $x_crop ." centers crop." , $x_crop, "s", ,300,250);
$height = $orig_height; $y_crop = "0"; sub "_ExecuteIV"; //$height is actually the same in this case since we are cropping [x]
}
else {
$height = ($orig_width * 3) / 4; //echo "Test 1: New height is ". $height ."";
$y_total_crop = $orig_height - $height;
$y_crop = round ($y_total_crop / 2); //msg "Crop amount is ". $y_crop ."";
$y_crop = input("Crop offset [y] vertically down from upper left corner 0,0.","Enter number from 1 to ". $y_total_crop .", e.g., ". $y_crop ." centers crop." , $y_crop, "s", ,300,250);
$width = $orig_width; $x_crop = "0"; sub "_ExecuteIV";
}
}
"_FileChk";
global $file, $token, $p, $xy, $rotate, $orig_width, $orig_height, $orig_ratio;
End GetInfo("CountSelected") < 1, "No file(s) have been selected.";
$p = property("#11", $file); // determine if the file is a picture
if ($p != "Picture") { // checks properties to make sure it's an image/picture
msg "This is not a picture image so you can't crop it.";
end 1==1,, 1;
}
$d = property(dimensions, $file);
$rotate = property("#245", $file);
$orig_width = property("ImageX", $file); // another method, $orig_width = gettoken($d, 1, " x ");
$orig_height = property("ImageY", $file);
$orig_ratio = round ($orig_width / $orig_height, 2); // msg "Original ratio is ". $orig_ratio ."";
if ($orig_width * $orig_height < 50000) { // you can decrease this # to crop smaller images
msg "This image is a bit small to crop ". $d . " px!";
end 1==1,, 1;
}
if ($rotate != "Rotate 270 degrees") { // If picture is rotated, it's in "portrait" and will have a ratio < 1 and will not be a concern for these checks
if ($orig_ratio == 1.33 AND ($xy == 4x3)) { // checks to see if the picture is already in 4x3 aspect ratio
msg "This picture is already in a 4x3 aspect ratio, ". $d . " px!";
end 1==1,, 1;
}
if ($orig_ratio == 1.6 AND ($xy == 8x5)) { // checks to see if the picture is already in 8x5 aspect ratio
msg "This picture is already in a 8x5 aspect ratio, ". $d . " px!";
end 1==1,, 1;
}
if ($orig_ratio == 1.78 AND ($xy == 16x9)) { // checks to see if the picture is already in 16x9 aspect ratio
msg "This picture is already in a 16x9 aspect ratio, ". $d . " px!";
end 1==1,, 0;
}
}
else {
$orig_width = property("ImageY", $file); // Reverses X and Y axis since these photos are rotated 270 degrees- need to reverse for std crop calculations
$orig_height = property("ImageX", $file);
}
"_ExecuteIV";
global $x_crop, $y_crop, $xy, $rotate, $width, $height, $file;
$path=getpathcomponent($file, "path");
$base=getpathcomponent($file, "base");
$ext=getpathcomponent($file, "ext");
$new_file = "$path\$base" . "_$xy.$ext"; // $new_file = gettoken ($file, 1,".")" . "_$xy.$ext";
$g_IrfanPath="d:\graphics\irfanView\i_view32.exe";
run """$g_IrfanPath"" $file /aspectratio /jpgq=100 /crop=($x_crop, $y_crop, $width, $height) /convert=$new_file", , 2,0;
wait(500);
Thanks,
Ken
XYplorer Beta Club