ps腳本早有耳聞地沮,此前用過(guò)網(wǎng)上批量修改導(dǎo)出圖片名稱(chēng)的腳本,因而對(duì)腳本批量處理圖片一直有一種崇敬酌泰,科技手段就是生產(chǎn)力呀购撼!
這次碰巧產(chǎn)品因?yàn)殚_(kāi)發(fā)周期緊湊的原因跪削,為了快速更換之前不科學(xué)的人工輸出的大量圖標(biāo),又一次需要大量輸出圖標(biāo)份招,自己就從0開(kāi)始用了半天時(shí)間寫(xiě)了一個(gè)ps腳本
此前為了給開(kāi)發(fā)節(jié)約時(shí)間成本切揭,使用了不科學(xué)的實(shí)現(xiàn)方式,對(duì)每一個(gè)等級(jí)的圖標(biāo)都采用了人工輸出一張圖片的方式锁摔,而不是客戶(hù)端動(dòng)態(tài)生成所要的圖片(一種難以啟齒的方式廓旬,這里就不說(shuō)產(chǎn)品和工程上的問(wèn)題了,說(shuō)好的不務(wù)正業(yè))
以上需要快速生成的圖標(biāo)谐腰,需要?jiǎng)討B(tài)修改的有背景孕豹,文字部分,數(shù)字等級(jí)部分
實(shí)現(xiàn)方式:將要更換的背景制作好十气,放到ps中的不同涂層励背,如下圖:
將要填入的文字標(biāo)簽用數(shù)組的方式存下來(lái),偽碼農(nóng)廢話不多說(shuō)上代碼:
//這里我使用了二維數(shù)組直接生成兩組標(biāo)簽
var arrayItem = [["小白","平民","蘿莉","玉女","歐尼","御姐","富美","森女","女神","女王"],["小白","平民","正太","金童","偶吧","大叔","富帥","暖男","男神","男帝"]]
//三層循環(huán)砸西,第一次便利標(biāo)簽組叶眉,第二層遍歷每組中的文字標(biāo)簽址儒,第三層遍歷等級(jí)
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]和數(shù)組類(lèi)似,表示從上到下第一個(gè)圖層
//設(shè)置可見(jiàn).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){
//創(chuàng)建文字圖層
var textLayer = app.activeDocument.artLayers.add()
var color1 = new SolidColor() //創(chuàng)建文字顏色
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")] //調(diào)整位置
textLayer.textItem.size = UnitValue("16 pt") //字體大小
textLayer.textItem.contents = arrayItem[i][j] //文字內(nèi)容
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圖標(biāo)的函數(shù)
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 //設(shè)置為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
}
}
當(dāng)然衅疙,整個(gè)過(guò)程少不了nullice大神的教程~
http://nullice.com/archives/1822#i-3
http://nullice.com/archives/1790#i-4
以及官方開(kāi)發(fā)文檔
http://wwwimages.adobe.com/content/dam/Adobe/en/devnet/photoshop/pdfs/photoshop-cc-javascript-ref-2015.pdf