欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

VBS调用Photoshop批量生成缩略图的代码

程序员文章站 2022-03-21 16:43:53
模仿腾讯新闻页,给kingcms添加了新闻页图片点播的代码,代码要求的图片点播格式如下: 0###http://www.website.org/uploadfile/12...

模仿腾讯新闻页,给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页出现图片点播;

http://www.website.org/uploadfile/123.jpg是第一幅原图地址。/small/123.gif是第一幅缩略图地址,原图和缩略图名字一样,后缀不一样,原图是jpg,缩略图是gif。标题一是第一幅图片的说明文字;

第二幅、第三幅图片格式和第一幅图一样;

###、@@@、***为相应的分隔符。

-------------------------------------------------分割线--------------------------------------------------------

开始我是用手工来写这些图片格式,发现效率很低,一下午只发布了两篇新闻,就编写了相应的vbs脚本。

脚本一:采集新闻图片,并生成相应的图片格式代码

directory = "原始图"
directory = createobject("scripting.filesystemobject").getfolder(".").path & "\" & directory & "\"

call deletefiles(directory)

strurl = inputbox("请输入网址:")
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图片质量差了一些,就放弃了。

本来的打算是不保存原图,直接打开网路图片,然后直接生成缩略图到本地。虽然photoshop可以打开网络图片,但在脚本里调用photoshop打开网络图片就不行,只好先保存网络图片到本地,然后再生成缩略图。

其实photoshop自带了图片批处理功能:

窗口->动作->创建新动作->在ps中打开所有你想做的图片->选择其中一张图片,调整大小,另存为gif格式->关闭你已做好的图片->停止播放/记录。
文件->自动->批处理->“动作”栏中选你刚刚新创建的动作名称->点“源”下面的“选择”选择你想要处理照片的文件夹->“目标”下面“选择”另外一个你想保存缩略图的文件夹->确定。就ok了!

但比起程序来,显然程序要灵活的多,而且很多批处理效果只能靠程序实现,所以没有通过录制动作来生成缩略图。

生成相应的图片格式代码,也可以在地址栏输入以下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);