0

I would like to create a pure PHP progress bar without the use of jQuery and Ajax.

I found this pure PHP progress code from here stackoverflow.com

<?php
// Output a 'waiting message'
echo 'Please wait while this task completes';

// Now while waiting for a certain task to
// complete, keep outputting .'s

while (true) {
  // Echo an extra dot, and flush the buffers
  // to ensure it gets displayed.
  echo ' .';
  flush();

  // Now sleep for 1 second and check again:
  sleep(1);
}
?>

My other PHP basically converts an image to a thumbnail. Basically a 3MB image takes around 5 to 6 seconds to convert it to a thumbnail.

I am not sure where to insert my php thumbnail image resize code. I tried and entered it here but it didn't work.

while (true) {
      // Echo an extra dot, and flush the buffers
      // to ensure it gets displayed.
      echo ' .';

I am still a newbie so please forgive my bliss.

UPDATE

This is my other image resize code:

<?php

$image01 = 'http://292fc373eb1b8428f75b-7f75e5eb51943043279413a54aaa858a.r38.cf3.rackcdn.com/eac44780278d69bc98130acce093fff64214606285-1408631133-53f6015d-620x348.jpg';

$image02 = 'http://upload.wikimedia.org/wikipedia/commons/3/39/American_soldiers_watch_as_the_Tricolor_flies_from_the_Eiffel_Tower_again.jpg';

// read page 1 
$im = new imagick($image02); 

// convert to jpg 
$im->setImageFormat('jpeg'); 

//resize 
$im->resizeImage(600, 350, imagick::FILTER_LANCZOS, 1, TRUE); 

//write image on server 
$im->writeImage('thumb.jpg'); 
$im->clear(); 
$im->destroy(); 

echo '<img src="thumb.jpg"/>';

?>
Community
  • 1
  • 1
user2333968
  • 135
  • 1
  • 3
  • 13
  • 7
    It helps if you understand that PHP executes on the server, running until it has completed; and only then is the output sent to the browser.... there's a couple of quirks with web server buffering, and flushing the buffer to try and persuade the webserver to send output early; but basically you can't do this – Mark Baker Aug 22 '14 at 08:27
  • 1
    There is no such thing as pure php progress bar. – Cthulhu Aug 22 '14 at 08:28
  • you can, if you use chunked output.. but its not really a good idea. progress bars that display in the Client space, should be handled by the client side – RaggaMuffin-420 Aug 22 '14 at 08:28
  • 2
    @Cthulhu well it is possible but one of the worst ideas ever. –  Aug 22 '14 at 08:28
  • 1
    @RaggaMuffin-420 - PHP can't control it, because it all comes down to the webserver – Mark Baker Aug 22 '14 at 08:29
  • erm... I can easily create a PHP that sends a chunking header and outputs some HTML every second, and is perfectly valid.. – RaggaMuffin-420 Aug 22 '14 at 08:29
  • We can't really help without the other part of your code anyway. How it is called and how do you know it finished ? – Clément Malet Aug 22 '14 at 08:32
  • 3
    Given the context of your task, you have to invoke a method that does the image resize. It's not an operation that you can split into multiple tasks and execute one by one. It's a single method invocation that simply takes time. You can't calculate how long it's going to take and all you can do is wait. Period. In order to make the waiting period slightly easier, you can display a .gif image representing some sort of a loader, so your clients know that something's being executed. – N.B. Aug 22 '14 at 08:32
  • @ClémentMalet I updated my other code in the question, please. – user2333968 Aug 22 '14 at 08:37
  • 2
    @user2333968 Your main issue is that you do not want to combine both code snippets into a single file, because the resizing code *will* block the rest of the script from running. You would need to run your resizing code in a different process, which means you'll need to enter the, ahem, _wonderful_ world of pcntl: http://php.net/manual/en/book.pcntl.php – Duroth Aug 22 '14 at 08:41
  • This is clunky, but if you do happen to have a predictable image resize time, you could run the resize as a background task using `shell_exec(convert ... &)`, then run your progress bar for the specified time, and finish up with a line of code to wait for your file to exist `while (!file_exists($filename)) sleep(1);` Then display your image or redirect, whatever... – UltrasoundJelly Dec 04 '17 at 04:05

0 Answers0