VBS invokes code _vbs for Photoshop batch generation of thumbnails

Source: Internet
Author: User

Imitate Tencent News page, to Kingcms added the news page picture on-demand code, code requirements of the picture on demand format as follows:

0## #yun_qi_img/@@@/small/123.gif@@@8 Title one ***yun_qi_img/@@@/small/456.gif@@@ 标题 two ***yun_qi_img/@@@/small/789. gif@@@ 标题 Three

The format is explained as follows:

0 on behalf of the No. 0 page appears the picture on demand;

yun_qi_img/is the first original image address. /small/123.gif is the first thumbnail address, the original image and the name of the thumbnail, the suffix is not the same, the original image is JPG, thumbnails are gif. Title one is the descriptive text of the first picture;

The second and third picture formats are the same as the first image;

###, @@@ 、 * * * are the corresponding separators.

-------------------------------------------------Split Line--------------------------------------------------------

At first I wrote these picture formats by hand, and found that the efficiency was very low, only two news stories were published in the afternoon, then the corresponding VBS script was written.

Script one: Collect news pictures, and generate the corresponding image format code

Directory = "Original Map"
Directory = CreateObject ("Scripting.FileSystemObject"). GetFolder ("."). Path & "\" & Directory & "\"

Call Deletefiles (Directory)

strURL = InputBox ("Please enter URL:")
If strurl <> "" Then
Call GetImages (strURL)
End If

Function getimages (strURL)
Set ie = WScript.CreateObject ("Internetexplorer.application")
Ie.visible = True
Ie.navigate strURL
Todo
Wscript.Sleep 500
Loop Until ie. Readystate=4
Set Objimgs = ie.document.getElementById ("Fontzoom"). getElementsByTagName ("img")

Strtitles = InputBox ("Please enter a picture to match word:")
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 "Please copy the result:", 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

Script Two: Call Photoshop Batch Build thumbnails

directory = "original Diagram" ' folder for original image '
newdirectory = "Thumbnail" Save a folder to reduce the figure

Const psdonotsavechanges = 2
Const psextensiontype_pslowercase = 2
Const psdisplaynodialogs = 3
Const pslocalselective = 7
Const Psblackwhite = 2
Const Psnodither = 1

Limitheight = 58 ' max height
Imgresolution = 72 ' Resolution

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
To compare, the GIF thumbnail is the smallest volume, so the GIF thumbnail. About the VBS call Photoshop, in Photoshop C:\Program files\adobe\adobe photoshop cs4\scripting\documents Directory is the description document,C : \program Files\adobe\adobe Photoshop cs4\scripting\sample Scripts directory is sample code. If you want to generate a PNG thumbnail, you can refer to the appropriate code for the document modification script:

Set pngopt = CreateObject ("Photoshop.pngsaveoptions")
With pngopt
. interlaced = False
End With

The first intention is to call set Jpeg = CreateObject ("Persits.jpeg") to generate thumbnails, the advantage is not to load a large Photoshop, the production of thumbnails is very fast, but the quality of the Photoshop picture than the poor, give up.

The intention is not to save the original image, directly open the network picture, and then directly generate thumbnails to local. Although Photoshop can open the network picture, but in the script calls Photoshop to open the network picture is not good, had to save the network picture to the local first, then produces the thumbnail image.

In fact, Photoshop has its own image batch processing function:

Window-> Action-> create a new action-> open all the pictures you want to make in PS-> Select one of the pictures, resize, save as GIF format-> close the pictures you have done-> stop playing/recording.
File-> automatic-> batch processing-> The Actions bar Select the action name you just created-> click "Choose" Below "source" to select the folder you want to work with photos-> "Select" The other folder you want to save the thumbnail for-> OK. It's OK!

However, the program is more flexible than the program, and many batch effects can only be implemented by the program, so there is no recording action to generate thumbnails.

Generate the appropriate picture format code, you can also enter the address bar the following JS code:

Javascript:d=prompt ("Picture with word", ""); 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 ("Copy", C); void (0);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.