网站首页  汉语字词  英语词汇  考试资料  写作素材  旧版资料

请输入您要查询的考试资料:

 

标题 vbs调用photoshop批量生成缩略图的代码
内容
    这篇文章主要为大家分享了通过vbs调用photoshop批量生成缩略图,需要的朋友可以参考下
    模仿腾讯新闻页,给kingcms添加了新闻页图片点播的代码,代码要求的图片点播格式如下:
    0###/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(请输入网址:)
    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);
随便看

 

在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。

 

Copyright © 2002-2024 cuapp.net All Rights Reserved
更新时间:2025/5/17 23:00:16