我有一个脚本来调整图像大小。反正有输出5张图像吗?

时间:2019-05-01 04:36:36

标签: javascript adobe photoshop

doc = app.activeDocument;  

// these are our values for the END RESULT width and height (in pixels) of our image
var fWidth = 1313;
var fHeight = 1750;

// do the resizing.  if height > width (portrait-mode) resize based on height.  otherwise, resize based on width
if (doc.height > doc.width) {
    doc.resizeImage(null,UnitValue(fHeight,"px"),null,ResampleMethod.BICUBIC);
}
else {
    doc.resizeImage(UnitValue(fWidth,"px"),null,null,ResampleMethod.BICUBIC);
}

// Makes the default background white
var white = new SolidColor(); 
white.rgb.hexValue = "FFFFFF";
app.backgroundColor = white;

// 2012, use it at your own risk;

#target photoshop

if (app.documents.length > 0) {
var myDocument = app.activeDocument;
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PERCENT;
myDocument.resizeCanvas(myDocument.width + 40, myDocument.height + 40, AnchorPosition.MIDDLECENTER)
app.preferences.rulerUnits = originalRulerUnits;
};

// our web export options
var options = new ExportOptionsSaveForWeb();
options.quality = 70;
options.format = SaveDocumentType.JPEG;
options.optimized = true;

var newName = 'test'+doc.name+'.jpg';

doc.exportDocument(File(doc.path+'/'+newName),ExportType.SAVEFORWEB,options);

我希望能够使用同一脚本生成5种具有5种不同尺寸的图像。它就像重复我的代码多次一样简单,还是我需要在两者之间重设一些变量?

当我尝试复制代码并更改输出文件名和大小时,可以执行此操作,但是我的画布大小不会重置并且不会根据当前图像大小进行更改。它只是不断变大。无论如何,是否可以根据当前图像大小调整画布大小?

var sizes = [
{
    width: 1531,
    height: 1948
},
{
    width: 1303,
    height: 1954
},  
{
    width: 1066,
    height: 1909
}
 ];
doc = app.activeDocument;
// looping through all the sizes
for (var i = 0; i < sizes.length; i++)
{
    var cloneDoc = doc.duplicate(); // duplicates current document
    resizeAndSave(sizes[i].width, sizes[i].height); // passes width and height of sizes to function with your code
    cloneDoc.close(SaveOptions.DONOTSAVECHANGES); // closes the clone
    activeDocument = doc; // making sure that foremost document is the original doc
}

function resizeAndSave(fWidth, fHeight)
{
    //your code


// get a reference to the current (active) document and store it in a variable named "doc"




// these are our values for the END RESULT width and height (in pixels) of our image
//var fWidth = 1313;
//var fHeight = 1750;

// do the resizing.  if height > width (portrait-mode) resize based on height.  otherwise, resize based on width
activeDocument = doc;
if (doc.height > doc.width) {
    doc.resizeImage(null,UnitValue(fHeight,"px"),null,ResampleMethod.BICUBIC);
}
else {
    doc.resizeImage(UnitValue(fWidth,"px"),null,null,ResampleMethod.BICUBIC);
}

// Makes the default background white
var white = new SolidColor(); 
white.rgb.hexValue = "FFFFFF";
app.backgroundColor = white;

// 2012, use it at your own risk;

#target photoshop

if (app.documents.length > 0) {

var myDocument = app.activeDocument;

var originalRulerUnits = app.preferences.rulerUnits;

app.preferences.rulerUnits = Units.PERCENT;

myDocument.resizeCanvas(myDocument.width + 40, myDocument.height + 40, AnchorPosition.MIDDLECENTER)

app.preferences.rulerUnits = originalRulerUnits;

};

// our web export options

var options = new ExportOptionsSaveForWeb();
options.quality = 70;
options.format = SaveDocumentType.JPEG;
options.optimized = true;

var newName = 'test'+doc.name+'.jpg';

doc.exportDocument(File(doc.path+'/'+newName),ExportType.SAVEFORWEB,options);

};

2 个答案:

答案 0 :(得分:0)

您需要在每个循环的开头重置或克隆原始文档。我更喜欢克隆,所以我会做这样的事情:

// creating array of sizes
var sizes = [
{
    width: 313,
    height: 750
},
{
    width: 413,
    height: 150
}, ];

// looping through all the sizes
for (var i = 0; i < sizes.length; i++)
{
    var cloneDoc = doc.duplicate(); // duplicates current document
    resizeAndSave(sizes[i].width, sizes[i].height); // passes width and height of sizes to function with your code
    cloneDoc.close(SaveOptions.DONOTSAVECHANGES); // closes the clone
    activeDocument = doc; // making sure that foremost document is the original doc
}

function resizeAndSave(fWidth, fHeight)
{
    //your code
};

答案 1 :(得分:0)

doc = app.activeDocument;
var savedState = app.activeDocument.activeHistoryState



// get a reference to the current (active) document and store it in a variable named "doc"




// these are our values for the END RESULT width and height (in pixels) of our image
var fWidth = 1155;
var fHeight = 1471;

// do the resizing.  if height > width (portrait-mode) resize based on height.  otherwise, resize based on width
activeDocument = doc;
if (doc.height > doc.width) {
    doc.resizeImage(null,UnitValue(fHeight,"px"),null,ResampleMethod.BICUBIC);
}
else {
    doc.resizeImage(UnitValue(fWidth,"px"),null,null,ResampleMethod.BICUBIC);
}

