fush not flushing as expected

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • beary
    New Member
    • Nov 2006
    • 170

    fush not flushing as expected

    Hi,

    Can someone please tell me why the code below is not flushing as I want it to? I need it to put a dot on the screen every second. All it's doing is echoing the whole thing at the end of 20 seconds. php.ini output_bufferin g is set to 0. Thanks.

    Code:
    <?php
    header("Content-type: image/png");
    $im = imagecreatetruecolor(600, 600);
    
    $white = imagecolorallocate($im, 255, 255, 255);
    imagefill($im, 0, 0, $white);
    
    $ptx=rand(0,600);
    $pty=rand(0,600);
    $xarr=array(300,0,600);
    $yarr=array(0,600,600);
    
    $i=0;
    ob_start();
    while($i<20)
    	{
    
    	$vertx=$xarr[array_rand($xarr)];
    	$verty=$yarr[array_search($vertx,$xarr)];
    	$ptx=($ptx+$vertx)*0.5;
    	$pty=($pty+$verty)*0.5;
    	imagefilledellipse($im, $ptx, $pty, 2, 2, imagecolorallocate($im, 255, 0, 0));
    	$i++;
    	ob_flush();
      flush();
    	usleep(1000000);
    	}
    
    imagepng($im);
    imagedestroy($im);
    ?>
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    This is a guess: even though PHP is able to flush the output to the browser, the browser itself is able to implement an output buffer and withhold that buffer until it deems itself ready to process it. I imagine this will be the case when working with binary data (images, etc.)

    Mark.

    Comment

    • beary
      New Member
      • Nov 2006
      • 170

      #3
      Yes, I also figured the image header might have something to do with it. Does anyone know for sure?

      Comment

      • Canabeez
        New Member
        • Jul 2009
        • 126

        #4
        Hey there... I'm not sure but I think you should run the [code=php]imagepng($im);[/code] within the while() otherwise you're creating the image with a while() and just then flushing it to the browser.

        You might also want to set_time_limit( 0); for this code, otherwise some servers might break your code after 30 seconds.

        Hope this helps

        Simon

        Comment

        Working...