8 Replies Latest reply on Jul 1, 2010 12:26 PM by Michael L Hale

    Adding a layer with image from URL

    pretzel2307 Level 1

      I am trying to write a script that adds a layer to a document, and loads an image from a url such as "http://fantasy411.mlblogs.com/ron-burgundy.jpg" .

       

      How can I go about doing this? Thanks in advance.

        • 1. Re: Adding a layer with image from URL
          Muppet Mark-QAl63s Level 4

          On the mac I would look to using app.system() and passing it 'curl -o' 'localPath' 'remotePath' then place the file as smart object. Im at a CS2 mac at the moment so I can't test this…

          • 2. Re: Adding a layer with image from URL
            Michael L Hale Level 5

            If you have a newer version of Photoshop that supports sockets you can do something like this.

             // openImageFromWeb.jsx
             // Copyright 2006-2009
             // Written by Jeffrey Tranberry
             // Photoshop for Geeks Version 3.0
             // modified by MLH
            
             /*
             Description:
             This sample script shows how to download images from a web server using the
             Socket object.
             */
            
             // Note: Socket.read() parameter & behavior
             // Socket.read() will read or time out. It may not read all data fromserver.
             // Socket.read(999999) will read 999999 bytes, or timeout, or socket will be
             // closed by the server.
            
             // enable double clicking from the
             // Macintosh Finder or the Windows Explorer
             #target photoshop
            
             // Make Photoshop the frontmost application
             app.bringToFront();
            
             /////////////////////////
             // SETUP
             /////////////////////////
            
             var socket = new Socket;
             var html = "";
             var domain = "www.adobe.com" // the domain for the file we want
             var sImg = "/ubi/globalnav/include/adobe-lq.png"; // the rest of the url for the file we want
             var port = ":80"; // the port for the file we want
            
             /////////////////////////
             // MAIN
             /////////////////////////
            
            
             var f = File("~/socket_sample_" + sImg.substr(sImg.length-4)); // 4 = .gif or .jpg
             f.encoding = "binary"; // set binary mode
             f.open("w");
            
             if (socket.open(domain + port, "binary")){
                    // alert("GET " + sImg +" HTTP/1.0\n\n");
                    socket.write("GET " + sImg +" HTTP/1.0\n\n"); // get the file
                    var binary = socket.read(9999999);
                    binary = removeHeaders(binary);
                    f.write(binary);
                    socket.close();
             }
            
             f.close();
             if(app.documents.length == 0) app.documents.add(new UnitValue(200,'px'), new UnitValue(200,'px'), 72, 'Untitled 1');
             placeSmartObject( f );
             f.remove(); // Remove temporary downloaded files
            
             /////////////////////////
             // FUNCTIONS
             /////////////////////////
            function placeSmartObject(fileRef){
            //create a new smart object  layer using a file
                 try { 
                      var desc = new ActionDescriptor();
                           desc.putPath( charIDToTypeID( "null" ), new File( fileRef ) );
                          desc.putEnumerated( charIDToTypeID( "FTcs" ), charIDToTypeID( "QCSt" ),charIDToTypeID( "Qcsa" ));
                          desc.putUnitDouble( charIDToTypeID( "Wdth" ),charIDToTypeID( "#Prc" ), 100 );
                          desc.putUnitDouble( charIDToTypeID( "Hght" ), charIDToTypeID( "#Prc" ), 100 );
                          desc.putUnitDouble( charIDToTypeID( "Angl" ), charIDToTypeID( "#Ang" ), 0 );
                          desc.putBoolean( charIDToTypeID( "Lnkd" ), true );
                           executeAction( charIDToTypeID( "Plc " ), desc, DialogModes.NO );
                           activeDocument.activeLayer.resize(100 ,100,AnchorPosition.MIDDLECENTER);
                           activeDocument.revealAll();
                  } catch (e) {
              if (!e.toString().match(/Place.+is not currently available/)) {
                  throw e;
                  }
              }
            };
             // Remove header lines from HTTP response
             function removeHeaders(binary){
                    var bContinue = true ; // flag for finding end of header
                    var line = "";
                    var nFirst = 0;
                    var count = 0;
                    while (bContinue) {
                    line = getLine(binary) ; // each header line
                    bContinue = line.length >= 2 ; // blank header == end of header
                    nFirst = line.length + 1 ;
                    binary = binary.substr(nFirst) ;
                    }
                    return binary;
             }
            
            
             // Get a response line from the HTML
             function getLine(html){
                    var line = "" ;
                    for (var i = 0; html.charCodeAt(i) != 10; i++){ // finding line end
                    line += html[i] ;
                    }
                    return line ;
             }
            

            You may have to check your firewall setting if you have one and it doesn't work with some servers. At least I can not get it to work with some.

            1 person found this helpful
            • 3. Re: Adding a layer with image from URL
              Muppet Mark-QAl63s Level 4

              I've not seen that 'socket' thing before what versions of CS can you do that with Mike? and is it available across the suite? I should imagine its a lot more robust than what I had in mind too. curl did work when I checked using bridge to make the system call…

              • 4. Re: Adding a layer with image from URL
                Michael L Hale Level 5

                CS3 or higher. You may be able to use Bridgetalk and access the file by HTTP with CS2, I can't remember.

                • 5. Re: Adding a layer with image from URL
                  Muppet Mark-QAl63s Level 4

                  BridgeTalk is what I've have very little luck with in CS2… Some success with my trails in CS5 which is promising because inter-app comm is possibly what I need most I can either process PS & AI files before they get placed in ID (what I try do most) or deal with the links later.

                  • 6. Re: Adding a layer with image from URL
                    pretzel2307 Level 1

                    that example does work with using the image provided in the code, but if I try and put in my own image from a location it does not work.. for example the following image would not load

                     

                    var domain = "www.wallpapers247.com" // the domain for the file we want

                    var images = ["/images/wallpapers/eiffel-tower-1024-833291.jpeg"];

                     

                    Anyone able to help?

                    • 7. Re: Adding a layer with image from URL
                      Paul Riggott Level 6

                      The problem is in the extension, the code is set up for a 3 character extension, to get it to work use:-

                       var f = File("~/socket_sample_" + sImg.substr(sImg.length-5)); // 4 = .gif or .jpg
                      
                      
                      • 8. Re: Adding a layer with image from URL
                        Michael L Hale Level 5

                        A better fix might be

                         var f = File("~/socket_sample_" + sImg.match( /(.*)(\.[^\.]+)/ )[2]);
                        

                        And unless you edited the script it does not take an array so the brackets in

                        var images = ["/images/wallpapers/eiffel-tower-1024-833291.jpeg"];
                        

                        might also be a problem.

                        1 person found this helpful