// Makes the default background white
var white = new SolidColor(); 
white.rgb.hexValue = "FFFFFF";
app.backgroundColor = white;

// 2012, use it at your own risk;

#target photoshop

if (app.documents.length > 0) {
activeDocument = doc;
var cwidth = 2000;
var cheight = 2000;

var originalRulerUnits = app.preferences.rulerUnits;

app.preferences.rulerUnits = Units.PIXELS;

doc.resizeCanvas(cwidth, cheight, AnchorPosition.MIDDLECENTER)

app.preferences.rulerUnits = originalRulerUnits;

};

// our web export options

var options = new ExportOptionsSaveForWeb();
options.quality = 70;
options.format = SaveDocumentType.JPEG;
options.optimized = true

var newName = 'MIR'+doc.name +'-22_28'+'.jpg';

doc.exportDocument(File(doc.path+'/'+newName),ExportType.SAVEFORWEB,options);

app.activeDocument.activeHistoryState = savedState

// these are our values for the END RESULT width and height (in pixels) of our image
var hWidth = 1141;
var hHeight = 1711;

// do the resizing.  if height > width (portrait-mode) resize based on height.  otherwise, resize based on width
activeDocument = doc;
if (doc.height > doc.width) {
    doc.resizeImage(null,UnitValue(hHeight,"px"),null,ResampleMethod.BICUBIC);
}
else {
    doc.resizeImage(UnitValue(hWidth,"px"),null,null,ResampleMethod.BICUBIC);
}

// Makes the default background white
var white = new SolidColor(); 
white.rgb.hexValue = "FFFFFF";
app.backgroundColor = white;

// 2012, use it at your own risk;

#target photoshop

if (app.documents.length > 0) {
activeDocument = doc;
var cwidth = 2000;
var cheight = 2000;

var originalRulerUnits = app.preferences.rulerUnits;

app.preferences.rulerUnits = Units.PIXELS;

doc.resizeCanvas(cwidth, cheight, AnchorPosition.MIDDLECENTER)

app.preferences.rulerUnits = originalRulerUnits;

};




// our web export options

var options = new ExportOptionsSaveForWeb();
options.quality = 70;
options.format = SaveDocumentType.JPEG;
options.optimized = true;

var newName = 'MIR'+doc.name+'-24_36'+'.jpg';

doc.exportDocument(File(doc.path+'/'+newName),ExportType.SAVEFORWEB,options);

app.activeDocument.activeHistoryState = savedState

// these are our values for the END RESULT width and height (in pixels) of our image
var fWidth = 1058;
var fHeight = 1897;

// do the resizing.  if height > width (portrait-mode) resize based on height.  otherwise, resize based on width
activeDocument = doc;
if (doc.height > doc.width) {
    doc.resizeImage(null,UnitValue(fHeight,"px"),null,ResampleMethod.BICUBIC);
}
else {
    doc.resizeImage(UnitValue(fWidth,"px"),null,null,ResampleMethod.BICUBIC);
}

// Makes the default background white
var white = new SolidColor(); 
white.rgb.hexValue = "FFFFFF";
app.backgroundColor = white;

// 2012, use it at your own risk;

#target photoshop

if (app.documents.length > 0) {
activeDocument = doc;
var cwidth = 2000;
var cheight = 2000;

var originalRulerUnits = app.preferences.rulerUnits;

app.preferences.rulerUnits = Units.PIXELS;

doc.resizeCanvas(cwidth, cheight, AnchorPosition.MIDDLECENTER)

app.preferences.rulerUnits = originalRulerUnits;

};

// our web export options

var options = new ExportOptionsSaveForWeb();
options.quality = 70;
options.format = SaveDocumentType.JPEG;
options.optimized = true;

var newName = 'MIR'+doc.name+'-24_43'+'.jpg';

doc.exportDocument(File(doc.path+'/'+newName),ExportType.SAVEFORWEB,options);

app.activeDocument.activeHistoryState = savedState

// these are our values for the END RESULT width and height (in pixels) of our image
var fWidth = 1360;
var fHeight = 1813;

// do the resizing.  if height > width (portrait-mode) resize based on height.  otherwise, resize based on width
activeDocument = doc;
if (doc.height > doc.width) {
    doc.resizeImage(null,UnitValue(fHeight,"px"),null,ResampleMethod.BICUBIC);
}
else {
    doc.resizeImage(UnitValue(fWidth,"px"),null,null,ResampleMethod.BICUBIC);
}

// Makes the default background white
var white = new SolidColor(); 
white.rgb.hexValue = "FFFFFF";
app.backgroundColor = white;

// 2012, use it at your own risk;

#target photoshop

if (app.documents.length > 0) {
activeDocument = doc;
var cwidth = 2000;
var cheight = 2000;

var originalRulerUnits = app.preferences.rulerUnits;

app.preferences.rulerUnits = Units.PIXELS;

doc.resizeCanvas(cwidth, cheight, AnchorPosition.MIDDLECENTER)

app.preferences.rulerUnits = originalRulerUnits;

};

// our web export options

var options = new ExportOptionsSaveForWeb();
options.quality = 70;
options.format = SaveDocumentType.JPEG;
options.optimized = true;

var newName = 'MIR'+doc.name+'-30_40'+'.jpg';

doc.exportDocument(File(doc.path+'/'+newName),ExportType.SAVEFORWEB,options);

app.activeDocument.activeHistoryState = savedState






// get a reference to the current (active) document and store it in a variable named "doc"