Jump to content

No Errors? Maybe a time out?


prodigy2k7

Recommended Posts

Hello,

I am a novice PHP scripter. I mostly create small things or adjust php scripts i find online slightly and use them. This time I am having a problem.

 

This script takes images in a directory, makes thumbnails of the images 1 by 1 in it and puts it in "thumbs" directory.

 

It works fine, but if I have a lot of images, once it gets to about 30 seconds in... it just stops, with no errors, the browser says "Done" (firefox).

 

set_time_limit(0);
function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{
  $dir = opendir( $pathToImages );
  while (false !== ($fname = readdir( $dir ))) {
    $info = pathinfo($pathToImages . $fname);
    if ( strtolower($info['extension']) == 'jpg' )
    {
      echo "Creating thumbnail for {$fname} <br>";
      $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
      $width = imagesx( $img );
      $height = imagesy( $img );
      $new_width = $thumbWidth;
      $new_height = floor( $height * ( $thumbWidth / $width ) );
      $tmp_img = imagecreatetruecolor( $new_width, $new_height );
      imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
      imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
    }
  }
  echo "done1";
  closedir( $dir );
  echo "done2";
}

 

I tried for fun the two "echo"s at the end, it never reaches that.

 

I also tried "set_time_limit(0);" at the top, I dont know if I used that correctly, but ya..

 

Anyone have any ideas?

 

Thanks,

Kenny

Link to comment
Share on other sites

Actually max_execution_time is 30, which means your script will be stopped after 30 seconds. In the beginning of the script:

 

<?php
ini_set('max_execution_time', '180');
?>

 

From the manual:

You can not change this setting with ini_set() when running in safe mode. The only workaround is to turn off safe mode or by changing the time limit in the php.ini. 

Link to comment
Share on other sites

Here's one solution:

 

set_time_limit(0);
ob_implicit_flush();
for ($i = 0; $i < 100; $i++) {
  print "$i seconds<br>";
  sleep(1);
}

 

Try that out .. if it works, you can similarly print out messages to avoid browser timeout.  Keep in mind that it's not very efficient to send individual messages like this (it takes more network bandwith this way).

 

It may not work if you have mod_gzip active.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.