|
|
||||||
Don't forget to see the The Developer's Notes where you can learn more tips and tricks! With Adobe Photoshop CS2 installed on a Windows Server 2003 web server, you CAN use the power of Photoshop CS2 to create images and perform other tasks by merely accessing a regular ASP web page with a web browser. You don't need to run the CMD.EXE in your code and you don't need to create any .BAT files either. The big question that you may have is...how can you do something like accept parameters used to create an image, pass those to your ASP page and in turn, pass that to Adobe Photoshop CS2 so that it creates the image? Although tedious to initially setup, the solution is simple...and is largely possible due to JSX importing that you can do with Adobe Photoshop CS2 (you configure a JSX file with parameters and commands similar to Javascript which Adobe Photoshop CS2 completes). Overview
SERVER STEPS:
This code snippet is helpful if you are in the testing phase of trying to create a file on the web server using a similar process as how an image file would be created on the web server with Photoshop CS2. If this code snippet does not work, you have a permissions issue to deal with. -- BEGIN TEST CODE SNIPPET <% dim appName, cmdLocation, locale appName = "name_of_asp_page.asp" cmdLocation = "c:\windows\system32\" locale = Server.MapPath(appName) : locale = replace(locale, appName, "") set sbatch = server.CreateObject("WScript.Shell") set labor = sbatch.Exec(cmdLocation & "cmd.exe /C echo test >> " & locale & "temp.txt") sbatch.close set sbatch = nothing : set labor = nothing set appName = nothing : set cmdLocation = nothing : set locale = nothing %> -- END TEST CODE SNIPPET PHOTOSHOP CS2 CODE SNIPPET: This code snippet is helpful when you've reached the point of actually having Photoshop CS2 run on the web server as a network service to create an image and having the process spawned through an ASP web page on the web server. Make sure your JSX is correct and has no errors (accomplished by double-clicking on the JSX file while you are locally at the web server; double-clicking should spawn Photoshop CS2 to execute the JSX; the JSX then creates the image and saves it on the web server). The Photoshop CS2 application will stay active on the web server as a Network Service after it is completed with creating the image. Typically this is not an issue considering it takes between 30 seconds to 1 minute for Photoshop CS2 to load...so most likely you will not want to repeatedly open and close the application and degrade server performance each time an image is created. Since Photoshop CS2 loads as a Network Service you will not see the application open on the Task Bar. You can only see the Photoshop CS2 process by viewing the Process tab of Task Manager on the web server. -- BEGIN PHOTOSHOP CS2 CODE SNIPPET <% dim appName, dbJsx, locale appName = "name_of_asp_page.asp" dbJsx = "my_jsx.jsx" locale = Server.MapPath(appName) : locale = replace(locale, appName, "") set sbatch = server.CreateObject("WScript.Shell") sbatch.Run locale & dbJsx, 0, FALSE sbatch.close set sbatch = nothing : set appName = nothing : set dbJsx = nothing : set locale = nothing %> -- END PHOTOSHOP CS2 CODE SNIPPET CLOSING DOWN PHOTOSHOP CS2 VIA A ASP WEB PAGE: You can close down the Photoshop CS2 Network Service process through an ASP web page if you want and can be accomplished using the ASP code snippet below (since there is no way I am aware of, as of this writing, to signal Photoshop CS2 to close itself down through JSX code): -- BEGIN CLOSE SNIPPET <% dim cmdLocation cmdLocation = "c:\windows\system32\" set sbatch = Server.CreateObject("WScript.Shell") set labor = sbatch.Exec(cmdLocation & "cmd.exe /C taskkill /F /IM Photoshop.exe") sbatch.close set sbatch = nothing : set labor = nothing %> -- END CLOSE SNIPPET STEP 2 Now that it has been established that Adobe Photoshop CS2 can be invoked from an ASP web page and that it can read and complete the actions in a simple JSX file you have written (a JSX file is just a text file with the JSX extension), it's time to add on what can be done. In this case, we'll prompt for some text so that the image that will be created will have some custom text inside of it. <html> <body> <form method="post" action="make_image.asp"> <input type="text" name="text" size="50" maxlength="50" /> Enter some text to put into the image<br /> <input type="submit" value="Create Custom Image" /> </form> </body> </html> STEP 3 Now it's time for the meat and potatoes. This ASP page (which should be the name of the ASP file you point to in the form (above)) will take the form data, create or modify the .JSX file and spawn Adobe Photoshop CS2 so that it can read the .JSX file and create the image on the web server. Ready for the code? Here it is... <% dim appName, dbJsx, errInfo, height, width, title, tcolor, bcolor, name, txt_font, txt_size, txt_horizontalScale, txt_verticalScale, txt_translateX, txt_translateY, gif_colors
' GET Form Data title = request.form("text") ' ERROR Accumulation Sub HandleError(ecode, emsg) errInfo = errInfo & "Error Code: " & ecode & "<br />Error Description: " & emsg & "<br />" End Sub ' ERROR Probe Sub ProbeError If Err.Number <> 0 Then HandleError Err.Number, Err.Description Error.Clear End If On Error GoTo 0 End Sub ' BEGIN Error Monitoring Err.Clear On Error Resume Next locale = Server.MapPath(appName) : locale = replace(locale, appName, "") local_photoshop = replace(locale, "\", "\\") ' FILTER tcolor = replace(tcolor, "#", "") : tcolor = replace(tcolor, ";", "") bcolor = replace(bcolor, "#", "") : bcolor = replace(bcolor, ";", "") title = replace(title, """", "\""") : title = replace(title, "“", "\""") title = replace(title, "”", "\""") : title = replace(title, "‘", "'") title = replace(title, "’", "'") ' BUILD the JSX File set paper = Server.CreateObject("Scripting.FileSystemObject") if paper.fileExists(locale & dbJsx) = False Then ' Create New JSX Content set pen = paper.OpenTextFile(locale & dbJsx, 2, True) Else ' Overwrite Existing JSX Content set pen = paper.OpenTextFile(locale & dbJsx, 2, False) End if ' Actually Build The JSX Here pen.WriteLine "#target photoshop" pen.WriteLine "var imageHeight = " & height & ";" pen.WriteLine "var imageWidth = " & width & ";" pen.WriteLine "var imageText = """ & title & """;" pen.WriteLine "var textColor = """ & tcolor & """;" pen.WriteLine "var fillColor = """ & bcolor & """;" pen.WriteLine "var fileLocation = """ & local_photoshop & """;" pen.WriteLine "var originalUnit = preferences.rulerUnits;" pen.WriteLine "preferences.rulerUnits = Units.PIXELS;" pen.WriteLine "backgroundColor.rgb.hexValue = fillColor;" pen.WriteLine "foregroundColor.rgb.hexValue = textColor;" pen.WriteLine "var headerDoc = documents.add(imageWidth, imageHeight, 72, imageText, NewDocumentMode.RGB, DocumentFill.BACKGROUNDCOLOR, 1);" pen.WriteLine "headerDoc.DialogModes = DialogModes.NO;" pen.WriteLine "var textLayer = headerDoc.artLayers.add();" pen.WriteLine "textLayer.kind = LayerKind.TEXT;" pen.WriteLine "textLayer.textItem.contents = imageText;" pen.WriteLine "textLayer.textItem.font = """ & txt_font & """;" pen.WriteLine "textLayer.textItem.color = foregroundColor;" pen.WriteLine "textLayer.textItem.size = " & txt_size & ";" pen.WriteLine "textLayer.textItem.horizontalScale = " & txt_horizontalScale & ";" pen.WriteLine "textLayer.textItem.verticalScale = " & txt_verticalScale & ";" pen.WriteLine "textLayer.textItem.antiAliasMethod = AntiAlias.SHARP;" pen.WriteLine "textLayer.translate(" & txt_translateX & ", " & txt_translateY & ");" pen.WriteLine "var headerSaveOptions = new GIFSaveOptions();" pen.WriteLine "headerSaveOptions.colors = " & gif_colors & ";" pen.WriteLine "var headerFile = new File(fileLocation + """ & name & ".gif"");" pen.WriteLine "headerDoc.flatten();" pen.WriteLine "headerDoc.saveAs(headerFile,headerSaveOptions,true,Extension.LOWERCASE);" pen.WriteLine "headerDoc.close(SaveOptions.DONOTSAVECHANGES);" pen.WriteLine "preferences.rulerUnits = originalUnit;" pen.WriteLine "imageHeight = null;" pen.WriteLine "imageWidth = null;" pen.WriteLine "headerText = null;" pen.WriteLine "textColor = null;" pen.WriteLine "fillColor = null;" pen.WriteLine "fileLocation = null;" pen.WriteLine "originalUnit = null;" pen.WriteLine "headerDoc = null;" pen.WriteLine "textLayer = null;" pen.WriteLine "headerSaveOptions = null;" pen.WriteLine "headerFile = null;" pen.close() set pen = nothing set paper = nothing ' ERROR Trap ProbeError() ' SPAWN Adobe With JSX set sbatch = Server.CreateObject("WScript.Shell") sbatch.Run locale & dbJsx, 0, FALSE set sbatch = nothing ' ERROR Trap ProbeError() ' SHOW Output response.write "<html>" & vbcrlf response.write "<body>" & vbcrlf response.write errInfo & vbcrlf response.write "</body>" & vbcrlf response.write "</html>" & vbcrlf ' FREE Resources set appName = nothing : set dbJsx = nothing : set errInfo = nothing : set height = nothing : set width = nothing set title = nothing : set tcolor = nothing : set bcolor = nothing : set name = nothing : set txt_font = nothing set txt_size = nothing : set txt_horizontalScale = nothing : set txt_verticalScale = nothing set txt_translateX = nothing : set txt_translateY = nothing : set gif_colors = nothing %> MORE TIPS AND TRICKS This is just the tip of the iceburg for advanced and in some cases "the secret arts" of what you can do with web sites. Check out The Developer's Notes today! This Step-by-Step guide for educational purposes only was first made available online 12-23-2006. |
|||||||
Main Page
![]() ![]() ![]() ©CommerceWeaver Pro™ & Web Site ©2004 All Rights Reserved. Optimized for IE 6x. Terms of Service.
![]() |
|||||||