views:

265

answers:

2

I am trying to have this function output data and the rescaled image after every loop but it doesn't display anything until it is finished looping through the complete array. I am assuming that this has something to do with the PHP image functions but is there a workaround?

function resize_images($images_l){
echo "Resizing Images<br>";
$new_height = 200;
$new_width = 200;
$c = 1;

foreach($images_l as $filename) {
 echo "Image" . $c . "<br>";
 $c++;


 // Get the new dimensions
 list($width, $height) = getimagesize($filename);
 echo $new_width . ", " . $new_height . "<br>";

 // Create the new image
 $image_blank = imagecreatetruecolor($new_width, $new_height);
 $baseimage = imagecreatefromjpeg($filename);
 imagecopyresampled($image_blank, $baseimage, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

 // Save the file
 imagejpeg($image_blank, 's_' . $filename , 100);

 echo "<img src=\"s_$filename\">";
}
return TRUE; }

Thanks

A: 

I assume this is on your web server? Your server only sends a web page to the client once, when it's done running. If you want to show all the intermediate stages you need to build a page that automatically refreshes or use javascript to reload the image at an interval.

If this is on CLI then how are you viewing the images. You post doesn't really make sense.

Dennis Baker
Sorry yes this will be operating on my own local machine. Of course AJAX would be nice, but in the mean time how would I go about refreshing the page after each loop?
@razass: It seems pretty clear that Dennis said that the page is only sent ONCE to the client, thus it is IMPOSSIBLE to make incremental changes to it via php. Implementing this using jQuery will be a joke, taking 5 minutes max.
tomzx
You can't refresh the image after each loop. It is one stream of data that goes to your web client that doesn't start displaying the data until the stream is CLOSED... which doesn't happen until after your loop finishes.
Dennis Baker
+1  A: 

I suggest you take a look at Output Control Functions, you could flush each image after its been re-scaled.

EXAMPLE

<?php

function gradual_output()
{
    # start using the output buffer.
    ob_start();

    for ($i = 0; $i <= 10; $i++) {
        echo "loop count: " . $i . "<br />\n";

        # flush the buffer to the user.
        ob_flush(); flush();

        # do something that takes a while...
        sleep(2);
    }

    # were done with the buffer, clean up.
    ob_end_clean();
}

gradual_output();

?>

Anything you echo out after calling ob_start() will be put into a buffer, you can decide when you want to flush this buffer to the user.

Luke Antins
This works perfect! by placing the ob_start() outside of the loop, and then flushing and clearing the data at the end of each loop the re-sized image and data is outputted to the screen in order.
Thanks a bunch!