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
https://forums.phpfreaks.com/topic/68605-no-errors-maybe-a-time-out/
Share on other sites

Bump!

I tried adding into the code after imagejpg

 

imagedestroy($tmp_img);

imagedestroy($img);

 

It seems this would be my problem, yet it still stops!

 

I dont know what to do.

 

Once again! $10 through paypal to anyone to helps me and fixes it.

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. 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.