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 217imagecopyresampled
(
$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.
php limits the amout of time a script is allowed to execute. You can increase this in php.ini or on a per-script basis using set_time_limit
Go to your php.ini
file and find max_execution_time
(about 30% of the way down in mine). Change that number to what you want.
Don't forget to output an indicator that your script is running as you expect so you can see that things are happening. Also, this tends to make my CPU temperature climb pretty quickly, indicating there's probably a better way to do things.
精彩评论