Photoshop JavaScript 将图像和画布调整为特定(非方形)尺寸

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17580923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:51:53  来源:igfitidea点击:

Photoshop JavaScript to resize image and canvas to specific (not square) sizes

javascriptcanvasresizephotoshop

提问by tibelchior

How can I use PhotoShop's JavaScript functionality to convert a set of hundreds of images to a specific, non-square size (e.g. 320x350px)?

如何使用 PhotoShop 的 JavaScript 功能将一组数百张图像转换为特定的非方形尺寸(例如 320x350px)?

回答by tibelchior

Searching the web I've found many potential solutions but 100% of them converted to square sizes. So, I gathered a few and solved the problem myself.

在网上搜索我发现了许多潜在的解决方案,但其中 100% 转换为正方形大小。所以,我收集了一些并自己解决了这个问题。

Save the code below as a .jsxfile in your Photoshop \Presets\Scriptsfolder. Then, make an ACTION if want to use it with many files.

将以下代码另存为.jsxPhotoshop\Presets\Scripts文件夹中的文件。然后,如果要与许多文件一起使用,请执行 ACTION。

// get a reference to the current (active) document and store it in a variable named "doc"
doc = app.activeDocument;  

// change the color mode to RGB.  Important for resizing GIFs with indexed colors, to get better results
doc.changeMode(ChangeMode.RGB);  

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

// 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;

// Convert the canvas size as informed above for the END RESULT
app.activeDocument.resizeCanvas(UnitValue(fWidth,"px"),UnitValue(fHeight,"px"));

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

var newName = 'web-'+doc.name+'.jpg';

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