Fit resize then crop, Help needed please

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

sanadw

Fit resize then crop, Help needed please

Post by sanadw »

Hi every body:
I got this script from a friend and need to add anchors for it, is it possible?

the script will fit then re-size any image to 390X310 pix by cropping the extras, even if the pic smaller still will re-size fit then crop if needed.
now am trying to modify it by some way putting an anchor to it, so the cropping will be from the one side only (not from top and bottom) or (left and right) at the same time!
wana end up with 4 scripts to anchor the top, bottom, left and right... so u can chose the area to be cropped by running different scripts!!
any help or hints pls???

//Set to the desired destination width in pixels.
var dstWidth = 390;
//Set to the desired destination height in pixels.
var dstHeight = 310;
//Set to the desired resample method.
//Valide options as of PS CS2 are:
//ResampleMethod.BICUBIC
//ResampleMethod.BICUBICSHARPER
//ResampleMethod.BICUBICSMOOTHER
//ResampleMethod.BILINEAR
//ResampleMethod.NEARESTNEIGHBOR
//ResampleMethod.NONE

var useResampleMethod = ResampleMethod.BICUBIC;
//Set to false if you don't want smaller images to be upsized.
var upsize = true;

// Remember current unit settings and then set units to
// the value expected by this script
var originalUnit = preferences.rulerUnits
preferences.rulerUnits = Units.PIXELS

doResizeCrop();

// Restore original ruler unit setting
app.preferences.rulerUnits = originalUnit

function doResizeCrop() {
if(app.documents.length == 0)
return;

var srcAspectRatio;
var dstAspectRatio;


srcAspectRatio = app.activeDocument.width / app.activeDocument.height;
dstAspectRatio = dstWidth / dstHeight;

//We must round the values before we can compare them.
srcAspectRatio = Math.round(srcAspectRatio * 1000) / 1000;
dstAspectRatio = Math.round(dstAspectRatio * 1000) / 1000;

crop = true;
//Resize to destination width, crop top and bottom.
if(dstAspectRatio > srcAspectRatio) {
newWidth = dstWidth;
newHeight = dstWidth / srcAspectRatio;
if(!upsize) {
if(app.activeDocument.width.value < dstWidth) {
dstWidth = app.activeDocument.width.value;
if(app.activeDocument.height.value > dstHeight)
newHeight = app.activeDocument.height.value;
else
dstHeight = newHeight;
}
}
crop = (newHeight - dstHeight) / 2
x1 = 0;
y1 = crop;
x2 = dstWidth;
y2 = newHeight - crop;
//Resize to destination height, crop left and right.
} else if(dstAspectRatio < srcAspectRatio) {
newWidth = dstHeight * srcAspectRatio;
newHeight = dstHeight;
if(!upsize) {
if(app.activeDocument.height.value < dstHeight) {
dstHeight = app.activeDocument.height.value;
if(app.activeDocument.width.value > dstWidth)
newWidth = app.activeDocument.width.value;
else
dstWidth = newWidth;
}
}
crop = (newWidth - dstWidth) / 2;
x1 = crop;
y1 = 0;
x2 = newWidth - crop;
y2 = dstHeight;
//Resize to destination width and height, don't crop
} else {
newWidth = dstWidth;
newHeight = dstHeight;
crop = false;
}
if(upsize || (newWidth < app.activeDocument.width || newHeight < app.activeDocument.height))
app.activeDocument.resizeImage(newWidth, newHeight, app.activeDocument.resolution, useResampleMethod);
if(crop)
app.activeDocument.crop(new Array(x1,y1,x2,y2));
}