這篇文章主要為大家分享了通過vbs調用photoshop批量生成縮略圖,需要的朋友可以參考下
模仿騰訊新聞頁,給kingcms添加了新聞頁圖片點播的代碼,代碼要求的圖片點播格式如下:
0###http://www.website.org/uploadfile/123.jpg@@@/small/123.gif@@@8標題一***http://www.website.org/uploadfile/456.jpg@@@/small/456.gif@@@標題二***http://www.website.org/uploadfile/789.jpg@@@/small/789.gif@@@標題三
格式解釋如下:
0代表第0頁出現(xiàn)圖片點播;
http://www.website.org/uploadfile/123.jpg是第一幅原圖地址。/small/123.gif是第一幅縮略圖地址,原圖和縮略圖名字一樣,后綴不一樣,原圖是jpg,縮略圖是gif。標題一是第一幅圖片的說明文字;
第二幅、第三幅圖片格式和第一幅圖一樣;
###、@@@、***為相應的分隔符。
-------------------------------------------------分割線--------------------------------------------------------
開始我是用手工來寫這些圖片格式,發(fā)現(xiàn)效率很低,一下午只發(fā)布了兩篇新聞,就編寫了相應的vbs腳本。
腳本一:采集新聞圖片,并生成相應的圖片格式代碼
directory = 原始圖
directory = createobject(scripting.filesystemobject).getfolder(.).path & \ & directory & \
call deletefiles(directory)
strurl = inputbox(請輸入網(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(請輸入圖片配字:)
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 請復制結果:, , 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
腳本二:調用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縮略圖。關于vbs調用photoshop,在photoshop的c:\program files\adobe\adobe photoshop cs4\scripting\documents目錄下是說明文檔,c:\program files\adobe\adobe photoshop cs4\scripting\sample scripts目錄下是示例代碼。如果要生成png縮略圖,可以參考文檔修改腳本相應的代碼即可:
set pngopt = createobject(photoshop.pngsaveoptions)
with pngopt
.interlaced = false
end with
開始打算是調用set jpeg = createobject(persits.jpeg)來生成縮略圖,好處是不用加載龐大的photoshop,生成縮略圖速度很快,但比起photoshop圖片質量差了一些,就放棄了。
本來的打算是不保存原圖,直接打開網(wǎng)路圖片,然后直接生成縮略圖到本地。雖然photoshop可以打開網(wǎng)絡圖片,但在腳本里調用photoshop打開網(wǎng)絡圖片就不行,只好先保存網(wǎng)絡圖片到本地,然后再生成縮略圖。
其實photoshop自帶了圖片批處理功能:
窗口->動作->創(chuàng)建新動作->在ps中打開所有你想做的圖片->選擇其中一張圖片,調整大小,另存為gif格式->關閉你已做好的圖片->停止播放/記錄。
文件->自動->批處理->“動作”欄中選你剛剛新創(chuàng)建的動作名稱->點“源”下面的“選擇”選擇你想要處理照片的文件夾->“目標”下面“選擇”另外一個你想保存縮略圖的文件夾->確定。就ok了!
但比起程序來,顯然程序要靈活的多,而且很多批處理效果只能靠程序實現(xiàn),所以沒有通過錄制動作來生成縮略圖。
生成相應的圖片格式代碼,也可以在地址欄輸入以下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(復制,c);void(0);