0

Ok, I'm hoping I can explain my situation rather than pasting lines and lines of code.

Currently, JSON sends positional info to my PHP file which in turn uses this data to generate an image, saves it and returns the filename via JSON back to browser. Javascript then refreshes the image on screen.

This all works fine at the moment, but I am wanting to optimise the process and look at the possibility of outputting the image file straight after it's created then save afterwards.

My ideal solution would be something like:

    header('Content-Type: image/gif');
    echo $this->canvas;

    // Save user file
    $this->canvas->writeImage( $this->userFile = 'user_img.gif' );          
    $this->canvas->destroy();

    // encode everything and send to browser
    echo json_encode(array('misc data back to the browser'));

(I still need to send data back to browser via JSON)

And in my HTML I would have the image laid out like this:

    <img src='json-processing-script.php' />

But as usual nothing is ever that simple, so I'd like to hear if anyone can make any pointers.

digout
  • 4,041
  • 1
  • 31
  • 38

1 Answers1

1

In your example, the json would be added to the gif, messing up your image. If you want to return these two completely different things from your php script, you would have to encode the image, add it to the json and extract it in the javascript to get the source of your image.

See for example this question.

Community
  • 1
  • 1
jeroen
  • 91,079
  • 21
  • 114
  • 132
  • Not as optimal as I wanted but worth testing anyway, thanks for the suggestion. – digout Nov 28 '11 at 22:38
  • 1
    @Will At least it saves the visitor a trip to the server to get the image data; that's what takes the most time I think. – jeroen Nov 28 '11 at 22:43
  • Your suggestion works fine in modern browser but not in earlier versions of IE.. There are 'workarounds' like http://dean.edwards.name/weblog/2005/06/base64-ie/ but it's still calling a file on the server.. – digout Nov 29 '11 at 15:48
  • @Will Making it work for new browsers and having a longer delay for old IE's would work form me... – jeroen Nov 29 '11 at 16:32