Forum Moderators: coopster

Message Too Old, No Replies

image processing progress: ajax or PHP only?

         

tunafish

11:45 pm on Apr 17, 2010 (gmt 0)

10+ Year Member



I have a PHP script that requires quite e few seconds of processing, up to about minute. The script resizes an array of high res images, sharpens them and finally zips them up for the user to download.

Because the script is unresponsive when processing, I need some sort of progress messages, like:
processing 001.jpg (1/12)
processing 002.jpg (2/12)
...

What is the best way of doing this?
I put something working together with jQuery.
It's a nice user experience because it's with ajax calls, but requires me to write a log file with the progress that I read with an interval from another script.

I think I saw something before done with E_USER_ERROR at runtime in PHP? Is this possible?



Here is what I have with the jQuery ajax calls right now:

$('a.download').click(function() {

var queryData = {images : ["001.jpg", "002.jpg", "003.jpg"]};

var progressCheck = function() {
$.get("progress.php",
function(data) {
$("p").html(data);
}
);
};

$.post('proccess.php', queryData,
function(intvalId) {
return function(data) {
$("p").html(data);
clearInterval(intvalId);
}
} (setInterval(progressCheck, 1000))
);

return false;
});


progress.php

$filename = "log.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
echo $contents;


proccess.php

$arr = $_POST['images'];
$arr_cnt = count($arr);

$filename = "log.txt";

$i = 1;
foreach ($arr as $val) {
$content = "processing $val ($i/$arr_cnt)";

$handle = fopen($filename, 'w');
fwrite($handle, $content);
fclose($handle);

$i++;
sleep(3); // to mimic image processing
}

echo "<a href='#'>download zip</a>";

Readie

11:57 pm on Apr 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



PHP happens before the page loads in the browser... So I don't think you can have a status message like this done in PHP.

JQuery/AJAX is probably your best bet.

tunafish

6:37 am on Apr 18, 2010 (gmt 0)

10+ Year Member



what about E_USER_ERROR?

Also, I tried writing the progress messages to $_session (instead of to a log file), but can not get it to work? Is it possible to change $_session variables at runtime and read them with another script?

Readie

1:33 pm on Apr 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know about E_USER_ERROR but it doesn't seem likely to me.

$_SESSION can be changed quite easily though.

$_SESSION['someName'] = 'Some String';

And that variable can be called with any PHP script on your server, as long as you have used the session_start() function.