标题 | 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); |
随便看 |
|
在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。