SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Generate graphs on server, display in browser

    Hello, this is my first post here. I have quite a bit of Win32 and Javascript programming experience but am new to server-side scripting. Here is my problem:

    I need to generate images on a server and send them to the user. In the HTML file, Javascript of the type

    document.images[1].src=TmpImg

    is waiting for the newly created image.

    This image must be generated by a Windows application running either on the Windows 2003 server or on a PC to which the server has access. The app is heavy, so instead of launching it for every request, I plan to launch a tiny executable that sends a request string to the app.

    So the logic is roughly:
    - user clicks a link saying "give me a new image with x=1, y=2, z=3"
    - script launches tiny executable on server
    - tiny executable sends WM_xx message to big app saying "generate image with x=1, y=2, z=3"
    - when big app has created image, it informs tiny executable "image is done, filename is MyTmp123.png"
    - tiny executable informs browser that image is ready
    - a call to document.images[1].src=MyTmp123.png replaces current image with new image.

    I thought this was an extremely simple setup, but my searches tell me it's not. Exec and System seem not to be widely used, and the idea of having a GUI app running on a server seems not so popular (I can parse the request manually to prevent exploits).

    Efficiency and speed are not important - there will not be many users. But I would be grateful for hints to a solution that is simple enough for a newbie in server-side scripting.

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,182
    Mentioned
    191 Post(s)
    Tagged
    2 Thread(s)
    Well, if you're on Windows, I guess GD and GIMP are out. Maybe WIA ??
    http://www.microsoft.com/downloads/d...displaylang=en

  3. #3
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague View Post
    Well, if you're on Windows, I guess GD and GIMP are out. Maybe WIA ??
    Thanks, but I have the app that generates the image, and do control it perfectly in a desktop environment. I want to know how I can do the same on a server... and how to send the generated image to the user's browser.

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,182
    Mentioned
    191 Post(s)
    Tagged
    2 Thread(s)
    My computer bogged out before I got to edit my post to say hi. So

    Hi DosFan, welcome to the forums,

    I did a little searching, and it seems GD does have a Windows version (Win32)
    http://www.libgd.org/releases/

  5. #5
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague View Post
    My computer bogged out before I got to edit my post to say hi. So

    Hi DosFan, welcome to the forums,

    I did a little searching, and it seems GD does have a Windows version (Win32)
    Thanks, really, I appreciate, but it's not what I am looking for. I am perfectly able to generate the images. It took me several years to develop my image package, and gd is definitely not able to generate the highly specific images I need. Generating images is not my problem - getting them back to the user's browser is what I do not understand.

  6. #6
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,182
    Mentioned
    191 Post(s)
    Tagged
    2 Thread(s)
    If an image is available, the browser can download it. If you do something like "click here for image" the server script can generate the image but the browser will "hang" while it's waiting. Not good if it takes a while. To get it to populate a javascript array, or otherwise show via javascript code, You need a way for the browser to know when it's ready. PHP can't "push" to the browser, but the browser can "pull" from a PHP file. In other words, the server doesn't so much "inform" the page as it does "answer" it. I'm not sure if all browsers would act as expected if you used AJAX to send a request, and then wait for the response. If it worked, only the AJAX script would "hang", not the page in the browser. And you could trigger a "waiting" type of thing to let user's know what's going on. It might be better to have AJAX periodically check a file for a ready=true url=path response and then make the request for the image.

  7. #7
    Your Lord and Master, Foamy gold trophy Hierophant's Avatar
    Join Date
    Aug 1999
    Location
    Lancaster, Ca. USA
    Posts
    12,305
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Why not just use Google Charts, Open Flash Chart or some other existing chart API available on the web?

    Other than that, both GD and Imagemagick work on the Windows OS.
    Wayne Luke
    ------------


  8. #8
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Hierophant View Post
    Why not just use Google Charts, Open Flash Chart or some other existing chart API available on the web?

    Other than that, both GD and Imagemagick work on the Windows OS.
    I am very sorry that apparently I don't manage to get the message across: I have my own image software. I do NOT need any image software. It is all described above.

  9. #9
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    AJAX is definitely what you need. Wouldn't be hard for you to implement either with your Javascript experience. There's tons of stuff out there on AJAX

  10. #10
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, so you want to run a single instance of your image creation application and just let it run as a daemon, waiting for something to talk to it, so it can do something and then it can reply with the result to that client.

    And if I understand correctly, you want to use php to execute a tiny executable process to interface between php and your daemon. And when the image is all done and created and the tiny executable gets the resulting filename, it will provide this filename to php and then the tiny executable process can just die off.

    It sounds like exec() is what you want. Theres also proc_open() but it doesnt sound like you need to actually do any real back and forth communication, just send some data once, and wait for the reply, so exec would be simpler.

    PHP Code:
    // this script receives the http request initiated by javascript, and sends a response to it
    $arg_string prepare_the_args($_POST['foo'], $_POST['bar']);

    // php will hang waiting for the executable to exit so it can return
    exec('/foo/bar/tiny_executable.exe ' $arg_string$result);

    // reply to javascript with the filename
    echo $result[0]; 
    I'm guessing your tiny executable would read stdin to get the arg_string, and write to stdout once it has aquired the filename. I say guessing because I haven't done any of this type of stuff yet

    If that sounds right, I can help with the javascript part too.

  11. #11
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    Ok, so you want to run a single instance of your image creation application and just let it run as a daemon

    I'm guessing your tiny executable would read stdin to get the arg_string, and write to stdout once it has aquired the filename. I say guessing because I haven't done any of this type of stuff yet

    If that sounds right, I can help with the javascript part too.
    That sounds perfect, thanks a lot. TinyApp could receive the querystring via the commandline, too? How do I recognise in javascript that FatApp has finished? I also considered a "passive" solution, i.e. send the request, wait a second and try to load the image - no communication needed.

    There is one point that worries me: Is it possible to run a Windows app, with real windows and message loops, on a server? I can move the window of FatApp outside the visible area, no problem, but I need an active window to do the drawing. If that is not possible, the only workaround would be to run the daemon on another desktop PC, and use net DDE to launch the request. Clumsy, and it needs extra hardware. Tomorrow I'll have access to the server, we'll see.
    Thanks again for reading my post thoroughly, crmalibu

  12. #12
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DosFan View Post
    TinyApp could receive the querystring via the commandline, too?
    I'd recomend doing some validation before you put anything into the command you feed to exec(). see escapeshellarg() to escape, but consider some strong validation too. You need to program very defensively on the web.


    Quote Originally Posted by DosFan View Post
    How do I recognise in javascript that FatApp has finished?
    This is where AJAX comes in, which is just an asynchronous functionality of javascript. When sending an ajax http request, the browser will send the request in the background asynchronously. The user can continue using the webpage, and will be unaware the browser is actually sending/loading something. Once the browser receives the response(from your php script), javascript will fire the onreadystatechange event. You can have javascript execute a callback function when this event gets fired. Your function would examine the response text your php script sent, and then make changes to the webpage based on it.

    I'll use the jquery javascript library to simplify the ajax code needed.
    Code:
    <html>
    <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js"></script>
    <script type="text/javascript">
    function myCallback(phpResponseText) {
        if (phpResponseText.length < 1) {
            var str = '...nothing...';
        } else {
            var str = phpResponseText;
            document.getElementById('img').src = 'http://example.com/images/' + str;
        }
        document.getElementById('phpsays').innerHTML = str;
    }
    function sendRequest() {
        var m = document.getElementById('message').value;
        var c = document.getElementById('color').value;
        var postData = {message : m, color: c};
        var phpScriptUrl = 'http://example.com/script.php';
        // jquery will send the ajax post request, posting the key values pairs found in 'data'
        // when it gets a reply, it will execute myCallback
        $.post(phpScriptUrl, postData, myCallback, "text");
    }
    </script>
    </head>
    <body>
    
    php says:<span id="phpsays"></span><br>
    <img id="img"><br>
    
    message <input id="message"><br>
    color <input id="color"><br>
    <input type="button" onclick="sendRequest()">
    </body>
    </html>


    Quote Originally Posted by DosFan View Post
    There is one point that worries me: Is it possible to run a Windows app, with real windows and message loops, on a server? I can move the window of FatApp outside the visible area, no problem, but I need an active window to do the drawing. If that is not possible, the only workaround would be to run the daemon on another desktop PC, and use net DDE to launch the request. Clumsy, and it needs extra hardware. Tomorrow I'll have access to the server, we'll see.
    I have no idea. My experience in programming for the desktop is limited to using the shell, and a few hello world programs in java.

    *Consider some type of flood control for this whole thing. A user could send a ton of image creation requests to try to DOS you, and without some type of flood control or queue, you may end up with a ton of tinyapp processes running.

  13. #13
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am advancing. After a few hours of fumbling with the manual php install, I decided for the msi and got it running as fastcgi - just guessing that fastcgi was as good as normal or isapi, no hints provided by the installer.

    So finally my first php script ran, and produced an "unable to fork" message for hello.exe...

    Eventually, I managed to get hello.exe run, and found out that some things work, others not. MessageBox, for example, just produces a beep, and then the browser hangs (but can be stopped with Escape or killed with Task Manager).

    It seems that all Windows functions that require a desktop don't work, but standardout echoes text directly into my browser. Greaaaat!

    Will keep trying, thanks a lot for your help

  14. #14
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just an update to a very old thread: I got it working. FatApp is running on a Windows server as a normal GUI app, and interprocess communication is done via files on disk. Not very elegant, but reliable...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •