上一篇是针对web图片,所以保存文件的时候,文件名会强制把空格变成减号。
这次改成普通的另存,并做了部分改进:
//for photoshop 2018,只能在ps2018运行,注意Tookit编辑器里选择脚本版本。
//前几行是配置
var image_folder = new Folder("E:/gitroot/g4/assets/rolePicTmpBk/Textures");
var dest_folder = new Folder("E:/gitroot/g4/g4u3d/Assets/Mega Pack/Textures");
var max_with = 256
var min_width = 0 //如果设置为0,表示不做限制
var max_height = 0;
var min_height = 0;
var image_path = File.decode(image_folder.fullName);
var dest_path = File.decode(dest_folder.fullName);
function processFile(f) {
if (f instanceof Folder) {
var files = f.getFiles();
for (var i = 0; i < files.length; i++) {
processFile(files[i])
}
} else if (f instanceof File) {
if (isPic(f.name)) {
var dest = File.decode(f.path);
dest = dest.replace(image_path, dest_path)
new Folder(dest).create()
dest = dest + "/" + File.decode(f.name)
$.writeln(dest)
//if (!new File(dest).exists) {
resizePic(f, dest, max_with)
//}
}
}
}
//保持比例缩放
function resizePic(file, destpath, maxw) {
try {
//$.writeln(file);
openlayer = open(file);
}
catch (err) {
$.writeln(err);
return;
}
// get a reference to the current (active) document and store it in a variable named "doc"
doc = app.activeDocument;
//doc.trim()
// $.writeln (doc.width.as ("px"))
var px_w = doc.width.as("px")
var px_h = doc.height.as("px");
var scale = limitWidthHeight(px_w,px_h);
w = px_w * scale;
h = px_h * scale;
//h = (w / doc.width.as("px")) * doc.height.as("px")
$.writeln(w)
$.writeln(h)
if(scale>=1)
{
w = px_w;
h = px_h;
}
// change the color mode to RGB. Important for resizing GIFs with indexed colors, to get better results
doc.changeMode(ChangeMode.RGB);
// 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(h, "px"), null, ResampleMethod.BICUBIC);
}
else {
doc.resizeImage(UnitValue(w, "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(w, "px"), UnitValue(h, "px"));
// our web export options
// var options = new ExportOptionsSaveForWeb();
// options.quality = 60 //PS默认值
// options.format = SaveDocumentType.PNG;
// options.optimized = true;
// var file = new File(destpath);
// doc.exportDocument(file, ExportType.SAVEFORWEB, options);
var options = new PNGSaveOptions();
//options.quality = 19 //基本清楚,最低标准,给移动网站使用
//options.format = SaveDocumentType.JPEG;
options.quality = 60 //PS默认值
options.format = SaveDocumentType.PNG;
var file = new File(destpath);
doc.saveAs(file,options, true, Extension.LOWERCASE);
openlayer.close(SaveOptions.DONOTSAVECHANGES)
}
function getFileExt(filePath) {
//获取最后一个.的位置
var index = filePath.lastIndexOf(".");
//获取后缀
var ext = filePath.substr(index + 1);
return ext
}
function isPic(fileName) {
var ext = getFileExt(fileName)
if ("jpg".toUpperCase() == ext.toUpperCase() ||
"jpeg".toUpperCase() == ext.toUpperCase() ||
"psd".toUpperCase() == ext.toUpperCase() ||
"png".toUpperCase() == ext.toUpperCase()) {
return true
}
return false
}
//返回限制以后的长、宽值
function limitInRange(px_w,min,max) {
var ret = px_w;
if(max>0) {
if(ret>max) {
ret = max
}
}
if(min>0) {
if(ret<min) {
ret = min;
}
}
return ret;
}
//返回缩放比例
function limitWidthHeight(px_w,px_h) {
var w = limitInRange(px_w,min_width,max_with);
var h = limitInRange(px_h,min_height,max_height);
var scale_w = w/px_w;
var scale_h = h/px_h;
if(scale_w==1) {
return scale_h;
}
if(scale_h==1) {
return scale_w;
}
return Math.min(scale_w,scale_h);
}
//这里是正式调用处理函数
processFile(image_folder)
$.writeln("处理结束");
vscode 运行配置,运行之前需要在下方选择合适的ps版本(2018)
{
"version": "1.0.0",
"configurations": [
{
"type": "extendscript-debug",
"request": "launch",
"name": "Testing this plugin",
"program": "${workspaceFolder}/resize.jsx"
}
]
}