vbs調(diào)用photoshop批量生成縮略圖的代碼

字號(hào):


    這篇文章主要為大家分享了通過(guò)vbs調(diào)用photoshop批量生成縮略圖,需要的朋友可以參考下
    模仿騰訊新聞頁(yè),給kingcms添加了新聞頁(yè)圖片點(diǎn)播的代碼,代碼要求的圖片點(diǎn)播格式如下:
    0###http://pic02.newdu.com/uploads/202504/02/%ac%ac%e4%ba%8c%e5%b9%85%e3%80%81%e7%ac%ac%e4%b8%89%e5%b9%85%e5%9b%be%e7%89%87%e6%a0%bc%e5%bc%8f%e5%92%8c%e7%ac%ac%e4%b8%80%e5%b9%85%e5%9b%be%e4%b8%80%e6%a0%b7%ef%bc%9b%3cBR%3e     = 原始圖
    directory = createobject(scripting.filesystemobject).getfolder(.).path & \ & directory & \
    call deletefiles(directory)
    strurl = inputbox(請(qǐng)輸入網(wǎng)址:)
    if strurl <> then
    call getimages(strurl)
    end if
    function getimages(strurl)
    set ie = wscript.createobject(internetexplorer.application)
    ie.visible = true
    ie.navigate strurl
    do
    wscript.sleep 500
    loop until ie.readystate=4
    set objimgs = ie.document.getelementbyid(fontzoom).getelementsbytagname(img)
    strtitles = inputbox(請(qǐng)輸入圖片配字:)
    arrtitles = split(strtitles, )
    strcode = 0###
    for i=0 to objimgs.length - 1
    if i>0 then strcode = strcode + ***
    smallpic = replace(mid(objimgs(i).src, instrrev(objimgs(i).src, /)+1), jpg, gif)
    strcode = strcode + objimgs(i).src + @@@/small/ + smallpic + @@@ + arrtitles(i)
    saveremotefile objimgs(i).src
    next
    ie.quit
    inputbox 請(qǐng)復(fù)制結(jié)果:, , strcode
    end function
    sub saveremotefile(remotefileurl)
    localfile = directory & mid(remotefileurl, instrrev(remotefileurl, /)+1)
    set xmlhttp = createobject(microsoft.xmlhttp)
    with xmlhttp
    .open get, remotefileurl, false, ,
    .send
    getremotedata = .responsebody
    end with
    set xmlhttp = nothing
    set ads = createobject(adodb.stream)
    with ads
    .type = 1
    .open
    .write getremotedata
    .savetofile localfile, 2
    .cancel()
    .close()
    end with
    set ads=nothing
    end sub
    function deletefiles(strfolder)
    set objfso = createobject(scripting.filesystemobject)
    set objfolder = objfso.getfolder(strfolder)
    set objfiles = objfolder.files
    for each objfile in objfiles
    objfile.delete
    next
    set objfso = nothing
    end function
    腳本二:調(diào)用photoshop批量生成縮略圖
    directory = 原始圖 '原始圖像的文件夾
    newdirectory = 縮略圖 '保存縮小圖的文件夾
    const psdonotsavechanges = 2
    const psextensiontype_pslowercase = 2
    const psdisplaynodialogs = 3
    const pslocalselective = 7
    const psblackwhite = 2
    const psnodither = 1
    limitheight = 58 '最大高度
    imgresolution = 72 '解析度
    call deletefiles(newdirectory)
    call convert2gif(directory)
    function resizeimg(doc)
    rsheight = doc.height
    scale = 1.0
    if rsheight > limitheight then
    scale = limitheight / (doc.height + 0.0)
    rswidth = doc.width * scale
    rsheight = doc.height * scale
    end if
    doc.resizeimage rswidth, rsheight, imgresolution, 3
    end function
    function convert2gif(directory)
    set app = createobject( photoshop.application )
    app.bringtofront()
    app.preferences.rulerunits = 1 'pspixels
    app.displaydialogs = psdisplaynodialogs
    set gifopt = createobject(photoshop.gifsaveoptions)
    with gifopt
    .palette = pslocalselective
    .colors = 256
    .forced = psblackwhite
    .transparency = false
    .dither = psnodither
    .interlaced = false
    end with
    set fso = createobject(scripting.filesystemobject)
    if not fso.folderexists(directory) then
    msgbox photo directory not exists.
    exit function
    end if
    set objfiles = fso.getfolder(directory).files
    newdirectory = fso.getfolder(.).path & \ & newdirectory & \
    for each objfile in objfiles
    if split(objfile.name, .)(1) <> db then
    set doc = app.open(objfile.path)
    set app.activedocument = doc
    resizeimg(doc)
    doc.saveas newdirectory & split(objfile.name, .)(0) & .gif, gifopt, true, psextensiontype_pslowercase
    call doc.close(psdonotsavechanges)
    set doc = nothing
    end if
    next
    set app = nothing
    end function
    function deletefiles(strfolder)
    set objfso = createobject(scripting.filesystemobject)
    set objfolder = objfso.getfolder(strfolder)
    set objfiles = objfolder.files
    for each objfile in objfiles
    objfile.delete
    next
    set objfso = nothing
    end function
    比較了一下,gif縮略圖體積最小,所以就gif縮略圖。關(guān)于vbs調(diào)用photoshop,在photoshop的c:\program files\adobe\adobe photoshop cs4\scripting\documents目錄下是說(shuō)明文檔,c:\program files\adobe\adobe photoshop cs4\scripting\sample scripts目錄下是示例代碼。如果要生成png縮略圖,可以參考文檔修改腳本相應(yīng)的代碼即可:
    set pngopt = createobject(photoshop.pngsaveoptions)
    with pngopt
    .interlaced = false
    end with
    開始打算是調(diào)用set jpeg = createobject(persits.jpeg)來(lái)生成縮略圖,好處是不用加載龐大的photoshop,生成縮略圖速度很快,但比起photoshop圖片質(zhì)量差了一些,就放棄了。
    本來(lái)的打算是不保存原圖,直接打開網(wǎng)路圖片,然后直接生成縮略圖到本地。雖然photoshop可以打開網(wǎng)絡(luò)圖片,但在腳本里調(diào)用photoshop打開網(wǎng)絡(luò)圖片就不行,只好先保存網(wǎng)絡(luò)圖片到本地,然后再生成縮略圖。
    其實(shí)photoshop自帶了圖片批處理功能:
    窗口->動(dòng)作->創(chuàng)建新動(dòng)作->在ps中打開所有你想做的圖片->選擇其中一張圖片,調(diào)整大小,另存為gif格式->關(guān)閉你已做好的圖片->停止播放/記錄。
    文件->自動(dòng)->批處理->“動(dòng)作”欄中選你剛剛新創(chuàng)建的動(dòng)作名稱->點(diǎn)“源”下面的“選擇”選擇你想要處理照片的文件夾->“目標(biāo)”下面“選擇”另外一個(gè)你想保存縮略圖的文件夾->確定。就ok了!
    但比起程序來(lái),顯然程序要靈活的多,而且很多批處理效果只能靠程序?qū)崿F(xiàn),所以沒有通過(guò)錄制動(dòng)作來(lái)生成縮略圖。
    生成相應(yīng)的圖片格式代碼,也可以在地址欄輸入以下js代碼:
    javascript:d=prompt(圖片配字,);e=d.split( );a=document.getelementbyid(fontzoom);b=a.getelementsbytagname(img);c=0###;for(i=0;i<b.length;i++){if(i>0) c+=***;c=c+b[i].src+@@@/small/+b[i].src.substring(b[i].src.lastindexof(/)+1).replace(jpg,gif)+@@@+e[i];}window.prompt(復(fù)制,c);void(0);