根据原始图像高度使用 Photoshop 和 JavaScript 调整条件图像大小



我正在努力为Photoshop动作创建一个js代码。我有一个不同大小的图像的文件夹。这些也是不同的高度和宽度。我的脚本需要:

对于每个图像:读取高度和宽度。无论大小(高度或宽度)最大,在 Photoshop 中创建白色背景正方形并将图像放置在正方形中。另存为 300 dpi JPG,名为 filename_01。对下一张图像执行相同的操作。

示例 - 如果第一张图像名为 angel.jpg并且高度为 600 像素,宽度为 400 像素,则 Photoshop 将:创建一个白色背景.jpg空白图像 SQUARE,尺寸为 600x600(因为高度是高度/宽度尺寸中最大的一个)。将图像居中放置在正方形中。将其另存为 300 dpi .jpg命名为 angel_01.jpg。

如果下一个图像为 520x390,则该框将为 520 像素。我找到了这个JS代码,但它不符合我的要求。

// 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 = 500;
var fHeight = 500;
// 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);
}
// call autoContrast and applySharpen on the active layer.
// if we just opened a gif, jpeg, or png, there's only one layer, so it must be the active one
doc.activeLayer.autoContrast();
doc.activeLayer.applySharpen();
// 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);

好的。 对你来说特别好,米尔扎·阿卜杜勒·拉赫曼。(请将 50,000 美元转入我在苏黎世的银行账户):-)

var defaultRulerUnits = preferences.rulerUnits; 
preferences.rulerUnits = Units.PIXELS;
if (documents.length >= 1){
var hres = 0;
var vres = 0;
var CubSize = 0;
var OldName = activeDocument.name.substring(0, activeDocument.name.indexOf('.'));
var CurrentFolder = activeDocument.path;
hres = activeDocument.width;
vres =  activeDocument.height;
activeDocument.selection.selectAll();
if (activeDocument.layers.length >1) {
	activeDocument.selection.copy(true);
}
else{
	if (activeDocument.layers.length =1) {
	activeDocument.selection.copy(false);
	}
}
activeDocument.close(SaveOptions.DONOTSAVECHANGES);	// Close Original Image
if (vres > hres)	
	CubSize=vres;    
else	
	CubSize=hres;                 
      
var newDoc = documents.add(CubSize,CubSize,72, OldName + '-01', NewDocumentMode.RGB, DocumentFill.WHITE);
newDoc.paste();
newDoc.mergeVisibleLayers(); 
jpgFile = new File(CurrentFolder + "/" + OldName+"_01" + ".jpg" );
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = 7;
newDoc.saveAs(jpgFile, jpgSaveOptions, true,   Extension.LOWERCASE);
}

最新更新