I have a script that makes thumbnails of the pictures in a directory. But it's execution takes too long (about 170 images in the directory).
The script is called by ajax request. After 70% completion, I receive an error probably due to time out (takes about 3-4 minutes).
How can I solve this issue?
function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{
// open the directory
$dir = opendir( $pathToImages );
// loop through it, looking for any/all JPG files:
while (false !== ($fname = readdir( $dir ))) {
// parse path for the extension
$info = pathinfo($pathToImages . $fname);
// continue only if this is a JPEG image
if ( strtolower($info['extension']) == 'jpg' )
{
// load image and get image size
$img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
$width = imagesx( $img );
$height = imagesy( $img );
// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresampled( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
// save thumbnail into a file
imagejpeg( $tmp_img, "{$pathToThumbs}thumb_{$fname}" );
}
}
// close the directory
closedir( $dir );
}
createThumbs($directory,$directory."/thumbs/",150);
ajax call;
var ajaxr=$.ajax({
type: "POST",
url: "after_upload.php",
timeout:600,
beforeSend: function() {
$("#result").html('<div align="center"><h2>מבצע עיבוד נתונים יקח זמן ,חכה..תכין קפה בנתיים ותעשן סיגריה</h2><div><img src="loader.gif"/><div dir="rtl" style="margin:15px;">טוען מידע וממיר תמונות... <button id="cancel" style="padding:5px;">בטל פעולה ותחזור חזרה [X]</button></div></div> </div>');
},
success: function(data){
$("#result").html(data);
},
error: function(xhr, textStatus, errorThrown) {
$("#result").html(textStatus);
}
});
now,increased the time out to 3000 in ajax call and it instantly,for some reason return timeout error.if i remove time out property from the call ..it preforms the call and script executes..but only 70% of job is done..done returned empty error...
UPDATE:..i preformed everythin to make script execution time better now:console return 404 Not Found..