Fatal error: maximum execution time exceeded when generating mass thumbnails in php
I am trying to write a PHP script that will perform the following:
Extract all files from an archive in a given directory (using the exec function to actually run 7z)
List all the files that are images (jpg, jpeg, gif, png) and store them in an array (using exec again to run "ls" and storing only the images in the array, I compare the extension)
- Cycle through the array and for each image:
- Move that image in a unique folder
- Generate thumbnails in that same unique folder (I use PHP Thumbnailer)
- Record that new image in a database
For a small number of images, I would say that the script works ok (it could probably be improved a lot).
My problem is that after a certain number (which seems random each time I run my script) of treated images, I get a fatal error: PHP Fatal error: Maximum execution time of 60 seconds exceeded in /var/www/ims/public_html/dev/include/phpThumb/GdThumb.inc.php on line 217
imagecopyresampled ( $this->workingImage, $this->oldImage, 0, 0, 0, 0, $this->newDimensions['newWidth'], $this->newDimensions['newHeight'], $this->currentDimensions['width'], $this->currentDimensions['height'] ); // <- this is line 217
I am hoping that someone could point me towards a reason for that timeout. Is the GD library limited ? Should I install a dedicated software on my linux server to specifically perform this task?
Thanks for your help.
The 60 second time limit is for the entire scripted process. It is primarily intended to keep a rogue php child process from tying up the apache child (or other web server resource) on a loaded server.
If you are using php as a shell scripting language rather than as a web application I would recommend either set_time_limit(0); or making sure you are running the script in the php cli interpreter which does this automatically.
If you are trying to perform a large batch operation in response to a document upload/form post within a web server, you might want to look into using some sort of job server to offload the processing work to instead of keeping the web server tied up for a long period of time. Gearman is one such system.