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

时间:2016-02-09 09:54:28

标签: javascript photoshop

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

对于每张图片: 读取高度和宽度。对于最大(高度或宽度)的尺寸,在Photoshop中创建白色背景正方形并将图像放在正方形中。保存为300 dpi jpg,名为filename_01。 对下一张图片也一样。

示例 - 如果第一张图片名为angel.jpg,高度为600像素,宽度为400像素,则photoshop会: 创建一个600x600的白色背景.jpg空白图像SQUARE(因为高度是高度/宽度尺寸中最大的)。将图像置于正方形的中心。将其保存为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);

1 个答案:

答案 0 :(得分:0)

行。 Mirza Abdul Rahman特别为您服务。 (请将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);

}