ps脚本早有耳闻,此前用过网上批量修改导出图片名称的脚本,因而对脚本批量处理图片一直有一种崇敬,科技手段就是生产力呀!
这次碰巧产品因为开发周期紧凑的原因,为了快速更换之前不科学的人工输出的大量图标,又一次需要大量输出图标,自己就从0开始用了半天时间写了一个ps脚本
此前为了给开发节约时间成本,使用了不科学的实现方式,对每一个等级的图标都采用了人工输出一张图片的方式,而不是客户端动态生成所要的图片(一种难以启齿的方式,这里就不说产品和工程上的问题了,说好的不务正业)
以上需要快速生成的图标,需要动态修改的有背景,文字部分,数字等级部分
实现方式:将要更换的背景制作好,放到ps中的不同涂层,如下图:
将要填入的文字标签用数组的方式存下来,伪码农废话不多说上代码:
//这里我使用了二维数组直接生成两组标签
var arrayItem = [["小白","平民","萝莉","玉女","欧尼","御姐","富美","森女","女神","女王"],["小白","平民","正太","金童","偶吧","大叔","富帅","暖男","男神","男帝"]]
//三层循环,第一次便利标签组,第二层遍历每组中的文字标签,第三层遍历等级
for(var i=10;i<25;i++){
for(var j=0;j<10;j++){
for(var l=j*5+1;l<=(j+1)*5;l++){
if(l<11){
//找到ps里的图层[0]和数组类似,表示从上到下第一个图层
//设置可见.visible
app.activeDocument.artLayers[0].visible = true
app.activeDocument.artLayers[1].visible = false
app.activeDocument.artLayers[4].visible = false
}
if(l>10&&l<21){
app.activeDocument.artLayers[0].visible = false
app.activeDocument.artLayers[1].visible = true
}
if(l>20&&l<31){
app.activeDocument.artLayers[2].visible = true
app.activeDocument.artLayers[1].visible = false
}
if(l>30&&l<41){
app.activeDocument.artLayers[3].visible = true
app.activeDocument.artLayers[2].visible = false
}
if(l>40&&l<51){
app.activeDocument.artLayers[4].visible = true
app.activeDocument.artLayers[3].visible = false
}
createItems (l,i,j)
}
}
}
function createItems(l,i,j){
//创建文字图层
var textLayer = app.activeDocument.artLayers.add()
var color1 = new SolidColor() //创建文字颜色
color1.rgb.hexValue = "FFFFFF"
textLayer.kind = LayerKind.TEXT
app.activeDocument.activeLayer.textItem.color = color1
app.activeDocument.activeLayer.textItem.font= "FZLTZHUNHJW--GB1-0" //修改文字字体
textLayer.textItem.position= [UnitValue("27px"), UnitValue("21px")] //调整位置
textLayer.textItem.size = UnitValue("16 pt") //字体大小
textLayer.textItem.contents = arrayItem[i][j] //文字内容
var levelLayer = app.activeDocument.artLayers.add()
levelLayer.kind = LayerKind.TEXT
var color2 = new SolidColor()
var colortype = color2Set(l)
$.write (l) //打log
color2.rgb.hexValue = colortype
app.activeDocument.activeLayer.textItem.color = color2
app.activeDocument.activeLayer.textItem.font= "FZLTZHUNHJW--GB1-0"
if(l<10){
levelLayer.textItem.position= [UnitValue("73px"), UnitValue("22px")]
}
else{
levelLayer.textItem.position= [UnitValue("67px"), UnitValue("22px")]
}
levelLayer.textItem.size = UnitValue("18 pt")
levelLayer.textItem.contents = String(l)
exportPng (i+1,l)
textLayer.remove()
levelLayer.remove()
}
//输出png图标的函数
function exportPng(folderName,exportName){
var document = app.activeDocument
var exportPath = "/Users/huangjinxue/Desktop/tabs/"+String(folderName)+"/"
var fileName = String(folderName)+"_"+"L"+String(exportName)+".png"
var fileOut = new File (exportPath+fileName)
var exportOptionsSaveForWeb = new ExportOptionsSaveForWeb()
exportOptionsSaveForWeb.transparency = true
exportOptionsSaveForWeb.includeProfile = true
exportOptionsSaveForWeb.lossy = 0
exportOptionsSaveForWeb.PNG8 = false //设置为png24
exportOptionsSaveForWeb.colors = 256
exportOptionsSaveForWeb.colorReduction = ColorReductionType.SELECTIVE
exportOptionsSaveForWeb.format = SaveDocumentType.PNG
exportOptionsSaveForWeb.ditherAmount = 0
exportOptionsSaveForWeb.dither = Dither.NOISE
exportOptionsSaveForWeb.palette = Palette.LOCALADAPTIVE
document.exportDocument(fileOut, ExportType.SAVEFORWEB, exportOptionsSaveForWeb)
}
function color2Set(l){
var colortype = "ffffff"
if(l<11){
colortype = "fd8098"
return colortype
}
if(l>10&&l<21){
colortype = "1ba2e6"
return colortype
}
if(l>20&&l<31){
colortype = "ea6948"
return colortype
}
if(l>30&&l<41){
colortype = "fec73b"
return colortype
}
if(l>40&&l<51){
colortype = "983eed"
return colortype
}
else{
colortype = "ffffff"
return colortype
}
}
输出的图片
当然,整个过程少不了nullice大神的教程~
http://nullice.com/archives/1822#i-3
http://nullice.com/archives/1790#i-4
以及官方开发文档
http://wwwimages.adobe.com/content/dam/Adobe/en/devnet/photoshop/pdfs/photoshop-cc-javascript-ref-2015.pdf