1 Reply Latest reply on Aug 16, 2010 5:08 AM by Muppet Mark-QAl63s

    Adding a layer with image from URL

    pretzel2307 Level 1

      A while ago I posted this question and I got a good answer, but now I need to get an image via HTTPS instead of HTTP.

       

      Here is what currently works for a HTTP url..

       

      Any help is greatly appreciated!

       

       // 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 ;
       }